类的组合:点,线,三角形
要求输入三点的坐标求三点各两点所围成线段长度和所围成三角形的周长和面积
#include<math.h>
#include <iostream>
using namespace std;
class myPoint
{
public:
myPoint();
myPoint(double x, double y);
double getX();
double getY();
private:
double mX, mY;
};
myPoint::myPoint()
{
this->mX = 0;
this->mY = 0;
}
myPoint::myPoint(double x, double y)
{
this->mX = x;
this->mY = y;
}
double myPoint::getX()
{
return mX;
}
double myPoint::getY()
{
return mY;
}
class Line
{
public:
Line(myPoint& a,myPoint& b)
{
A = a; B = b;
}
Line(double x, double y)
{
this->x = x;
this->y = y;
}
Line()
{
this->x = 0;
this->y = 0;
}
double GetDistance()
{
double dx = A.getX() - B.getX();
double dy = A.getY() - B.getY();
return sqrt(dx * dx + dy * dy);
}
private:
myPoint A, B;
double x=0, y=0;
};
class Triangle
{
public:
Triangle(myPoint& a, myPoint& b, myPoint& c):A(a, b), B(a, c), C(b, c)
{
}
double getGirth()
{
double len;
len = A.GetDistance() + B.GetDistance() + C.GetDistance();
return len;
}
double getArea()
{
double len,s,t;
len = A.GetDistance() + B.GetDistance() + C.GetDistance();
s = len / 2.0;
t = sqrt(s * (s - A.GetDistance()) * (s - B.GetDistance()) * (s - C.GetDistance()));
return t;
}
private:
myPoint X, Y, Z;
Line A, B, C;
};
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;
cout << "点1的坐标为:(" << x1 << "," << y1 << ")" << endl;
cout << "点2的坐标为:(" << x2 << "," << y2 << ")" << endl;
cout << "点3的坐标为:(" << x3 << "," << y3 << ")" << endl;
myPoint p1(x1, y1), p2(x2, y2), p3(x3, y3);
Line line1(p1, p2);
Line line2(p2, p3);
Line line3(p3, p1);
cout << "线1长度:" << line1.GetDistance() << endl;
cout << "线2长度:" << line2.GetDistance() << endl;
cout << "线3长度:" << line3.GetDistance() << endl;
Triangle t(p1, p2, p3);
cout << "该三角形的周长为:" << t.getGirth() << endl;
cout << "该三角形的面积为:" << t.getArea() << endl;
return 0;
}
看完点个赞吧呜呜呜