简单的计算器实现

  1 /************************************************************************/
  2 /*                           简单的计算器实现                             */
  3 /************************************************************************/
  4 
  5 
  6 #include <stdio.h>
  7 #include <ctype.h>
  8 
  9 void Play(void);
 10 void GetS(void);
 11 float Get_one(void);
 12 float Get_two(void);
 13 
 14 int main()
 15 {
 16     int ch;
 17     float one;
 18     float two;
 19 
 20     Play();
 21     while ((ch = getchar()) != 'q')
 22     {
 23         switch(ch)
 24         {
 25         case 'a':
 26             one = Get_one();
 27             two = Get_two();
 28             printf("%.2f + %.2f = %.2f\n", one, two, one + two);
 29             break;
 30         case 's':
 31             one = Get_one();
 32             two = Get_two();
 33             printf("%.2f - %.2f = %.2f\n", one, two, one - two);
 34             break;
 35         case 'm':
 36             one = Get_one();
 37             two = Get_two();
 38             printf("%.2f * %.2f = %.2f\n", one, two, one * two);
 39             break;
 40         case 'd':
 41             one = Get_one();
 42             two = Get_two();
 43             printf("%.2f / %.2f = %.2f\n", one, two, one / two);
 44             break;
 45 
 46         }
 47         while (getchar() !='\n')
 48         {
 49             continue;
 50         }
 51         Play();
 52     }
 53     printf("Good Bye!");
 54 
 55     getch();
 56 
 57     return 0;
 58 }
 59 
 60 void Play(void)
 61 {
 62     printf("Enter the Operation of your choice:\n");
 63     printf("a. add\t\ts. subtract\n");
 64     printf("m. multiply\td. divide\n");
 65     printf("q. quit\n");
 66 }
 67 
 68 void GetS(void)
 69 {
 70     int ch;
 71 
 72     while ((ch = getchar()) != '\n')
 73     {
 74         putchar(ch);
 75     }
 76 }
 77 
 78 float Get_one(void)
 79 {
 80     float one;
 81     
 82     while (1)
 83     {
 84         printf("Enter First number:");
 85         if (scanf("%f", &one) && one != 0)
 86         {
 87             return one;                
 88         }
 89         GetS();                
 90         printf(" is not an number\n");
 91 
 92     }
 93 
 94 }
 95 
 96 float Get_two(void)
 97 {
 98     float two;
 99     while (1)
100     {
101         printf("Enter second number:");
102         if (scanf("%f", &two) && two != 0)
103         {
104             return two;    
105         }
106         GetS();                
107         printf(" is not an number\n");    
108 
109     }
110 }

 

posted @ 2013-10-25 09:42  donsh  阅读(228)  评论(0编辑  收藏  举报