用switch语句,根据用户的选择计算相应的形状(长方形,圆形,三角形)的面积
时间: 2023-05-18 17:07:15 浏览: 61
好的,以下是使用switch语句计算不同形状面积的示例代码:
```c
#include <stdio.h>
int main() {
int choice;
float area, width, height, radius, base, side1, side2;
printf("请选择要计算面积的形状:\n");
printf("1. 长方形\n");
printf("2. 圆形\n");
printf("3. 三角形\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入长方形的宽度和高度:\n");
scanf("%f %f", &width, &height);
area = width * height;
printf("长方形的面积为:%.2f\n", area);
break;
case 2:
printf("请输入圆形的半径:\n");
scanf("%f", &radius);
area = 3.14 * radius * radius;
printf("圆形的面积为:%.2f\n", area);
break;
case 3:
printf("请输入三角形的底和高:\n");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("三角形的面积为:%.2f\n", area);
break;
default:
printf("输入的选择不正确!\n");
break;
}
return 0;
}
```
希望这个示例代码能够帮到你!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)