2 yueyueyue8 yueyueyue8 于 2016.09.06 15:50 提问

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

设计一个程序来模拟一个简单的手持计算器。程序支持算术运算+、-、*、/、=,以及C(清除)、A(全清除)操作。用于测试时至少有5种计算方式

2个回答

qq_29593541
qq_29593541   2016.09.06 17:27

#include

int main(void)

{
int data1, data2;
char op;

printf("please input data1 op data2:");
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
yueyueyue8 运行错误了,,,不能运行,这个程序运行环境是什么啊
大约一年之前 回复
C20180630
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);
}
Csdn user default icon
上传中...
上传图片
插入图片