【嵙大】CPP实验合集03(经测试全部通过)

发布于:2025-07-11 ⋅ 阅读:(18) ⋅ 点赞:(0)

【oj】【CPP】实验03
  • 注:本次不再单独附上append.cc中提供的main函数部分

Contest - 物联网工程2024级面向对象程序设计-实验3

Start Time: 2025-05-28 16:00:00 End Time: 2025-05-28 18:00:00
Current Time: 2025-7-3 10:51:51 Status:Ended  Private

Problem ID Title
1123 Problem A 平面上的点——Point类 (III)
1124 Problem B 平面上的点——Point类 (IV)
1322 Problem C 静态数据成员
1323 Problem D 静态成员函数
1778 Problem E 点在圆内吗?
最难的一集 1779 Problem F 质心算法

Problem A: 平面上的点——Point类 (III)

Time Limit: 1 Sec  Memory Limit: 4 MB
Submit: 11170  Solved: 7097
[Submit][Status]

Description

在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。

根据“append.cc”,完成Point类的构造方法和show()方法,输出各Point对象的构造和析构次序。实现showPoint()函数。

接口描述:
showPoint()函数按输出格式输出Point对象,调用Point::show()方法实现。
Point::show()方法:按输出格式输出Point对象。

Input

输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内。

Output

输出每个Point对象的构造和析构行为。showPoint()函数用来输出(通过参数传入的)Point对象的值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。输出格式见sample。

C语言的输入输出被禁用。

Sample Input

1,2 3,3 2,1

Sample Output

Point : (0, 0) is created. Point : (1, 2) is created. Point : (1, 2) is copied. Point : (1, 2) Point : (1, 2) is erased. Point : (1, 2) is erased. Point : (3, 3) is created. Point : (3, 3) is copied. Point : (3, 3) Point : (3, 3) is erased. Point : (3, 3) is erased. Point : (2, 1) is created. Point : (2, 1) is copied. Point : (2, 1) Point : (2, 1) is erased. Point : (2, 1) is erased. Point : (0, 0) is copied. Point : (1, 1) is created. Point : (0, 0) is copied. Point : (1, 1) is copied. Point : (0, 0) is copied. Point : (0, 0) Point : (1, 1) Point : (0, 0) Point : (0, 0) is erased. Point : (1, 1) is erased. Point : (0, 0) is erased. Point : (1, 1) is erased. Point : (0, 0) is erased. Point : (0, 0) is erased.

HINT

思考构造函数、拷贝构造函数、析构函数的调用时机。

Append Code

#include <iostream>
#include <iomanip>
#include <cstring>


using namespace std;


class Point{
private:
    double x;
    double y;
public:
    Point()
    {
        x=0;
        y=0;
        cout<<"Point : ("<<x<<", "<<y<<") is created."<<endl;
    }
    ~Point()
    {
        cout<<"Point : ("<<x<<", "<<y<<") is erased."<<endl;
    }
    Point(double x,double y)
    {
        this->x=x;
        this->y=y;
        cout<<"Point : ("<<x<<", "<<y<<") is created."<<endl;
    }
    Point(double x)
    {
        this->x=x;
        y=1;
        cout<<"Point : ("<<x<<", "<<y<<") is created."<<endl;
    }
    Point(const Point&p)
    {
        this->x=p.x;
        this->y=p.y;
        cout<<"Point : ("<<x<<", "<<y<<") is copied."<<endl;
    }
    void show()
    {
        cout<<"Point : ("<<x<<", "<<y<<")"<<endl;
    }

};
void showPoint(Point p)
{
    p.show();
}
void showPoint(Point p1,Point p2,Point p3)//不是传引用
{
    p1.show();
    p2.show();
    p3.show();
}

int main()
{
    char c;
    double a, b;
    Point q;//无参构造 默认00——输出created
    while(std::cin>>a>>c>>b)
    {
        Point p(a, b);//含双参构造
        showPoint(p);
    }
    Point q1(q), q2(1);//拷贝构造 单参构造
    showPoint(q1, q2, q);//传入的全部是对象
}

