c语言设计一个简易计算器
时间: 2024-06-13 15:08:56 浏览: 166
以下是一个简单的C语言计算器程序,可以进行加、减、乘、除四种基本运算:
```c
#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
printf("*****************计算器*****************\n");
printf("**************1.加法计算****************\n");
printf("**************2.减法计算****************\n");
printf("**************3.乘法计算****************\n");
printf("**************4.除法计算****************\n");
printf("***************5.退出********************\n");
printf("*************请选择(1~~5)***************\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入两个数进行加法计算:\n");
scanf("%f %f", &num1, &num2);
result = num1 + num2;
printf("%.2f + %.2f = %.2f\n", num1, num2, result);
break;
case 2:
printf("请输入两个数进行减法计算:\n");
scanf("%f %f", &num1, &num2);
result = num1 - num2;
printf("%.2f - %.2f = %.2f\n", num1, num2, result);
break;
case 3:
printf("请输入两个数进行乘法计算:\n");
scanf("%f %f", &num1, &num2);
result = num1 * num2;
printf("%.2f * %.2f = %.2f\n", num1, num2, result);
break;
case 4:
printf("请输入两个数进行除法计算:\n");
scanf("%f %f", &num1, &num2);
if (num2 == 0) {
printf("除数不能为0!n");
} else {
result = num1 / num2;
printf("%.2f / %.2f = %.2f\n", num1, num2, result);
}
break;
case 5:
printf("退出计算器!\n");
break;
default:
printf("输入有误,请重新选择!\n");
break;
}
return 0;
}
```
阅读全文