c++ 求四边形面积和周长_C语言编程题 题目:任意输入4个点,求围成四边形的面积是多少?C语言编程题 题目:任意输入4...
时间: 2024-03-09 08:48:19 浏览: 28
好的,这是一个不错的编程题目,下面是 C++ 代码实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x1, y1, x2, y2, x3, y3, x4, y4;
double a, b, c, d, e, f;
double p, area;
cout << "请输入4个点的坐标(x,y):" << endl;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
// 计算四边形的周长
a = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
b = sqrt((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2));
c = sqrt((x4-x3)*(x4-x3) + (y4-y3)*(y4-y3));
d = sqrt((x1-x4)*(x1-x4) + (y1-y4)*(y1-y4));
e = sqrt((x3-x1)*(x3-x1) + (y3-y1)*(y3-y1));
f = sqrt((x4-x2)*(x4-x2) + (y4-y2)*(y4-y2));
double perimeter = a + b + c + d;
// 计算四边形的面积
double s1 = (a + b + e) / 2;
double s2 = (c + d + f) / 2;
double area1 = sqrt(s1 * (s1-a) * (s1-b) * (s1-e));
double area2 = sqrt(s2 * (s2-c) * (s2-d) * (s2-f));
area = area1 + area2;
cout << "四边形的周长为:" << perimeter << endl;
cout << "四边形的面积为:" << area << endl;
return 0;
}
```
这个程序首先要求用户输入四个点的坐标,然后用勾股定理计算出四边形的各个边长,再根据海龙公式求出四边形的面积,最后输出周长和面积。注意,这里假设输入的四个点是按照逆时针方向给出的,如果是按照顺时针方向,那么计算出来的面积将是负数。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)