Problem B: 平面上的点——Point类 (IV)

Time Limit: 1 Sec  Memory Limit: 4 MB
Submit: 12938  Solved: 7586
[Submit][Status]

Description

在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。

根据“append.cc”,完成Point类的构造方法和show()、showCounter()、showSumOfPoint()方法;实现showPoint()函数。

接口描述:
showPoint()函数:按输出格式输出Point对象,调用Point::show()方法实现。
Point::show()方法:按输出格式输出Point对象。
Point::showCounter()方法:按格式输出当前程序中Point对象的计数。
Point::showSumOfPoint()方法:按格式输出程序运行至当前存在过的Point对象总数。

Input

输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内。

Output

对每个Point对象,调用show()方法输出其值,或者用showPoint()函数来输出(通过参数传入的)Point对象的值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。调用用showCounter()方法和showSumOfPoint()输出Point对象的计数统计,输出格式见sample。

C语言的输入输出被禁用。
所以不能提到stdio

Sample Input

1,2 3,3 2,1

Sample Output

Point : (1, 2) Current : 2 points. Point : (3, 3) Current : 2 points. Point : (2, 1) Current : 2 points. In total : 4 points. Current : 3 points. Point : (0, 0) Point : (1, 1) Point : (0, 0) In total : 6 points.

HINT

对象计数通过静态成员来实现

Append Code


#include <iomanip>
#include <iostream>
#include <cstring>


using namespace std;



//静态成员和静态成员函数
class Point{
private:
    double x;
    double y;

    //静态数据成员
    static int cnt;
    static int allcnt;
public:
    Point()
    {
        x=0;y=0;
        cnt++;
        allcnt++;
    }
    Point(double a,double b)
    {
        x=a;
        y=b;
        cnt++;
        allcnt++;
    }
    void show()
    {
        cout<<"Point : ("<<setprecision(16)<<x<<", "<<setprecision(16)<<y<<")"<<endl;
    }
    static void showCounter()
    {
        cout<<"Current : "<<cnt<<" points."<<endl;
    }
    static void showSumOfPoint()
    {
        cout<<"In total : "<<allcnt<<" points."<<endl;
    }
    ~Point()
    {
        cnt--;
    }
    Point(const Point& a)
    {
        this->x=a.x;
        this->y=a.y;
        cnt++;
        allcnt++;
    }
    Point(double x)
    {
        this->x=x;
        y=1;
        cnt++;
        allcnt++;
    }
};
int Point::cnt=0;
int Point::allcnt=0;

void showPoint(Point& a,Point& b,Point& c)//不知道是不是传引用
{
    a.show();
    b.show();
    c.show();
}


int main()
{
    char c;
    double a, b;
    Point q;//所有构造里面都要cnt++//默认构造
    while(std::cin>>a>>c>>b)
    {
        Point p(a, b);//双参数构造
        p.show();
        p.showCounter();
    }
    q.showSumOfPoint();
    Point q1(q), q2(1);//拷贝构造和单参数构造
    Point::showCounter();//在调用静态成员函数
    showPoint(q1, q2, q);
    Point::showSumOfPoint();
}

Problem C: 静态数据成员

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1500  Solved: 954
[Submit][Status]

Description

定义一个Person类,有age(int类型)、sex(bool类型)两个非静态数据成员,和一个名为num的静态数据成员。num用于表示当前所创建的对象是第几个。实现其默认构造函数、带参数的构造函数和拷贝构造函数,使得所给出的main函数能够产生如下输出:

The No.1 person, age=?,sex=?? is created!

The No.2 person, age=?,sex=?? is copyed!

The No.3 person, age=0,sex=F is created!

The No.4 person, age=0,sex=F is copyed!

其中,

