C语言写出来的简单计算器
do…while无限循环中套入switch
#include<stdio.h>
int main(void)
{
char operator;
int esc;
double a, b, c;
do
{
printf("请输入运算符。\n");
printf("提醒:按q键退出计算器\n");
scanf_s(" %c", &operator, 1);
if (operator == 'q') {
return (1);
}
if (operator != '+' && operator != '-' && operator != '*' && operator!='/') {
printf("运算符错误,请重新输入ー\n");
continue;
}
printf("请输入数字。\n");
scanf_s("%lf%lf", &a, &b);
esc = 1;
switch (operator)
{
case '+':
c = a+b;
break;
case '-':
c = a-b;
break;
case '*':
c = a*b;
break;
case '/':
if (b == 0) {
printf("错误:除数不能为0\n");
esc = 0;
break;
}
c = a/b;
break;
}
if (esc != 0) {
printf("%lf%c%lf = %g\n", a, operator, b, c);
}
} while(1);
return 0;
}