declare.h
复制代码
1
2
3
4
5#pragma once class Point; class line; class Rectangle;
Rectangle.h
复制代码
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142#pragma once #include <iostream> #include <algorithm> using namespace std; class Point { double x, y; public: Point(double px = 0, double py = 0) { x = px; y = py; } Point(const Point& p) {//复制构造 x = p.x; y = p.y; } ~Point() {//析构 } void change(double px, double py) {//改变Point的位置 x = px; y = py; } void show_point() {//输出 cout << endl << "x=" << x << endl << "y=" << y << endl; } bool operator ==(const Point &p) {//重载==运算符 if (p.x == x && p.y == y) { return true; } return false; } double get_x() { return x; } double get_y() { return y; } }; class line { double l, r;//左端点 ,右端点 public: line(double nl, double nr) { l = nl; r = nr; } ~line() {//析构函数 } double get_l() { return l; } double get_r() { return r; } static bool cmp(line& l1, line& l2) { return l1.get_l() <= l2.get_l(); } line find_lap(line l2, bool& flag) { line arrL[2] = { line(l,r),l2 }; sort(arrL, arrL + 2, cmp);//这样就完成了一个x坐标从小到大的排列 其实也就是比较经典的区间线段合并 if (flag && arrL[0].get_r() >= arrL[1].get_l()) {//只有当flag为true时候才会进去,防止本来是false的被覆盖为true flag = true; } else { flag = false; } return line(arrL[1].get_l(), arrL[1].get_l() + arrL[0].get_r() - arrL[1].get_l());//返回了重叠的线 } }; class Rectangle { Point p1, p2;//p1左端点,p2右端点,可以极大程度简化code量 public: Rectangle(double x1, double y1, double x2, double y2) {//两种初始化方式 p1.change(x1, y1), p2.change(x2, y2); } Rectangle(Point np1, Point np2) { p1 = np1; p2 = np2; } Rectangle(const Rectangle& r) { p1 = r.p1; p2 = r.p2; } ~Rectangle() {//析构 } double get_s() {//计算面积 return abs(p1.get_x() - p2.get_x()) * abs(p1.get_y() - p2.get_y()); } bool operator == (Rectangle& r) {//判断是否相等 重载==运算符 if (p1 == r.get_p1() && r.get_p2() == p2) { return true; } else { return false; } } void change_size(double p2tox, double p2toy) {//改变右下角坐标 p2.change(p2tox, p2toy); cout << "after moving the Lower right corner" << endl; this->show_point(); } void moveoff(double dx, double dy) {//移动 p1.change(p1.get_x() + dx, p1.get_y() + dy); p2.change(p1.get_x() + dx, p2.get_y() + dy); } void show_point() { cout << "the Upper left corner is"; p1.show_point(); cout << "the Lower right corner is"; p2.show_point(); } Point get_p1() { return p1; } Point get_p2() { return p2; } Rectangle find_overlap(Rectangle& r, bool& flag) { flag = true; line xl1(p1.get_x(), p2.get_x()); line xl2(r.p1.get_x(), r.p2.get_x()); line yl1(p2.get_y(), p1.get_y()); line yl2(r.p2.get_y(), r.p1.get_y()); line ansx = xl1.find_lap(xl2, flag);//x方向重叠区域 line ansy = yl1.find_lap(yl2, flag);//y方向重叠区域 //此时用flag判断是否有一个正确的结果,true为有,反之无 return Rectangle(Point(ansx.get_l(), ansy.get_r()), Point(ansx.get_r(), ansy.get_l())); } //思路来源 sdu新生赛 D题 }; //class 创建完成
main
复制代码
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#include <iostream> using namespace std; #include "declare.h" #include "Rectangle.h" int main() { Rectangle a(1, 2, 3, 1); Rectangle b(-2, -1, -1, -3); Rectangle c(1, 2, 3, 1); bool flag = 0; cout << "the area of a is" << a.get_s() << endl;//计算面积 if (a == b) { cout << "a==b" << endl; } else { cout << "a does not equal b" << endl; } if (a == c) { cout << "a==c" << endl; } else { cout << "a does not equal c" << endl; } a.change_size(9, 4); a.moveoff(1, 3); a.show_point(); Rectangle ans = a.find_overlap(b, flag); if (flag) { ans.show_point(); } else { cout << "They do not have overlap" << endl; } return 0; }
最后
以上就是安详泥猴桃最近收集整理的关于实验E5:设计一个矩形类(类的设计)的全部内容,更多相关实验E5:设计一个矩形类(类内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复