编写函数area(a,b,c),返回以a,b,c为三边的三角形面积。输入三角形三边,利用该函数求出三角形面积。要求程序中对输入的三边值做合法性判断
时间: 2024-06-13 20:04:51 浏览: 26
```python
def area(a, b, c):
if a + b > c and a + c > b and b + c > a:
p = (a + b + c) / 2
s = (p * (p - a) * (p - b) * (p - c)) ** 0.5
return s
else:
return "输入的三边不能构成三角形"
```
以上是一个Python函数,可以计算以a,b,c为三边的三角形面积。在函数中,首先判断输入的三边是否能构成三角形,如果不能则返回“输入的三边不能构成三角形”,否则利用海伦公式计算三角形面积并返回。
以下是使用该函数的示例代码:
```python
a = float(input("请输入三角形第一条边长:"))
b = float(input("请输入三角形第二条边长:"))
c = float(input("请输入三角形第三条边长:"))
s = area(a, b, c)
print(s)
```
在示例代码中,首先通过input函数获取用户输入的三角形三边长度,然后调用area函数计算三角形面积并将结果赋值给变量s,最后输出s的值。
相关问题
c语言编写程序,输入三角形的三边长a,b,c,求三角形面积area,不构成三角形输出不能构成三角形
下面是一个用 C 语言实现的程序,可以根据输入的三角形三边长计算出三角形的面积:
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, s, area;
printf("请输入三角形的三边长:\n");
scanf("%f%f%f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a)
{
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("该三角形的面积为:%f\n", area);
}
else
{
printf("输入的三边长不能构成三角形!\n");
}
return 0;
}
```
代码中,先通过 `scanf` 函数输入三角形的三条边长,然后判断这三条边能否构成三角形。如果能够构成三角形,则根据海伦公式计算三角形的面积并输出;否则输出提示信息。
编写程序求三角形的面积。已知三角形的三边a、b、c,则三角形的面积为
根据海伦公式,三角形的面积可以通过三条边a、b、c计算得出:
```
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
```
其中,s为半周长,sqrt为平方根函数。你可以使用这个公式来编写计算三角形面积的程序。以下是一个Python代码示例:
```
import math
a = float(input("请输入三角形的第一条边长:"))
b = float(input("请输入三角形的第二条边长:"))
c = float(input("请输入三角形的第三条边长:"))
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print("三角形的面积为:", area)
```
在程序中,我们使用了Python的math库中的sqrt函数来计算平方根。程序会提示用户输入三角形的三条边长,然后计算并输出三角形的面积。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)