Problem : 平面上的点和线——Point类、Line类 (VII)

本文介绍了一个关于平面上点和线的封装实践案例,通过C++实现Point类和Line类,展示了如何构造这些类并统计对象的创建与销毁过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说,先上题目为敬~

Problem G: 平面上的点和线——Point类、Line类 (VII)

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 2867   Solved: 2021
[ Submit][ Status][ Web Board]

Description

在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定,两点确定一条线段。现在我们封装一个“Point类”和“Line类”来实现平面上的点的操作。
根据“append.cc”,完成Point类和Line类的构造方法和show()方法,输出各Line对象和Point对象的构造和析构次序。
接口描述:
Point::showCounter()方法:按格式输出当前程序中Point对象的计数。
Point::showSum()方法:按格式输出程序运行至当前存在过的Point对象总数。
Line::showCounter()方法:按格式输出当前程序中Line对象的计数。
Line::showSum()方法:按格式输出程序运行至当前存在过的Line对象总数。

Input

输入的第一行为N,表示后面有N行测试样例。
每行为两组坐标“x,y”,分别表示线段起点和终点的x坐标和y坐标,两组坐标间用一个空格分开,x和y的值都在double数据范围内。

Output

输出格式见sample。
C语言的输入输出被禁用。

Sample Input

40,0 1,11,1 2,32,3 4,50,1 1,0

Sample Output

Current : 3 points.In total : 3 points.Current : 6 lines.In total : 6 lines.Current : 17 points.In total : 17 points.Current : 6 lines.In total : 7 lines.Current : 15 points.In total : 17 points.Current : 6 lines.In total : 8 lines.Current : 17 points.In total : 21 points.Current : 6 lines.In total : 9 lines.Current : 15 points.In total : 21 points.Current : 6 lines.In total : 10 lines.Current : 17 points.In total : 25 points.Current : 6 lines.In total : 11 lines.Current : 15 points.In total : 25 points.Current : 6 lines.In total : 12 lines.Current : 17 points.In total : 29 points.Current : 6 lines.In total : 13 lines.Current : 15 points.In total : 29 points.Current : 9 lines.In total : 17 lines.Current : 21 points.In total : 37 points.Current : 13 lines.In total : 21 lines.Current : 21 points.In total : 45 points.

HINT

Append Code

append.cc中的内容为
int main()
{
    int num, i;
    Point p(1, -2), q(2, -1), t;
    t.showCounter();
    t.showSum();
    std::cin>>num;
    Line line[num + 1];
    for(i = 1; i <= num; i++)
    {
        Line *l1, l2;
        l1->showCounter();
        l1->showSum();
        l1 = new Line(p, q);
        line[i].readLine();
        p.showCounter();
        p.showSum();
        delete l1;
        l2.showCounter();
        l2.showSum();
        q.showCounter();
        q.showSum();
    }
    Line l1(p, q), l2(p,t), l3(q,t), l4(l1);
    Line::showCounter();
    Line::showSum();
    Point::showCounter();
    Point::showSum();
    Line *l = new Line[num];
    l4.showCounter();
    l4.showSum();
    delete[] l;
    t.showCounter();
    t.showSum();
}
这是盯瞎了眼系列的最后一个题目~~答案如下~
#include <iostream>
using namespace std;
#include <iomanip>
class Point{
private:
    double x_,y_;
    friend class Line;
    static int sta1;
    static int sta2;
public:
    Point(double x,double y)
    {
        x_ = x;
        y_ = y;
        sta1++;
        sta2++;
        //cout<<"Point : ("<<x_<<", "<<y_<<") is created."<<endl;
    }
    Point(double d)
    {
        x_ = d;
        y_ = d;
        sta1++;
        sta2++;
    }
    Point()
    {
        x_ = 0;
        y_ = 0;
        sta1++;
        sta2++;
        //cout<<"Point : ("<<x_<<", "<<y_<<") is created."<<endl;
    }
    void setvalue(double xx,double yy)
    {
        x_ = xx;
        y_ = yy;
    }
    void setx(int xx) {x_ = xx;}
    void sety(int yy) {y_ = yy;}
    void show()
    {
        cout<<"Point : ("<<x_<<", "<<y_<<")"<<endl;
    }
    int x() { return x_;}
    int y() { return y_;}
    Point(const Point &p)
    {
        sta1++;
        sta2++;
        x_ = p.x_;
        y_ = p.y_;
        //cout<<"Point : ("<<x_<<", "<<y_<<") is copied."<<endl;
    }
    ~Point()
    {
       sta1 = sta1 - 1;
        //cout<<"Point : ("<<x_<<", "<<y_<<") is erased."<<endl;
    }
    static void showCounter()
    {
        cout<<"Current : "<<sta1<<" points."<<endl;
    }
    static void showSum()
    {
        cout<<"In total : "<<sta2<<" points."<<endl;
    }
};
 int Point::sta1(0);
 int Point::sta2(0);
