Problem C: 從點到面
阿新 • • 發佈:2018-05-27
limit hint 出現 pac 屬性 code get 構造函數、析構函數 拷貝構造函數
Submit: 3317 Solved: 1854
[Submit][Status][Web Board]
Problem C: 從點到面
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3317 Solved: 1854
[Submit][Status][Web Board]
Description
一個矩形可以由左上角和右下角的頂點而唯一確定。現在請定義兩個類:Point和Rectangle。
其中Point類有x和y兩個屬性(均為int類型),表示二維空間內一個點的橫縱坐標,並具有相應的構造函數、析構函數和拷貝構造函數。此外,還有getX()和getY()方法用以得到一個點的坐標值。
Rectangle類有leftTop和rightBottom兩個屬性(均為Point類的對象),表示一個矩形的左上角和右下角的兩個點,並具有相應的構造函數、析構函數。此外,還有getLeftTop()、getRightBottom()方法用於獲取相應的左上角點、右下角點,getArea()方法用以獲取面積。
Input
輸入有多行。
第一行是一個正整數M,表示後面有M個測試用例。
每個測試用例占一行,包括4個正整數,分別為左上角的橫坐標、縱坐標,右下角的橫坐標、縱坐標。
註意:
1.請根據輸出樣例判斷兩個類中相應方法的書寫方法。
2. 假定屏幕的左下角為坐標原點。
Output
輸出見樣例。
Sample Input
1 10 10 20 0
Sample Output
A point (10, 10) is created! A point (20, 0) is created! A rectangle (10, 10) to (20, 0) is created! Area: 100 Left top is (10, 10) A point (20, 0) is copied! A point (20, 0) is copied! Right bottom is (20, 0) A point (20, 0) is erased! A point (20, 0) is erased! A rectangle (10, 10) to (20, 0) is erased! A point (20, 0) is erased! A point (10, 10) is erased!
HINT
Append Code
append.cc,int main() { int cases; int x1, y1, x2, y2; cin>>cases; for (int i = 0; i < cases; i++) { cin>>x1>>y1>>x2>>y2; Rectangle rect(x1,y1,x2,y2); cout<<"Area: "<<rect.getArea()<<endl; cout<<"Left top is ("<<rect.getLeftTop().getX()<<", "<<rect.getLeftTop().getY()<<")"<<endl; cout<<"Right bottom is ("<<rect.getRightBottome().getX()<<", "<<rect.getRightBottome().getY()<<")"<<endl; } return 0; }
#include <iostream> using namespace std; class Point { public : double x_, y_; Point(double x=0):x_(x), y_(x) { cout<<"A point ("<<x_<<", "<<y_<<") is created!"<<endl; } Point(double x, double y):x_(x), y_(y) { cout<<"A point ("<<x_<<", "<<y_<<") is created!"<<endl; } Point(const Point &p) { x_=p.x_; y_=p.y_; cout<<"A point ("<<x_<<", "<<y_<<") is copied!"<<endl; } double getX(){return x_;} double getY(){return y_;} ~Point() { cout<<"A point ("<<x_<<", "<<y_<<") is erased!"<<endl; } }; class Rectangle { public : Point leftTop, rightBottom; double x1, y1, x2, y2; Rectangle(double a, double b, double c, double d):leftTop(a,b), rightBottom(c,d) { x1=a; y1=b; x2=c; y2=d; cout<<"A rectangle ("<<a<<", "<<b<<") to ("<<c<<", "<<d<<") is created!"<<endl; } Point &getLeftTop() { return leftTop; } Point getRightBottome()//本題純屬胡鬧,為了一個出現copy一個不出現copy一個加&一個不加&。 { return rightBottom; } double getArea() { return(x1-x2)*(y2-y1); } ~Rectangle() { cout<<"A rectangle ("<<x1<<", "<<y1<<") to ("<<x2<<", "<<y2<<") is erased!"<<endl; } }; int main() { int cases; int x1, y1, x2, y2; cin>>cases; for (int i = 0; i < cases; i++) { cin>>x1>>y1>>x2>>y2; Rectangle rect(x1,y1,x2,y2); cout<<"Area: "<<rect.getArea()<<endl; cout<<"Left top is ("<<rect.getLeftTop().getX()<<", "<<rect.getLeftTop().getY()<<")"<<endl; cout<<"Right bottom is ("<<rect.getRightBottome().getX()<<", "<<rect.getRightBottome().getY()<<")"<<endl; } return 0; }
Problem C: 從點到面