c语言 已知三角形的三边,求三角形面积,将其编写成一个函数,在主函数中调用验证。 #include <stdio.h> .... .... .... int main(){ double a,b,c; scanf("%lf%lf%lf",&a,&b,&c); printf("%lf\n",TriArea(a,b,c)); } 输入输出样例: 4 5 6 (注:运行时的输入) 9.921567 (注:运行时的输出)
时间: 2024-02-05 15:03:42 浏览: 32
可以使用海伦公式计算三角形面积,将其封装成一个函数。具体代码如下:
```
#include <stdio.h>
#include <math.h>
double TriArea(double a, double b, double c) {
double s = (a + b + c) / 2; // 计算半周长
double area = sqrt(s * (s - a) * (s - b) * (s - c)); // 计算面积
return area;
}
int main() {
double a, b, c;
scanf("%lf%lf%lf", &a, &b, &c);
printf("%lf\n", TriArea(a, b, c));
return 0;
}
```
在主函数中,首先输入三角形的三边长,然后调用 `TriArea` 函数计算面积并输出。函数中的计算方法与上面的代码相同。
相关问题
用C语言写一个程序,已知三角形三条边,利用自写的函数求三角形面积
好的,我可以帮你写一个程序。以下是代码:
```c
#include <stdio.h>
#include <math.h>
float calculate_area(float a, float b, float c);
int main() {
float a, b, c, area;
printf("请输入三角形三边长度:\n");
scanf("%f %f %f", &a, &b, &c);
area = calculate_area(a, b, c);
printf("三角形面积为:%f\n", area);
return 0;
}
float calculate_area(float a, float b, float c) {
float p = (a + b + c) / 2;
float area = sqrt(p * (p - a) * (p - b) * (p - c));
return area;
}
```
函数`calculate_area`用来计算三角形面积,参数为三角形三边长度,返回值为三角形面积。在`main`函数中,先读入三角形三边长度,再调用`calculate_area`函数计算面积并输出。
用C语言编写一个程序,已知三角形三边求面积的函数,对于给定的3个量(正值).按两边之和大于第三边的规定,判别其能否构成三角形,若能构成三角形,输出对应的三角形面积。要求主函数输入10组三角形三边,输出其构成三角形的情况。
好的,以下是用C语言编写的程序:
```c
#include <stdio.h>
#include <math.h>
float area(float a, float b, float c) {
float p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
int main() {
float a, b, c;
for (int i = 0; i < 10; i++) {
printf("请输入第%d组三角形的三边长:", i + 1);
scanf("%f%f%f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) {
printf("第%d组三边长能构成三角形,其面积为%.2f\n", i + 1, area(a, b, c));
} else {
printf("第%d组三边长不能构成三角形\n", i + 1);
}
}
return 0;
}
```
程序的逻辑很简单,首先定义一个求三角形面积的函数 `area`,然后在主函数中循环输入10组三角形的三边长,判断是否能构成三角形,如果能,就调用 `area` 函数计算面积并输出,否则输出不能构成三角形的信息。
注意,在求面积的函数中,我们使用了 `math.h` 库中的 `sqrt` 函数来计算平方根,需要在编译时链接该库。如果使用 gcc 编译器,可以添加 `-lm` 选项来链接 `math` 库:
```
gcc -o triangle triangle.c -lm
```
希望这个程序能够帮到你,有任何疑问可以随时问我哦!
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)