请用c语言写一个简单的计算器
时间: 2023-09-11 09:08:15 浏览: 74
Sure, I can help you with that! Here's a simple calculator program in C:
```
#include <stdio.h>
int main()
{
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch(operator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator\n");
return 1;
}
printf("Result: %.2lf\n", result);
return 0;
}
```
You can copy and paste this code into your favorite C compiler and run it. When prompted, enter the operator (+, -, *, or /) and the two numbers you want to perform the calculation on. The program will then display the result of the operation. Enjoy!
阅读全文