两个数的简单计算器
本题要求编写一个简单计算器程序,可根据输入的运算符,对2个整数进行加、减、乘、除或求余运算。题目保证输入和输出均不超过整型范围。
方法一:
#include <stdio.h>
//#include <math.h>
int main(){
int a,b,s;
char c;
scanf("%d %c %d",&a,&c,&b);
if(c=='+'){
s = a+b;
printf("%d",s);
}
else if(c=='-'){
s = a-b;
printf("%d",s);
}
else if(c=='*'){
s = a*b;
printf("%d",s);
}
else if(c=='/'){
if(b!=0){
s = a/b;
printf("%d",s);
}
}
else if(c=='%'){
s = a%b;
printf("%d",s);
}
else{
printf("ERROR");
}
return 0;
}
方法二:
#include <stdio.h>
//#include <math.h>
int main(){
int a,b,s;
char c;
scanf("%d %c %d",&a,&c,&b);
switch(c){
case '+':printf("%d",a+b);break;
case '-':printf("%d",a-b);break;
case '*':printf("%d",a*b);break;
case '/':printf("%d",a/b);break;
case '%':printf("%d",a%b);break;
default:printf("ERROR");
}
return 0;
}