/*计算器循环代码*/
#include<stdio.h>
#include<math.h>
int main(void)
{
double number1=0.0;
double number2=0.0;
char operation=0; /*操作数必须为'+'、'-'、'*'、'/'、'%'*/
char re=0;
ret:
{
printf("请输入计算式:\n");
scanf("%lf %c %lf",&number1,&operation,&number2);
switch(operation)
{
case '+':
printf("\t=%lf\n",number1+number2);
break;
case '-':
printf("\t=%lf\n",number1-number2);
break;
case '*':
printf("\t=%lf\n",number1*number2);
break;
case '/':
if(number2==0)
printf("输入错误,除数不能为零\n");
else
printf("\t=%lf\n",number1/number2);
break;
case '%':
if(number2==0)
printf("输入错误,除数不能为零\n");
else
printf("\t=%ld\n",(long)number1%(long)number2);
break;
default:
printf("输入错误\n");
break;
}
}
ret1:
{
printf("是否继续计算(Y or N):");
scanf("%c",&re);
switch(re)
{
case 'Y':case 'y':
goto ret;
break;
case 'N':case 'n':
return 0;
break;
default:
printf("输入错误");
goto ret1;
break;
}
}
return 0;
}
运行结果如下所示:
多了一行的不需要的提示,解决方法为程序中两处scanf()函数后添加一个getchar()函数来接收回车字符。