用C语言写代码多种图形求面积 描述 编写一个程序,实现求矩形,三角形,圆形的面积(PI取3. 1415926,输出结果取整) 输入 先输入一个整数type, 如果type=1,则输入长宽a b求矩形面积。如果type=2,则输入底高a b求三角形面积,如果type=3,则输入半径r求圆形面积。 输出 输出面积 输入样例1自 输出样例1 123 6
时间: 2023-02-07 18:50:40 浏览: 116
你可以使用以下代码来实现你的需求:
```
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926
int main(int argc, char* argv[]) {
int type;
scanf("%d", &type);
if (type == 1) {
// 求矩形面积
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", a * b);
} else if (type == 2) {
// 求三角形面积
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", a * b / 2);
} else if (type == 3) {
// 求圆形面积
int r;
scanf("%d", &r);
printf("%d\n", (int)(PI * r * r));
} else {
printf("无效的类型\n");
}
return 0;
}
```
在这段代码中,我们先输入一个整数 `type`,表示当前要求的图形的类型。然后,使用 `if-else` 语句来判断输入的类型,并根据需要输入相应的参数来计算面积。最后,我们使用 `printf` 函数来输出结果。
相关问题
使用c语言写一个求三角形面积的代码
以下是使用 C 语言编写的求三角形面积的代码:
#include <stdio.h>
int main() {
float base, height, area;
printf("请输入三角形的底边长:");
scanf("%f", &base);
printf("请输入三角形的高:");
scanf("%f", &height);
area = 0.5 * base * height;
printf("三角形的面积为:%f", area);
return 0;
}
注意:这个程序只适用于计算普通三角形的面积,不适用于计算等边三角形、等腰三角形等特殊情况。
用c语言写一个求三角形面积的程序
我可以为您提供一个示例程序:#include <stdio.h>int main()
{
float a, b, c, s, area; printf("请输入三角形的三条边:");
scanf("%f%f%f", &a, &b, &c); s = (a + b + c) / 2;
area = sqrt(s * (s-a) * (s-b) * (s-c)); printf("三角形的面积是: %f", area); return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)