PTA習題6-7 簡單計算器(20 分)
阿新 • • 發佈:2019-01-01
#include<stdio.h>
int main()
{
int num,sum,error=0;
char op='0';
scanf("%d",&sum);
scanf("%c",&op);
do{
if(op=='=') break;
scanf("%d",&num);
if(op=='+'){
sum+=num;
}else if(op=='-'){
sum-=num;
}else if(op=='*'){
sum*=num;
}else if(op=='/'){
if(num==0){
error=1;
break;
}else sum/=num;
}else{
error=1;
break;
}
scanf("%c",&op);
}while(op!='=');
if(error){
printf ("ERROR");
}else{
printf("%d",sum);
}
return 0;
}