class Line{friend class Point;
private:
    Point x1_,y1_;
    double x1,y1,x2,y2;
 
 
public:static int sta3;
    static int sta4;
    Line(double xx1,double yy1,double xx2,double yy2):x1_(xx1,yy1),y1_(xx2,yy2)
    {
        sta3++;
        sta4++;
        //cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;
    }
    Line(Point &q1,Point &q2):x1_(q1),y1_(q2)
    {
        sta3++;
        sta4++;
        //cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;
    }
    Line():x1_(),y1_(){sta3++; sta4++;/*cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;*/}
    Line setLine(double xx3,double yy3,double xx4,double yy4)
    {
 
        sta3++;
        sta4++;
        x1_.x_ = xx3;
        x1_.y_ = yy3;
        y1_.x_ = xx4;
        y1_.y_ = yy4;
        return *this;
    }
    void show()
    {
        cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<")"<<endl;
    }
    ~Line()
    {
        sta3 = sta3 - 1;
        //cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is erased."<<endl;
    }
    Line &setLine(const Point &p1,const Point &p2)
    {
        x1_ = p1;
        y1_ = p2;
        return *this;
    }
    Line &setLine(const Line& q)
    {
       x1_ = q.x1_;
       y1_ = q.y1_;
       return *this;
    }
    void readLine()
    {
       double x1,y1,x2,y2;
       char c;
       cin>>x1>>c>>y1>>x2>>c>>y2;
       x1_.x_ = x1;
       x1_.y_ = y1;
       y1_.x_ = x2;
       y1_.y_ = y2;
    }
    Line(const Line &b):x1_(b.x1_),y1_(b.y1_)
    {
        sta3++;
        sta4++;
        //cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is copied."<<endl;
    }
    static void showCounter()
    {
        cout<<"Current : "<<sta3<<" lines."<<endl;
    }
    static void showSum()
    {
        cout<<"In total : "<<sta4<<" lines."<<endl;
    }
};
int Line::sta3 = 0;
int Line::sta4(0);
 
int main()
{
    int num, i;
    Point p(1, -2), q(2, -1), t;
    t.showCounter();
    t.showSum();
    std::cin>>num;
    Line line[num + 1];
    for(i = 1; i <= num; i++)
    {
        Line *l1, l2;
        l1->showCounter();
        l1->showSum();
        l1 = new Line(p, q);
        line[i].readLine();
        p.showCounter();
        p.showSum();
        delete l1;
        l2.showCounter();
        l2.showSum();
        q.showCounter();
        q.showSum();
    }
    Line l1(p, q), l2(p,t), l3(q,t), l4(l1);
    Line::showCounter();
    Line::showSum();
    Point::showCounter();
    Point::showSum();
    Line *l = new Line[num];
    l4.showCounter();
    l4.showSum();
    delete[] l;
    t.showCounter();
    t.showSum();
}
 




题目描述 定义一个Point,表示平面上的,具有xy两个坐标成员变量,以及设置获取坐标的方法。还要实现计算两之间距离的方法。 输入描述 无输入。 输出描述 无输出。 样例 无样例。 提示 要求定义Point,具有如下成员: 成员变量: double x:表示的横坐标。 double y:表示的纵坐标。 成员函数: Point():构造函数,将xy初始值为0.0。 Point(double x, double y):构造函数,将xy初始值为参数值。 void setX(double x):设置的横坐标。 double getX():获取的横坐标。 void setY(double y):设置的纵坐标。 double getY():获取的纵坐标。 double distance(Point another):计算当前另外一个之间的距离,返回距离值。 注意:在的实现中,要包含头文件cmath,使用其中的sqrt函数求平方根。 C++ 代码 ```cpp #include <iostream> #include <cmath> using namespace std; class Point { private: double x, y; public: Point() : x(0.0), y(0.0) {} Point(double x, double y) : x(x), y(y) {} void setX(double x) { this->x = x; } double getX() { return x; } void setY(double y) { this->y = y; } double getY() { return y; } double distance(Point another) { double dx = x - another.x; double dy = y - another.y; return sqrt(dx * dx + dy * dy); } }; int main() { Point p1, p2(3.0, 4.0); p1.setX(1.0); p1.setY(2.0); cout << "p1: (" << p1.getX() << ", " << p1.getY() << ")" << endl; cout << "p2: (" << p2.getX() << ", " << p2.getY() << ")" << endl; cout << "distance between p1 and p2: " << p1.distance(p2) << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值