1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include <iostream>
- //根据点的定义,写出线段和三角形的定义,并通过主程序进行验证
- //mypoint.h
- class myPoint
- {
- public:
- myPoint();
- myPoint(double x, double y);
- double getX();
- double getY();
- private:
- double mX,mY;
- };
- #include <iostream> // 用于cout, cin, endl
- #include <cmath> // 用于pow, sqrt
- using namespace std;
- // Line类的声明
- class Line {
- private:
- myPoint p1, p2;
- public:
- Line(myPoint a, myPoint b) : p1(a), p2(b) {}
- double GetDistance() {
- return sqrt(pow(p2.getX() - p1.getX(), 2) + pow(p2.getY() - p1.getY(), 2));
- }
- };
- // Triangle类的声明
- class Triangle {
- private:
- myPoint p1, p2, p3;
- Line l1, l2, l3;
- public:
- Triangle(myPoint a, myPoint b, myPoint c) : p1(a), p2(b), p3(c), l1(a, b), l2(a, c), l3(b, c) {}
- double getGirth() {
- return l1.GetDistance() + l2.GetDistance() + l3.GetDistance();
- }
- double getArea() {
- double s = getGirth() / 2;
- return sqrt(s * (s - l1.GetDistance()) * (s - l2.GetDistance()) * (s - l3.GetDistance()));
- }
- };
- // myPoint类的实现
- myPoint::myPoint() : mX(0), mY(0) {}
- myPoint::myPoint(double x, double y) : mX(x), mY(y) {}
- double myPoint::getX() { return mX; }
- double myPoint::getY() { return mY; }
- 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),line2(p1,p3),line3(p2,p3);
- 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;
- }
|