码迷,mamicode.com
首页 > 其他好文 > 详细

Exercise 3.4 A calculator that allows multiple calculations

时间:2014-12-18 13:14:04      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   sp   for   on   div   

Exercise 3-4. Modify the last example in the chapter that implemented a calculator so
that the user is given the option to enter y or Y to carry out another calculation, and n or
N to end the program. (note: You’ll have to use a goto statement for this here, but you’ll
learn a better way of doing this in the next chapter.)

 

My:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     double number1 = 0.0;
 5     double number2 = 0.0;
 6     char operation = 0;
 7     
 8     start:
 9     printf("/nEnter the calculation");
10     scanf("%lf %c %lf", &number1, &operation, &number2);
11     
12     switch(operation)
13     {
14         case +:
15             printf("= %lf\n",number1 + number2);
16             break;
17         case -:
18             printf("%lf\n", &number1 - &number2);
19             break;
20         case*:
21             printf("%lf\n", &number1 * &number2);
22             break;
23         case/:
24             if(number2 == 0) //check the second operand for zero
25                 printf("\n\n\a Division by zero erro\n");
26                 break;
27             else 
28                 printf("%lf\n", &number / &number2);
29                 break;
30         case%:
31             if((long)number2 = 0;)
32                 printf("\n\n\aDivision by zero erro\n");
33                 break;
34             else
35                 printf("%lf\n", (long)number1 % (long)number2);
36                 break;
37         default:
38             printf("\n\n\aIllegal operation\n");
39             break;        
40     }
41     
42     //The followoing statements added to prompt for continuing
43     char answer = n;
44     printf("\n Do you want to do another calculation?(y or n);");
45     scanf("%c", &answer);
46     if(answer==y||answer == y);
47         go to start; // go back to the beginning
48         
49         return 0;
50     
51  } 

Original:

 1 /*Exercise 3.4 A calculator that allows multiple calculations */
 2 #include <stdio.h>
 3 
 4 int main(void)
 5 {
 6   double number1 = 0.0;          /* First operand value a decimal number  */
 7   double number2 = 0.0;          /* Second operand value a decimal number */
 8   char operation = 0;            /* Operation - must be +, -, *, /, or %  */
 9 start:
10   printf("\nEnter the calculation\n");
11   scanf("%lf %c %lf", &number1, &operation, &number2);
12 
13   switch(operation)
14   {
15     case +:                     // No checks necessary for add
16       printf("= %lf\n", number1 + number2);
17       break;
18 
19     case -:                     // No checks necessary for subtract
20       printf("= %lf\n", number1 - number2);
21       break;
22 
23     case *:                     // No checks necessary for multiply
24       printf("= %lf\n", number1 * number2);
25       break;                              
26 
27     case /:
28       if(number2 == 0)           // Check second operand for zero 
29         printf("\n\n\aDivision by zero error!\n");
30       else
31         printf("= %lf\n", number1 / number2);
32       break;
33 
34     case %:                    // Check second operand for zero
35       if((long)number2 == 0) 
36          printf("\n\n\aDivision by zero error!\n");
37       else
38         printf("= %ld\n", (long)number1 % (long)number2);
39       break;
40 
41     default:                     // Operation is invalid if we get to here
42       printf("\n\n\aIllegal operation!\n");
43       break;
44   }
45 
46   /* The following statements added to prompt for continuing */
47   char answer = n;
48   printf("\n Do you want to do another calculation? (y or n): ");
49   scanf(" %c", &answer);
50   if(answer == y || answer == Y)
51     goto start;                       /* Go back to the beginning */
52 
53   return 0;
54 }

 

Exercise 3.4 A calculator that allows multiple calculations

标签:style   blog   ar   io   color   sp   for   on   div   

原文地址:http://www.cnblogs.com/xiaomi5320/p/4171392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!