默认构造函数使用age=0和sex=false来构造一个对象;带参数的构造函数使用指定的参数构造一个对象。上述输出中的?是同一个值,??也是同样的值,并且要用所创建对象的实际数值所代替。当sex=true时,??为字母M,否则??是字母F。

输出可参考样例。

Input

输入2个数:第一个数是age属性的值,第二个数是sex属性的值。其中第二个数只能是0和1(0表示false,1表示true)。

Output

输出格式见样例。

Sample Input

45 1

Sample Output

The No.1 person, age=45,sex=M is created! The No.2 person, age=45,sex=M is copyed! The No.3 person, age=0,sex=F is created! The No.4 person, age=0,sex=F is copyed!

HINT

 静态数据成员如何初始化?

Append Code


#include <iomanip>
#include <iostream>
#include <cstring>


using namespace std;

class Person{
private:
    int age;
    bool sex;
    static int num;

public:
    Person()
    {
        age=0;
        sex=false;
        num++;
        if(sex)
            cout<<"The No."<<num<<" person, age="<<age<<",sex=M is created!"<<endl;
        else
            cout<<"The No."<<num<<" person, age="<<age<<",sex=F is created!"<<endl;
    }
    Person(int a,bool c)
    {
        age=a;
        sex=c;
        num++;
        if(sex)
            cout<<"The No."<<num<<" person, age="<<age<<",sex=M is created!"<<endl;
        else
            cout<<"The No."<<num<<" person, age="<<age<<",sex=F is created!"<<endl;
    }
    Person(const Person& p)
    {
        num++;
        this->age=p.age;
        this->sex=p.sex;
        if(sex)
            cout<<"The No."<<num<<" person, age="<<age<<",sex=M is copyed!"<<endl;
        else
            cout<<"The No."<<num<<" person, age="<<age<<",sex=F is copyed!"<<endl;
    }


};
int Person::num=0;

int main()
{
    int a;
    bool s;
    cin>>a>>s;
    Person p1(a,s),p2(p1),p3,p4(p3);
    return 0;
}

Problem D: 静态成员函数

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1117  Solved: 829
[Submit][Status]

Description

定义一个Person类,有age(int类型)、sex(bool类型)两个非静态数据成员,和一个名为num的静态数据成员。num用于表示当前所创建的对象是第几个。实现其默认构造函数、带参数的构造函数、拷贝构造函数和一个静态成员函数showNum,使得所给出的main函数能够产生如下输出:

The No.1 person, age=?,sex=?? is created!

There are 1 persons are created!

The No.2 person, age=?,sex=?? is copyed!

The No.3 person, age=0,sex=F is created!

The No.4 person, age=0,sex=F is copyed!

There are 4 persons are created!

其中,

默认构造函数使用age=0和sex=false来构造一个对象;带参数的构造函数使用指定的参数构造一个对象。上述输出中的?是同一个值,??也是同样的值,并且要用所创建对象的实际数值所代替。当sex=true时,??为字母M,否则??是字母F。

showNum的原型为:

static void showNum();

输出可参考样例。

Input

输入2个数:第一个数是age属性的值,第二个数是sex属性的值。其中第二个数只能是0和1(0表示false,1表示true)。

Output

输出格式见样例。

Sample Input

33 0

Sample Output

The No.1 person, age=33,sex=F is created! There are 1 persons are created! The No.2 person, age=33,sex=F is copyed! The No.3 person, age=0,sex=F is created! The No.4 person, age=0,sex=F is copyed! There are 4 persons are created!

HINT

Append Code


#include <iomanip>
#include <iostream>
#include <cstring>


using namespace std;

