用c语言编写一个简单计算器
时间: 2023-05-26 08:01:53 浏览: 86
#include<stdio.h>
int main()
{
int a, b, res;
char op;
printf("Enter the operation (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
switch(op)
{
case '+':
res = a + b;
printf("%d + %d = %d", a, b, res);
break;
case '-':
res = a - b;
printf("%d - %d = %d", a, b, res);
break;
case '*':
res = a * b;
printf("%d * %d = %d", a, b, res);
break;
case '/':
if(b == 0)
{
printf("Error! Cannot divide by zero");
}
else
{
res = a / b;
printf("%d / %d = %d", a, b, res);
}
break;
default:
printf("Invalid Operator");
}
return 0;
}
阅读全文