用C语言制作计算器菜单
时间: 2023-10-09 08:06:54 浏览: 137
好的,下面是用C语言制作计算器菜单的示例代码:
```
#include <stdio.h>
int main() {
int choice;
float num1, num2;
printf("Calculator Menu\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("%.2f + %.2f = %.2f", num1, num2, num1+num2);
break;
case 2:
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("%.2f - %.2f = %.2f", num1, num2, num1-num2);
break;
case 3:
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("%.2f * %.2f = %.2f", num1, num2, num1*num2);
break;
case 4:
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
if(num2 == 0) {
printf("Error: division by zero");
} else {
printf("%.2f / %.2f = %.2f", num1, num2, num1/num2);
}
break;
default:
printf("Invalid choice");
}
return 0;
}
```
这个程序会显示一个计算器菜单,让用户选择需要进行的操作。根据用户的选择,程序会请求用户输入相应的数字,并计算结果。注意程序中的格式化输出语句,它们会将计算结果显示为浮点数,并且保留两位小数。
阅读全文