程序可以运行 的数也是对的 但是输出出现错位 而且有些输出的地方不一样
这是我和答案输出的不同
可以看到应该是length=答案 area=答案 但是length area=和答案输出错了位 差了好几行
而且有些地方应该输出copyconstructor run但却输出成destructor run
有些地方应该输出destructor run却成了copyconstructor run
下面是答案要求得到的输出
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Triangle Constructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Triangle CopyConstructor run
Triangle(A=Point(X=0,Y=0),B=Point(X=0,Y=0),C=Point(X=0,Y=0))
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Area=0
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Length=0
Triangle(A=Point(X=3,Y=2),B=Point(X=1,Y=1),C=Point(X=2,Y=2))
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Area=0.5
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Length=4.65028
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
下面是我的输出
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Triangle Constructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Triangle CopyConstructor run
Triangle(A=Point(X=0,Y=0),B=Point(X=0,Y=0),C=Point(X=0,Y=0))
Area=Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
0
Length=Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
0
Triangle(A=Point(X=3,Y=2),B=Point(X=1,Y=1),C=Point(X=2,Y=2))
Area=Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
0.5
Length=Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
4.65028
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
下面是我的代码
注意1行到61行 和152行到182行是题里给的程序测试样例 是改不了的 其他地方可以改
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point(double xv = 0, double yv = 0);
Point(const Point &p);
~Point();
virtual void show() const;
void setX(double xv);
void setY(double yv);
double getX() const;
double getY() const;
};
Point::Point(const double xv, const double yv)
{
x = xv;
y = yv;
cout << "Point Constructor run" << endl;
}
Point::Point(const Point &p)
{
x = p.x;
y = p.y;
cout << "Point CopyConstructor run" << endl;
}
Point::~Point()
{
cout << "Point Destructor run" << endl;
}
void Point::show() const
{
cout << "Point(X=" << x << ",Y=" << y << ")";
}
void Point::setX(double xv)
{
x = xv;
}
void Point::setY(double yv)
{
y = yv;
}
double Point::getX() const
{
return x;
}
double Point::getY() const
{
return y;
}
class Plane
{
public:
virtual double length() const = 0; /*周长*/
virtual double area() const = 0; /*面积*/
};
double pointDistance(Point p1, Point p2)
{
double distance = 0;
distance = sqrt((p1.getY() - p2.getY()) * (p1.getY() - p2.getY()) + (p1.getX() - p2.getX()) * (p1.getX() - p2.getX()));
return distance;
}
class Triangle : public Plane
{
private:
Point A;
Point B;
Point C;
public:
Triangle(const Point &a = Point(0, 0), const Point &b = Point(0, 0), const Point &c = Point(0, 0));
Triangle(const Triangle &);
~Triangle();
void setA(const Point &);
Point getA() const;
void setB(const Point &);
Point getB() const;
void setC(const Point &);
Point getC() const;
void show() const;
double area() const;
double length() const;
};
Triangle::Triangle(const Point &a, const Point &b, const Point &c):A(a),B(b),C(c)
{
cout << "Triangle Constructor run" << endl;
}
Triangle::Triangle(const Triangle &t):A(t.A),B(t.B),C(t.C)
{
cout << "Triangle CopyConstructor run" << endl;
}
Triangle::~Triangle()
{
cout << "Triangle Destructor run" << endl;
}
void Triangle::setA(const Point &a)
{
A = a;
}
Point Triangle::getA() const
{
return A;
}
void Triangle::setB(const Point &b)
{
B = b;
}
Point Triangle::getB() const
{
return B;
}
void Triangle::setC(const Point &c)
{
C = c;
}
Point Triangle::getC() const
{
return C;
}
void Triangle::show() const
{
cout << "Triangle(A=Point(X=" << A.getX() << ",Y=" << A.getY() << "),B=Point(X=" << B.getX() << ",Y=" << B.getY() << "),C=Point(X=" << C.getX() << ",Y=" << C.getY() << "))";
}
double Triangle::area() const
{
double area = 0;
double a, b, c, s;
a = pointDistance(A, B);
b = pointDistance(B, C);
c = pointDistance(A, C);
s = 0.5 * (a + b + c);
area = sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
double Triangle::length() const
{
double length = 0;
double a, b, c;
a = pointDistance(A, B);
b = pointDistance(B, C);
c = pointDistance(A, C);
length = a + b + c;
return length;
}
void length(Plane *p)
{
cout << "Length=" << p->length() << endl;
}
void area(Plane &p)
{
cout << "Area=" << p.area() << endl;
}
int main(void)
{
double x, y;
Point p1, p2(1, 1), p3(2, 2);
Triangle t1, t2(t1);
t1.show();
cout << endl;
area(t1);
length(&t1);
cin >> x >> y;
p1.setX(x);
p1.setY(y);
t2.setA(p1);
t2.setB(p2);
t2.setC(p3);
t2.show();
cout << endl;
area(t2);
length(&t2);
return 0;
}