【问题描述】 根据点的类构造线三角形类,并测试 【输入形式】输入三角形的3个坐标点 【输出形式】三角形的3个坐标点、3边长及周长和面积 【样例输入】 请输入点1的x的值:1 请输入点1的y的值:1 请输入点2的x的值:4 请输入点2的y的值:5 请输入点3的x的值:8 请输入点3的y的值:9 【样例输出】 点1的坐标为:(1,1) 点2的坐标为:(4,5) 点3的坐标为:(8,9) 边长1的长度:5 边长2的长度:10.6301 边长3的长度:5.65685 该三角形的周长为:21.287 该三角形的面积为:2
时间: 2024-02-20 07:59:04 浏览: 33
以下是 C++ 的代码实现,可以实现题目要求:
```
#include <iostream>
#include <cmath>
using namespace std;
// 定义点类
class Point {
public:
double x; // 横坐标
double y; // 纵坐标
Point(double x, double y) {
this->x = x;
this->y = y;
}
};
// 定义线段类
class Line {
public:
Point p1; // 线段的起点
Point p2; // 线段的终点
Line(Point p1, Point p2) {
this->p1 = p1;
this->p2 = p2;
}
// 计算线段长度
double getDistance() {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt(dx * dx + dy * dy);
}
};
// 定义三角形类
class Triangle {
public:
Point p1; // 三角形的第一个顶点
Point p2; // 三角形的第二个顶点
Point p3; // 三角形的第三个顶点
Triangle(Point p1, Point p2, Point p3) {
this->p1 = p1;
this->p2 = p2;
this->p3 = p3;
}
// 计算三角形周长
double getGirth() {
Line l1(p1, p2);
Line l2(p1, p3);
Line l3(p2, p3);
return l1.getDistance() + l2.getDistance() + l3.getDistance();
}
// 计算三角形面积
double getArea() {
double a = p1.x - p2.x;
double b = p1.y - p2.y;
double c = p1.x - p3.x;
double d = p1.y - p3.y;
double e = p2.x - p3.x;
double f = p2.y - p3.y;
double g = p1.x - p2.x;
double h = p1.y - p2.y;
double i = p2.x - p3.x;
double j = p2.y - p3.y;
double k = (a * d - b * c) / 2;
return abs(k);
}
};
int main() {
double x1, x2, x3, y1, y2, y3;
cout << "请输入点1的x的值:";
cin >> x1;
cout << "请输入点1的y的值:";
cin >> y1;
cout << "请输入点2的x的值:";
cin >> x2;
cout << "请输入点2的y的值:";
cin >> y2;
cout << "请输入点3的x的值:";
cin >> x3;
cout << "请输入点3的y的值:";
cin >> y3;
Point p1(x1, y1), p2(x2, y2), p3(x3, y3);
cout << "点1的坐标为:(" << p1.x << "," << p1.y << ")" << endl;
cout << "点2的坐标为:(" << p2.x << "," << p2.y << ")" << endl;
cout << "点3的坐标为:(" << p3.x << "," << p3.y << ")" << endl;
Line l1(p1, p2), l2(p1, p3), l3(p2, p3);
cout << "边长1的长度:" << l1.getDistance() << endl;
cout << "边长2的长度:" << l2.getDistance() << endl;
cout << "边长3的长度:" << l3.getDistance() << endl;
Triangle t(p1, p2, p3);
cout << "该三角形的周长为:" << t.getGirth() << endl;
cout << "该三角形的面积为:" << t.getArea() << endl;
return 0;
}
```
以上代码中,Point 类表示点,Line 类表示线段,Triangle 类表示三角形,实现了计算三角形周长和面积的功能。
相关推荐
![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)
![](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)