C++ experiment 03 (03) combination class: Triangle class and Point class

Title description
Define a point class Point in a plane coordinate system, with integer data members x, y coordinate values. The member functions include: (1) Constructor with default value, the default value is 0; (2) Copy constructor; (3) Set x, y coordinate value; (4) Take x, y coordinate value, the parameter is Two integer references are used to obtain the x and y coordinate values. (5) Output function, used to output x, y coordinate values. (6) A function to find the distance between two points. The parameter is an object reference of the Point class.
Define a triangle type Triangle in a plane coordinate system, and the data members are three Point objects p1, p2, p3. The member functions include: (1) Constructor with parameters. The parameters are integers x1, y1, x2, y2, x3, y3, which are the coordinates of the three vertices of the triangle. (2) Constructor with parameters, the parameters are references to three Point class objects. (3) Find the perimeter of the triangle. (4) Find the area of ​​the triangle. (5) Output the coordinates, perimeter and area of ​​the three vertices of the triangle.
Define an ordinary function: determine whether the coordinates of the three vertices can form a triangle.
In main(), input the coordinates of three points from the keyboard to determine whether the three points can form a triangle. If not, then prompt to re-enter and re-enter the point coordinates; if yes, output three vertex coordinates, perimeter and area.
Input description
The coordinates of the three points. If the triangle cannot be formed, re-enter the coordinates of the three points.
Output description The coordinates of the
three vertices
, the circumference of the triangle and the area of ​​the triangle.
Input example
0 0
1 1
2 2

0 0
5 6
3 0
Output example The
vertex coordinates are not correct and cannot form a triangle! Please re-enter the coordinates!
The coordinates of the three vertices of the triangle are:
(0,0) (5,6) (3,0) The
perimeter of the triangle is 17.1348 and the area is 9

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
    
    
public:
	Point(int a=0,int b=0)
	{
    
    
		x=a;
		y=b;
	}
	Point(const Point &a)//拷贝构造函数
	{
    
    
		x=a.x;
		y=a.y;
	}
	void input()
	{
    
    
		cin>>x>>y;
	}
	int getx(int &x)
	{
    
    
		return x;
	}
	int gety(int &y)
	{
    
    
		return y;
	}
	void output()
	{
    
    
		cout<<"("<<x<<","<<y<<")";
	}
	double distance(Point &a)
	{
    
    
		double dx=x-a.x,dy=y-a.y;
		return sqrt(dx*dx+dy*dy);
	}
private:
	int x,y;
};
class Triangle
{
    
    
public:
	Triangle(int x1,int y1,int x2,int y2,int x3,int y3)
	{
    
    
		A=(x1,y1);
		B=(x2,y2);
		C=(x3,y3);
	}
	Triangle(Point &a,Point &b,Point &c)
	{
    
    
		A=a;
		B=b;
		C=c;
	}
	double Len()
	{
    
    
		return A.distance(B)+B.distance(C)+C.distance(A);
	}
	double Area()
	{
    
    
		double s=Len()/2.0;
		return sqrt(s*(s-A.distance(B))*(s-B.distance(C))*(s-C.distance(A)));
	}
	void output()
	{
    
    	
		cout<<endl<<"三角形周长为:"<<Len()<<",面积为:"<<Area();
	}
private:
	Point A,B,C;
};
bool ifTriangle(Point &p1,Point &p2,Point &p3)
{
    
    	
	here:
	if((p1.distance(p2)+p2.distance(p3)>p3.distance(p1))&& (p1.distance(p2) + p3.distance(p1) > p2.distance(p3)) && (p2.distance(p3) + p3.distance(p1) > p1.distance(p2)))
	{
    
    	
		cout << "三角形三个顶点坐标为:" << endl;
		p1.output();
		p2.output();
		p3.output();
		Triangle T(p1,p2,p3);
		T.output();
	}
	else
	{
    
    
		cout << "顶点坐标不正确,不能构成三角形!请重新输入坐标!" << endl;
		return false;
	}
	
}
int main()
{
    
    
	Point A,B,C;
	here:
	A.input();
	B.input();
	C.input();
	if (ifTriangle(A, B, C) == false)
	{
    
    
		goto here;
	}
	return 0;
}

Determine whether it is a triangle

The sum of the two sides is greater than the third side
or (the difference between the two sides is less than the third side)
but three must be listed,
such as
a+b>c&&a+c>b&&b+c>a

if((p1.distance(p2)+p2.distance(p3)>p3.distance(p1))&& (p1.distance(p2) + p3.distance(p1) > p2.distance(p3)) && (p2.distance(p3) + p3.distance(p1) > p1.distance(p2)))
	```

Guess you like

Origin blog.csdn.net/weixin_44179485/article/details/105754192