题目1
【问题描述】
根据点的类构造线和三角形类,并测试
1 2 3 4 5 6 7 8 9 10 11 12
|
class myPoint { public: myPoint(); myPoint(double x, double y); double getX(); double getY(); private: double mX,mY; };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 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); cout<<"线长度:"<<line1.GetDistance()<<endl; Triangle t(p1, p2, p3); cout << "该三角形的周长为:" << t.getGirth() << endl; cout << "该三角形的面积为:" << t.getArea() << endl; return 0; }
|
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include<iostream> #include<cmath> using namespace std; myPoint::myPoint(double x,double y) {mX=x; mY=y; } myPoint::myPoint() {mX=0; mY=0; } double myPoint::getX() {return mX;} double myPoint::getY() {return mY;} class Line {myPoint x; myPoint y; public: Line(myPoint b,myPoint k):x(b),y(k) { } double GetDistance() {double b; b=sqrt((x.getX()-y.getX())*(x.getX()-y.getX())+((x.getY()-y.getY())*(x.getY()-y.getY()))); return b; }}; class Triangle {myPoint x,y,z; public: Triangle(myPoint h,myPoint s,myPoint a):x(h),y(s),z(a) { } double getGirth() {Line a(x,y); Line b(y,z); Line c(x,z); double C; C=a.GetDistance()+b.GetDistance()+c.GetDistance(); return C; } double getArea() {Line a(x,y); Line b(y,z); Line c(x,z); double p; p=(this->getGirth())/2; double f; f=sqrt(p*(p-a.GetDistance())*(p-b.GetDistance())*(p-c.GetDistance())); return f; } };
|
题目2
【问题描述】
编写类描述如下图形并求出阴影(蓝色)部分的面积
圆形 正方形 组合图形
【输入形式】
输入圆半径:2
输入正方形边长:3
【输出形式】
圆面积为:12.56
正方形面积为:9
组合面积为:3.56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include<iostream> using namespace std; const double PI=3.14; class Circle {
}; class Square {
};
int main() { double r, len;Circle *pc;Square *ps; cout << "输入圆半径:"; cin >> r; Circle r1(r); cout << "输入正方形边长:"; cin >> len; Square s1(len); Mix m1(r, len); pc=&r1; cout<<"圆面积为:"<<pc->getArea()<<endl; ps=&s1; cout<<"正方形面积为:"<<ps->getArea()<<endl;
cout<<"组合面积为:"<<m1.getArea()<<endl; return 0; }
|
【代码】
1 2 3 4 5 6 7 8 9 10 11 12
| private: int r; public: Circle(){} Circle(int r) { this->r=r; } double getArea() { return PI*r*r; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| private: int x; public: Square(){} Square(int x) { this->x=x; } double getArea() { return x*x; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Mix { private: Square b; Circle c; public: Mix(Circle C,Square B) { b=B,c=C; } void set(Circle C,Square B) { c=C,b=B; } double getArea() { return c.getArea()-b.getArea(); }
};
|
代码可能略有不足,请大佬多多指教