简单的计算器

来源:互联网 发布:剑灵天族女捏脸数据库 编辑:程序博客网 时间:2024/06/06 03:13
#include <stdio.h>int main(){int a,b,res;char c;while(1){printf("Please enter the calculation formula to calculate.\nIf you want to exit,please press CTRL + C.\n ");scanf("%d%c%d",&a,&c,&b);switch(c){case '+' :res=a+b;printf("result:%d\n",res);break;case '-' :res = a - b;printf("result:%d\n",res);break;case '*' :res = a * b;printf("result:%d\n",res);break;case '/' :if(b == 0){printf("Incorrect input,please re-enter.\n");break;}else{res = a / b;printf("result:%d\n",res);break;}default :printf("Incorrect input,please re-enter.\n");break;}}return 0;}

简单的计算器,通关switch语句进行对符号的选择,其中被除数不能为0.switch 选择计算符号,+,-,*,/。

当运算符为 + 的时候计算加法,a+b;

当运算符为 - 的时候计算减法,a-b;

当运算符为 * 的时候计算乘法,a*b;

当运算符为 / 的时候计算除法,先判断b是否为0,b为0,被除数为0,错误计算退出。否则就计算a/b。