class Person{
private:
    int age;
    bool sex;
    static int num;

public:
    Person()
    {
        age=0;
        sex=false;
        num++;
        if(sex)
            cout<<"The No."<<num<<" person, age="<<age<<",sex=M is created!"<<endl;
        else
            cout<<"The No."<<num<<" person, age="<<age<<",sex=F is created!"<<endl;
    }
    Person(int a,bool c)
    {
        age=a;
        sex=c;
        num++;
        if(sex)
            cout<<"The No."<<num<<" person, age="<<age<<",sex=M is created!"<<endl;
        else
            cout<<"The No."<<num<<" person, age="<<age<<",sex=F is created!"<<endl;
    }
    Person(const Person& p)
    {
        num++;
        this->age=p.age;
        this->sex=p.sex;
        if(sex)
            cout<<"The No."<<num<<" person, age="<<age<<",sex=M is copyed!"<<endl;
        else
            cout<<"The No."<<num<<" person, age="<<age<<",sex=F is copyed!"<<endl;
    }
    static void showNum()
    {
        cout<<"There are "<<num<<" persons are created!"<<endl;
    }

};
int Person::num=0;

int main()
{
    int a;
    bool s;
    cin>>a>>s;
    Person p1(a,s);
    Person::showNum();
    Person p2(p1),p3,p4(p3);
    p2.showNum();
    return 0;
}

Problem E: 点在圆内吗?

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 7100  Solved: 3771
[Submit][Status]

Description

定义一个Point类和Circle类,用于判断给定的一系列的点是否在给定的圆内。

其中,Point类:

1.有2个成员x和y,分别为其横坐标和纵坐标;1个静态成员numOfPoints,用于计算生成的点的个数。

2.具有构造函数、析构函数和拷贝构造函数,具体格式输出根据样例自行判断。

  1. 具有静态方法int getNumOfPoints(),用于返回numOfPoints的值。

  2. 具有int getX()和int getY()方法,用于获取横坐标和纵坐标。

Circle类:

  1. 拥有Point类的对象center,表示圆心坐标。拥有radius对象,表示圆的半径;1个静态成员numOfCircles,用于指示生成了多少个圆对象。

  2. 具有构造函数、析构函数和拷贝构造函数,具体格式根据样例自行判断。

3.具有静态方法int getNumOfCircles(),返回numOfCircles的值。

  1. 具有getCenter()方法,返回圆心坐标。注意:根据输出结果判断返回值类型。

  2. 具有bool pointInCircle(Point &)方法,用于判断给定的点是否在当前圆内。是则返回true,否则返回false。

Input

输入分多行。

第一行M>0,表示有M个测试用例。

每个测试用例又包括多行。第1行包含3个整数,分别为一个圆的横坐标、纵坐标和半径。第2行N>0,表示之后又N个点,每个点占一行,分别为其横坐标和纵坐标。

所有输入均为整数,且在int类型范围内。

Output

输出见样例。注意:在圆的边上的点,不在圆内。

Sample Input

2 0 0 10 3 2 2 11 11 10 0 1 1 20 3 2 2 1 1 100 100

Sample Output

The Point (0, 0) is created! Now, we have 1 points. The Point (1, 1) is created! Now, we have 2 points. A circle at (1, 1) and radius 1 is created! Now, we have 1 circles. We have 2 points and 1 circles now. The Point (0, 0) is created! Now, we have 3 points. A Point (0, 0) is copied! Now, we have 4 points. A Point (0, 0) is copied! Now, we have 5 points. A circle at (0, 0) and radius 10 is created! Now, we have 2 circles. A Point (0, 0) is erased! Now, we have 4 points. The Point (2, 2) is created! Now, we have 5 points. (2, 2) is in the circle at (0, 0). The Point (11, 11) is created! Now, we have 6 points. (11, 11) is not in the circle at (0, 0). The Point (10, 0) is created! Now, we have 7 points. (10, 0) is not in the circle at (0, 0). A Point (0, 0) is erased! Now, we have 6 points. A circle at (0, 0) and radius 10 is erased! Now, we have 1 circles. A Point (0, 0) is erased! Now, we have 5 points. The Point (1, 1) is created! Now, we have 6 points. A Point (1, 1) is copied! Now, we have 7 points. A Point (1, 1) is copied! Now, we have 8 points. A circle at (1, 1) and radius 20 is created! Now, we have 2 circles. A Point (1, 1) is erased! Now, we have 7 points. The Point (2, 2) is created! Now, we have 8 points. (2, 2) is in the circle at (1, 1). The Point (1, 1) is created! Now, we have 9 points. (1, 1) is in the circle at (1, 1). The Point (100, 100) is created! Now, we have 10 points. (100, 100) is not in the circle at (1, 1). A Point (1, 1) is erased! Now, we have 9 points. A circle at (1, 1) and radius 20 is erased! Now, we have 1 circles. A Point (1, 1) is erased! Now, we have 8 points. We have 8 points, and 1 circles. A circle at (1, 1) and radius 1 is erased! Now, we have 0 circles. A Point (1, 1) is erased! Now, we have 7 points. A Point (0, 0) is erased! Now, we have 6 points.

