Problem F: 質心算法


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<iostream>
#include<iomanip>
using namespace std;
class Point
{
private:
    double x,y;
public:
    Point(double a=0,double b=0):x(a),y(b){cout<<setprecision(2)<<fixed<<"The Point ("<<x<<", "<<y<<") is created!\n";}
    ~Point(){cout<<setprecision(2)<<fixed<<"A Point ("<<x<<", "<<y<<") is erased!\n";}
    Point(const Point &q):x(q.x),y(q.y){cout<<setprecision(2)<<fixed<<"A Point ("<<x<<", "<<y<<") is copied!\n";}
    double getX(){return x;}
    double getY(){return y;}
    double setX(double a){x=a;}
    double setY(double b){y=b;}
};
class Graph
{
private:
    Point *points;
    int numOfPoints;
public:
    Graph(Point *p,int n):numOfPoints(n){points=new Point[numOfPoints];for(int i=0;i<n;i++)
        {
            points[i] = p[i];
        }  cout<<"A graph with "<<numOfPoints<<" points is created!\n";}
    ~Graph(){delete []points;cout<<"A graph with "<<numOfPoints<<" points is erased!\n";}
        Point getCentroid(){
        double sum1=0;
        for(int i=0;i<numOfPoints;i++)
            sum1+=points[i].getX();
        double sum2=0;
        for(int i=0;i<numOfPoints;i++)
            sum2+=points[i].getY();
        Point *p = new Point(sum1/numOfPoints,sum2/numOfPoints);
        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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM