c语言怎么才能已知三边求三角形面积
时间: 2024-06-12 12:06:56 浏览: 19
可以使用海龙公式来计算三角形的面积,海龙公式的公式为:
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
其中,a、b、c为三角形的三条边长,s为半周长,即三边之和的一半,sqrt表示开平方。
下面是一个使用海龙公式计算三角形面积的C语言程序示例:
```
#include <stdio.h>
#include <math.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("三角形的面积为:%.2f\n", area);
return 0;
}
```
在程序中,先从用户输入三角形的三条边长,然后计算出半周长s和面积area,最后输出面积结果。需要注意,由于使用了sqrt函数,需要在程序开头添加math.h头文件。
相关问题
c语言已知三边求三角形面积
以下是两种C语言已知三边求三角形面积的例子:
1.使用海伦公式计算三角形面积
```c
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, p, s;
printf("请依次输入三个边长\n");
scanf("%lf%lf%lf", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) //判断是否可以构成三角形。
{
p = (a + b + c) / 2; //计算半周长
s = sqrt(p * (p - a) * (p - b) * (p - c)); //套用海伦公式,计算面积
printf("面积为%lf\n", s); //输出结果
}
else
printf("无法构成三角形\n"); //输入不合法,提示。
return 0;
}
```
2.使用海伦公式计算三角形面积
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, p, s;
printf("Please enter three sides:\n");
scanf("%f%f%f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) // 构成三角形的条件
{
p = (a + b + c) / 2; // 周长的一半
s = sqrt(p * (p - a) * (p - b) * (p - c)); // 海伦公式
printf("The area of triangle is %f.", s);
}
else
printf("Data error!");
return 0;
}
```
c语言实现已知三点坐标求三角形面积
C语言实现已知三点坐标求三角形面积的步骤如下:
1. 定义三个点的坐标(x1,y1),(x2,y2),(x3,y3)
2. 根据公式计算三角形的三边长a,b,c,其中a=√[(x1-x2)²+(y1-y2)²],b=√[(x1-x3)²+(y1-y3)²],c=√[(x2-x3)²+(y2-y3)²]
3. 根据海伦公式计算三角形面积s,其中s=(a+b+c)/2,面积S=√[s(s-a)(s-b)(s-c)]
C语言代码如下:
```c
#include <stdio.h>
#include <math.h>
int main()
{
float x1, y1, x2, y2, x3, y3;
float a, b, c, s, S;
// 输入三个点的坐标
printf("请输入三个点的坐标(x1,y1),(x2,y2),(x3,y3):\n");
scanf("%f,%f,%f,%f,%f,%f", &x1, &y1, &x2, &y2, &x3, &y3);
// 计算三边长
a = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
b = sqrt(pow(x1-x3, 2) + pow(y1-y3, 2));
c = sqrt(pow(x2-x3, 2) + pow(y2-y3, 2));
// 计算面积
s = (a + b + c) / 2;
S = sqrt(s * (s - a) * (s - b) * (s - c));
// 输出结果
printf("三角形的面积为:%f\n", S);
return 0;
}
```
输入三个点的坐标后,程序会自动计算并输出三角形的面积。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](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)