首页 > 代码库 > Exercise 3.4 A calculator that allows multiple calculations
Exercise 3.4 A calculator that allows multiple calculations
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 zero25 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 else35 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 continuing43 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 beginning48 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 add16 printf("= %lf\n", number1 + number2);17 break;18 19 case ‘-‘: // No checks necessary for subtract20 printf("= %lf\n", number1 - number2);21 break;22 23 case ‘*‘: // No checks necessary for multiply24 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 else31 printf("= %lf\n", number1 / number2);32 break;33 34 case ‘%‘: // Check second operand for zero35 if((long)number2 == 0) 36 printf("\n\n\aDivision by zero error!\n");37 else38 printf("= %ld\n", (long)number1 % (long)number2);39 break;40 41 default: // Operation is invalid if we get to here42 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。