博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Exercise 3.4 A calculator that allows multiple calculations
阅读量:4962 次
发布时间:2019-06-12

本文共 3759 字,大约阅读时间需要 12 分钟。

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
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 
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 }

 

转载于:https://www.cnblogs.com/xiaomi5320/p/4171392.html

你可能感兴趣的文章
Oracle操作语言分类
查看>>
Metasploits之ms10_018
查看>>
ubuntu14.04安装Thinkphp
查看>>
在Apline编译Mariadb 常见错误
查看>>
C#操作windows服务
查看>>
python虚拟环境--virtualenv
查看>>
mac os x忘记了root密码怎么办,忘记登录密码(普通帐号密码)也是一样的
查看>>
判断日期段是否交叉
查看>>
C++类学习心得
查看>>
iview 中Tree 树形控件,遇到的问题
查看>>
个人总结
查看>>
【转】Java中finally的执行时机
查看>>
HttpClient之可恨的Expect(C# http 请求卡住的解决办法)
查看>>
N皇后问题
查看>>
HTML5终极备忘大全(图片版+文字版)
查看>>
超类、子类、主程序执行终极步骤(二)
查看>>
DirectFB 之 字体显示(2)
查看>>
nginx典型官方模块解释
查看>>
数据结构学习记录_2019.02.23
查看>>
使用Android Studio来阅读Android源码
查看>>