HINT

Append Code


#include <iomanip>
#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;



//一个类的属性是另一个类的对象——类的组合
//指针对象
class Point{
private:
    int x;
    int y;
    static int numOfPoints;
public:
    Point(int a,int b)
    {
        x=a;
        y=b;
        numOfPoints++;
//        cout<<"The Point ("<<x<<", "<<y<<") is created! Now, we have "<<numOfPoints<<" points."<<endl;
cout<<"The Point ("<<x<<", "<<y<<") is created! Now, we have "<<numOfPoints<<" points."<<endl;
    }
    Point()
    {
        x=1;
        y=1;
        numOfPoints++;
//        cout<<"The Point ("<<x<<", "<<y<<") is created! Now, we have "<<numOfPoints<<" points."<<endl;
cout<<"The Point ("<<x<<", "<<y<<") is created! Now, we have "<<numOfPoints<<" points."<<endl;
    }
    ~Point()
    {
        numOfPoints--;
//         cout<<"The Point ("<<x<<", "<<y<<") is erased! Now, we have "<<numOfPoints<<" points."<<endl;
cout<<"A Point ("<<x<<", "<<y<<") is erased! Now, we have "<<numOfPoints<<" points."<<endl;
    }
    Point(const Point& p)
    {
        this->x=p.x;
        this->y=p.y;
        numOfPoints++;
//        cout<<"The Point ("<<x<<", "<<y<<") is copied! Now, we have "<<numOfPoints<<" points."<<endl;
cout<<"A Point ("<<x<<", "<<y<<") is copied! Now, we have "<<numOfPoints<<" points."<<endl;
    }
    static int getNumOfPoints()
    {
        return numOfPoints;
    }
    int getX()
    {
        return x;
    }
    int getY()
    {
        return y;
    }


};

class Circle{
private:
    Point center;
    int radius;
    static int numOfCircles;
public:
    static int getNumOfCircles()
    {
        return numOfCircles;
    }
    Circle(int a,int b,int c):center(a,b),radius(c)
    {
        numOfCircles++;
//        cout<<"A circle at ("<<center.getX()<<", "<<center.getY()<<") and radius "<<radius<<" is created! Now, we have "<<numOfCircles<<" circles."<<endl;
  cout<<"A circle at ("<<center.getX()<<", "<<center.getY()<<") and radius "<<radius<<" is created! Now, we have "<<numOfCircles<<" circles."<<endl;

    }
    Circle(Point p,int r):center(p),radius(r)//不要用center(p.getX(),p.getY()),radius(r) 这个会少调用一次copied 导致输出错误
    {
         numOfCircles++;
//        cout<<"A circle at ("<<center.getX()<<", "<<center.getY()<<") and radius "<<radius<<" is created! Now, we have "<<numOfCircles<<" circles."<<endl;
cout<<"A circle at ("<<center.getX()<<", "<<center.getY()<<") and radius "<<radius<<" is created! Now, we have "<<numOfCircles<<" circles."<<endl;
    }
    ~Circle()
    {
        numOfCircles--;
//        cout<<"A circle at ("<<center.getX()<<", "<<center.getY()<<") and radius "<<radius<<" is erased! Now, we have "<<numOfCircles<<" circles."<<endl;
   cout<<"A circle at ("<<center.getX()<<", "<<center.getY()<<") and radius "<<radius<<" is erased! Now, we have "<<numOfCircles<<" circles."<<endl;
    }
    Circle(const Circle&c)//必须有这个拷贝构造不然会wa
    {
        this->center=c.center;
        this->radius=c.radius;
         numOfCircles++;
    }
    Point& getCenter()
    {
        return center;
    }
//    bool pointInCircle(Point& p)
//    {
//        if((p.getX()*p.getX()+p.getY()*p.getY())<=radius*radius)//数学公式错了 这个公式是判断点到原点的距离
//            return true;
//        else
//            return false;
//    }
    int pointInCircle(Point &p)
    {
        double d;
        d=sqrt(((p.getX()-center.getX())*(p.getX()-center.getX()))+((p.getY()-center.getY())*(p.getY()-center.getY())));
        if(d<radius)
            return 1;
        else
            return 0;
    }

};
int Point::numOfPoints=0;
int Circle::numOfCircles=0;




