我写的简单计算器代码,请高手看看不好之处!谢谢了!
void main(){
double operand1,operand2,result;
char ch;
printf("请输入计算式:\n");
do
{
scanf("%lf%c",&operand1,&ch);
while(ch!='=')
{
scanf("%lf",&operand2);
switch(ch)
{
case '+':
result=operand1+operand2;
operand1=result;
scanf("%c",&ch);
break;
case '-':
result=operand1-operand2;
operand1=result;
scanf("%c",&ch);
break;
case '*':
result=operand1*operand2;
operand1=result;
scanf("%c",&ch);
break;
case '/':
if(operand2!=0)
{
result=operand1/operand2;
operand1=result;
scanf("%c",&ch);
}
else
ch='=';
break;
case '%':
if(operand2!=0)
{
result=(int)operand1%(int)operand2;
operand1=result;
scanf("%c",&ch);
}
else
ch='=';
break;
default :
printf("你的运算符错误,请重新编译输入计算式:\n");
break;
}
}
if(operand2!=0)
printf("%f\n",result);
else
printf("运算数为零,请重新编译输入计算式:\n");
}while(operand2!=0);
}