added string2hex prog (usefull for wep key)
[my-code/beginners.git] / calc.c
1 #include <stdio.h>
2
3 char input[50];       /* for operator and number */
4 char choice[3];       /* for the choice i give you, hahahaha */
5 char operator;        /* tells me how to calculate */
6 char quit;            /* to quit the averager */
7
8 float result;         /* the result of the calculations */
9 int yachoi;           /* for ya choice that i gave you, hahahaha */
10 float value;          /* the value i have to calculate with */
11 int counter;          /* counting the amount of numbers to average */
12
13 int main()
14 {
15   printf ("Welcome to Frank's Calculator.\n");
16   printf ("\n");
17
18   while(1)    /* Loop 1  -- ya choice ! */
19   {
20
21    printf ("What do you want to do:\n");
22    printf ("1=Calculate\n");
23    printf ("2=Average an amount of numbers\n");
24    printf ("3=Quit Program\n");
25    printf ("\n");
26    printf ("Your choice: ");
27
28    fgets(choice, sizeof(choice), stdin);
29    sscanf(choice, "%d", &yachoi);
30    
31    if (yachoi == 3)
32      {
33       printf ("Thanks for using the Frankulator -- see ya!\n");
34       break;
35      }  
36
37     switch (yachoi)
38      {
39
40      case 1:
41
42        result = 0;  /* I don't want the computer to get a number by chance */
43
44        printf ("The CaLcUlAtOr --  press 'q` to go to main menu!\n");
45        printf ("\n");
46        while(1)        /* Loop 1.1  -- the calculator */
47          {
48           printf ("\n");
49           printf ("Result = %f !\n", result);
50           printf ("\n");
51           printf ("Enter operator and number: ");
52
53           fgets(input, sizeof(input), stdin);
54           sscanf(input, "%c %f", &operator, &value);
55
56           if (operator == 'q')
57             {
58              printf ("See ya dude!\n");
59              break;
60             }
61
62            switch (operator)
63              {
64             case '+':
65                 result += value;
66                 break;
67             case '-':
68                 result -= value;
69                 break;
70             case '*':
71                 result *= value;
72                 break;
73             case '/':
74                 if (value == 0)
75                 {
76                   printf("Illegal you stoopid! ...try again\n");
77                   break;
78                 }  
79                 result /= value;
80                 break;
81              default:
82                 printf ("Sorry, don't know operator...try another!\n");
83                 break;
84              }
85          }
86       break;
87          
88      case 2:
89
90        printf ("Welcome to the aVeRaGeR  -- press 'q` for main menu!\n");
91        printf ("\n");
92
93        result = 0;
94        counter = 1;
95
96        while(1)
97          {
98         printf ("Enter number %d : ",counter);
99         
100         fgets(input, sizeof(input), stdin);
101         sscanf(input, "%c", &operator);
102          
103         if (operator == 'q')
104           {
105            if (counter == 1)
106              {
107               printf ("Stoopido...  try anothertime, he!\n");
108               break;
109              }
110            printf ("The average is : %f !\n",result/(counter-1));
111            break;
112           }
113         
114         sscanf(input, "%f", &value);
115         result += value;
116         ++counter;
117        } 
118       break;
119
120      default:
121
122        printf ("Come on try again...stoopid!\n");
123        printf ("\n");
124        break;
125      }    
126   }
127 return (0);
128 }
129
130