int main()
{
    int cases,num;
    int x, y, r, px, py;
    Point aPoint(0,0), *bPoint;//双参数构造
    Circle aCircle(1,1,1);//三参数构造
    cin>>cases;
    cout<<"We have "<<Point::getNumOfPoints()<<" points and "<<Circle::getNumOfCircles()<<" circles now."<<endl;
    for (int i = 0; i < cases; i++)
    {
        cin>>x>>y>>r;
        bPoint = new Point(x,y);
        Circle circle(*bPoint, r);//这里传的是一个Point对象而不是引用 所以circle会先把这个对象拷贝进自己的point里——此处明确要用point的拷贝构造
        cin>>num;
        for (int j = 0; j < num; j++)
        {
            cin>>px>>py;
            if (circle.pointInCircle(*(new Point(px, py))))
            {
                cout<<"("<<px<<", "<<py<<") is in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
            else
            {
                cout<<"("<<px<<", "<<py<<") is not in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;//调用circle.getCenter()得到一个Point对象 再调用这个point对象里的成员函数getX
            }
        }
        delete bPoint;
    }
    cout<<"We have "<<Point::getNumOfPoints()<<" points, and "<<Circle::getNumOfCircles()<<" circles."<<endl;//为什么是这么调用?因为是静态函数吗
    return 0;
}

Problem F: 质心算法

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 7235  Solved: 3957
[Submit][Status]

Description

在很多应用中,需要对某个目标进行定位。比如对于一个未知坐标的点A,假定已知A点与N个点相邻,且已知N个相邻点的坐标,则可取N个点的质心作为A点坐标的一个估计值。

所谓质心,就是指其横坐标、纵坐标分别为N个点的横坐标平均值、纵坐标平均值的点。即:假定N个点的坐标分别(x1,y1),(x2,y2),......,则质心的坐标为((x1+x2+...)/N, (y1+y2+...)/N)。

现在需要你编写2个类:

  1. Point类:包括一个点的横坐标和纵坐标,并提供适当的构造函数、析构函数和拷贝构造函数,以及getX()和getY()方法。

  2. Graph类

(1)数据成员Point *points;表示与A点相邻的点的集合。

(2)数据成员:int numOfPoints;表示相邻点的个数。

(3)适当的构造函数、析构函数。

(4)Point getCentroid()方法:用于返回质心点。

注意:同一类的对象之间的赋值运算(=)不调用拷贝构造函数。

Input

输入为多行,第一行M>0表示有M个测试用例。

每个测试用例包含多行。第一行N>0表示有N个点,之后是N个点的横坐标和纵坐标,每个点占一行。

Output

见样例。

Sample Input

1 5 0 0 1 1 2 2 3 3 4 4

Sample Output

The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (1.00, 1.00) is created! The Point (2.00, 2.00) is created! The Point (3.00, 3.00) is created! The Point (4.00, 4.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! A graph with 5 points is created! The Point (2.00, 2.00) is created! A Point (2.00, 2.00) is copied! A Point (2.00, 2.00) is erased! The centroid is (2.00, 2.00). A Point (4.00, 4.00) is erased! A Point (3.00, 3.00) is erased! A Point (2.00, 2.00) is erased! A Point (1.00, 1.00) is erased! A Point (0.00, 0.00) is erased! A graph with 5 points is erased! A Point (4.00, 4.00) is erased! A Point (3.00, 3.00) is erased! A Point (2.00, 2.00) is erased! A Point (1.00, 1.00) is erased! A Point (0.00, 0.00) is erased! A Point (2.00, 2.00) is erased!

HINT

 当使用对象作为函数返回值时,会产生一个临时对象,此时会调用拷贝构造函数。但是在g++编译器(也就是大家常用的code::blocks所用的编译器)中,当函数返回的对象给另一个对象进行赋值时,如果函数返回值是一个局部变量,则不会调用拷贝构造函数。所以,如果想在此程序中实现拷贝构造函数的调用,必须在getCentroid中返回一个使用new运算符创建的对象,而不是一个已经定义的局部对象。

Append Code


#include <iomanip>
#include <iostream>
#include <cstring>
#include <cmath>



using namespace std;

class Point{
private:
    double x,y;
public:
    Point()
    {
         x=0;
         y=0;
         cout<<"The Point ("<<fixed<<setprecision(2)<<x<<", "<<fixed<<setprecision(2)<<y<<") is created!"<<endl;
    }
    Point(double a,double b)
    {
        x=a;
        y=b;
        cout<<"The Point ("<<fixed<<setprecision(2)<<x<<", "<<fixed<<setprecision(2)<<y<<") is created!"<<endl;
    }
    ~Point()
    {
        cout<<"A Point ("<<fixed<<setprecision(2)<<x<<", "<<fixed<<setprecision(2)<<y<<") is erased!"<<endl;
    }
    Point(const Point&p)
    {
        this->x=p.x;
        this->y=p.y;
        cout<<"A Point ("<<fixed<<setprecision(2)<<x<<", "<<fixed<<setprecision(2)<<y<<") is copied!"<<endl;
    }
    double getX()
    {
         return x;
    }
    double getY()
    {
         return y;
    }
};

class Graph{
private:
    Point* points;
    int numOfPoints;
public:
    Graph(Point* intpoints,int c)
    {
        numOfPoints=c;

points=new Point[numOfPoints];
        for(int i=0;i<numOfPoints;i++)
        {
            points[i]=intpoints[i];
        }
        cout<<"A graph with "<<numOfPoints<<" points is created!"<<endl;
    }
    ~Graph()
    {
         delete[] points;
        cout<<"A graph with "<<numOfPoints<<" points is erased!"<<endl;
    }
    Point getCentroid()
    {
        double a=0.0,b=0.0;
         for(int i=0;i<numOfPoints;i++)
         {
                a+=points[i].getX();
                b+=points[i].getY();
         }
         a/=numOfPoints;
         b/=numOfPoints;


        Point* p=new Point(a,b);
        return *p;

    }
};
int main()
{
    int cases,num;
    double x, y;
    Point centroid;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>num;
        Point points[num];
        for (int j = 0; j < num; j++)
        {
            cin>>x>>y;
            points[j] = *(new Point(x, y));
        }
        Graph graph(points, num);
        centroid = graph.getCentroid();
        cout<<setprecision(2)<<fixed<<"The centroid is ("<<centroid.getX()<<", "<<centroid.getY()<<")."<<endl;
    }
    return 0;
}

解析其中某一句
拷贝构造与赋值
为什么说会变成野指针
此题的构造析构顺序问题


网站公告

今日签到

点亮在社区的每一天
去签到