c语言窗体简易计算器代码
时间: 2023-12-19 11:31:48 浏览: 120
c语言简单计算器代码
以下是一个使用 C 语言实现简易计算器的例子,它可以在控制台中进行加、减、乘、除四种运算:
```c
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
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("Error! Invalid operator.");
return 1;
}
printf("%.2lf %c %.2lf = %.2lf", num1, operator, num2, result);
return 0;
}
```
阅读全文