yueyueyue8 于 2016.09.06 15:50 提问

c++设计一个简单计算器

2个回答

qq_29593541   2016.09.06 17:27

#include

int main(void)

{
int data1, data2;
char op;

scanf("%d %c %d", &data1 , &op , &data2);
switch (op)
{
case '+':
printf("%d + %d = %.0f\n", data1, data2, (double)data1 + (double)data2);
break;
case '-':

printf("%d - %d = %.0f\n", data1, data2, (double)data1 - (double)data2);
break;
case '*':
printf("%d * %d = %.0f\n", data1, data2, (double)data1 * (double)data2);
break;
case '/':
if (data2 == 0)
{
printf("Error! chu shu wei 0.");
}
else
{
printf("%d / %d = %.0f\n", data1, data2, (double)data1 / (double)data2);
}
break;
case '%':
if (data2 == 0)
{
printf("Error! chu shu wei 0.");
}
else
{
printf("%d %% %d=%d3\n", data1, data2, data1 % data2);
}
break;
default:
printf("Unknown operator\n");
}
return 0;
}如果要求是浮点数的话换一下数据类型就可以了

yueyueyue8 运行错误了，，，不能运行，这个程序运行环境是什么啊

C20180630   2016.09.08 12:35

`````` #include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,m;
char c;
scanf ("%d %d %c",&a,&b,&c);
if (c!='+'&&c!='-'&&c!='*'&&c!='/') printf("Invalid operator!");
else if (c=='+')    printf("%d",a+b);
else if (c=='-')    printf("%d",a-b);
else if (c=='*')    printf("%d",a*b);
else if (c=='/') b==0?printf("Divided by zero!"):printf("%d",a/b);
}
``````