added beginner c sources
[my-code/beginners.git] / calc.c
diff --git a/calc.c b/calc.c
new file mode 100644 (file)
index 0000000..a4e99aa
--- /dev/null
+++ b/calc.c
@@ -0,0 +1,130 @@
+#include <stdio.h>
+
+char input[50];       /* for operator and number */
+char choice[3];       /* for the choice i give you, hahahaha */
+char operator;        /* tells me how to calculate */
+char quit;            /* to quit the averager */
+
+float result;         /* the result of the calculations */
+int yachoi;           /* for ya choice that i gave you, hahahaha */
+float value;          /* the value i have to calculate with */
+int counter;          /* counting the amount of numbers to average */
+
+int main()
+{
+  printf ("Welcome to Frank's Calculator.\n");
+  printf ("\n");
+
+  while(1)    /* Loop 1  -- ya choice ! */
+  {
+
+   printf ("What do you want to do:\n");
+   printf ("1=Calculate\n");
+   printf ("2=Average an amount of numbers\n");
+   printf ("3=Quit Program\n");
+   printf ("\n");
+   printf ("Your choice: ");
+
+   fgets(choice, sizeof(choice), stdin);
+   sscanf(choice, "%d", &yachoi);
+   
+   if (yachoi == 3)
+     {
+      printf ("Thanks for using the Frankulator -- see ya!\n");
+      break;
+     }  
+
+    switch (yachoi)
+     {
+
+     case 1:
+
+       result = 0;  /* I don't want the computer to get a number by chance */
+
+       printf ("The CaLcUlAtOr --  press 'q` to go to main menu!\n");
+       printf ("\n");
+       while(1)        /* Loop 1.1  -- the calculator */
+        {
+          printf ("\n");
+          printf ("Result = %f !\n", result);
+          printf ("\n");
+          printf ("Enter operator and number: ");
+
+          fgets(input, sizeof(input), stdin);
+          sscanf(input, "%c %f", &operator, &value);
+
+          if (operator == 'q')
+           {
+             printf ("See ya dude!\n");
+             break;
+            }
+
+           switch (operator)
+            {
+           case '+':
+                result += value;
+                break;
+            case '-':
+                result -= value;
+                break;
+           case '*':
+                result *= value;
+                break;
+            case '/':
+                if (value == 0)
+               {
+                  printf("Illegal you stoopid! ...try again\n");
+                  break;
+               }  
+                result /= value;
+                break;
+            default:
+                printf ("Sorry, don't know operator...try another!\n");
+                break;
+            }
+        }
+      break;
+        
+     case 2:
+
+       printf ("Welcome to the aVeRaGeR  -- press 'q` for main menu!\n");
+       printf ("\n");
+
+       result = 0;
+       counter = 1;
+
+       while(1)
+        {
+        printf ("Enter number %d : ",counter);
+        
+        fgets(input, sizeof(input), stdin);
+        sscanf(input, "%c", &operator);
+         
+        if (operator == 'q')
+         {
+           if (counter == 1)
+            {
+              printf ("Stoopido...  try anothertime, he!\n");
+              break;
+            }
+           printf ("The average is : %f !\n",result/(counter-1));
+           break;
+         }
+        
+        sscanf(input, "%f", &value);
+        result += value;
+        ++counter;
+       } 
+      break;
+
+     default:
+
+       printf ("Come on try again...stoopid!\n");
+       printf ("\n");
+       break;
+     }   
+  }
+return (0);
+}
+
+