由用户输入一个实现两个整数的四则运算(+-*/)的表达式,并给出结果。

如果用户输入的除数为0,则输出:

Divisor can not be 0

如果输入的符号不是+-*/,则输出:

input error

/* C语言程序设计题,由用户输入一个实现两个整数的四则运算(+-* /)的表达式
并给出结果。如果用户输入的除数为0,则输出: Divisor can not be 0
如果输入的符号不是+-* /,则输出: input error */
#include "stdio.h"
void main()
{
int x,y,result;
float temp;
char oprand,flag='Y';
while(flag=='Y'||flag=='y')
{
do
{
printf("请输入四则运算式子\n");
scanf("%d%c%d",&x,&oprand,&y);
if(oprand!='+'&&oprand!='-'&&oprand!='*'&&oprand!='/')printf("Input error\n");
}while(oprand!='+'&&oprand!='-'&&oprand!='*'&&oprand!='/');
switch(oprand)
{
case '+':result=x+y;printf("result=%d\n",result);break;
case '-':result=x-y;printf("result=%d\n",result);break;
case '*':result=x*y;printf("result=%d\n",result);break;
case '/':
if(y==0)printf("Divisor can not be 0\n");
else {temp=(float)x;printf("result=%f\n",temp/y);}
break;
}
getchar();
printf("是否继续运算?Y/y or N/n\n");
scanf("%c",&flag);
}
}
这是一个可以循环进行测试的程序,并且输入运算符直到正确为止。
温馨提示:答案为网友推荐,仅供参考