用c语言编程简便计算器,求大神解答
发布网友
发布时间:2022-05-16 07:22
我来回答
共1个回答
热心网友
时间:2023-10-15 02:00
#include <stdio.h>
int main()
{
int a,b,r;
float r_div;
char <a href="https://www.baidu.com/s?wd=op&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4PjFbPWFbP16sn1f4rjw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1m4rHmLn1D" target="_blank" class="-highlight">op</a>;
scanf("%d",&a);//读入操作数a
<a href="https://www.baidu.com/s?wd=op&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4PjFbPWFbP16sn1f4rjw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1m4rHmLn1D" target="_blank" class="-highlight">op</a> = getchar();//读入运算符号
scanf("%d",&b);//读入操作数b
switch(<a href="https://www.baidu.com/s?wd=op&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4PjFbPWFbP16sn1f4rjw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1m4rHmLn1D" target="_blank" class="-highlight">op</a>)//根据运算符号,进行对应的<a href="https://www.baidu.com/s?wd=%E5%8A%A0%E5%87%8F%E4%B9%98%E9%99%A4&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4PjFbPWFbP16sn1f4rjw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1m4rHmLn1D" target="_blank" class="-highlight">加减乘除</a>运算。
{
case '+':
r = a+b;//加法
break;
case '-':
r = a-b;//减法
break;
case '*':
r = a*b;//乘法
break;
case '/':
r_div = a/(float)b;//除法
break;
default:
printf("unknow operator\n");//未知操作符,属于输入错误。退出程序。
return -1;
}
//根据操作符号,输入对应的运行结果。
if(op == '/')printf("%d%c%d=%f\n", a,op,b,r_div);
else printf("%d%c%d=%d\n", a,op,b,r);
return 0;
}