A. 时钟调整(运算符前后增量)
假定一个时钟包含时、分、秒三个属性,取值范围分别为0~11,0~59,0~59,具体要求如下:
1、用一元运算符++,并且是前增量的方法,实现时钟的调快操作。例如要把时钟调快5秒,则执行5次” ++<对象> “ 的操作
2、用一元运算符--,并且是后增量的方法,实现时钟的调慢操作。例如要把时钟调慢10秒,则执行10次” <对象>-- “的操作
3、用构造函数的方法实现时钟对象的初始化,用输出函数实现时钟信息的输出
clock和time是系统内部函数,所以不要用来做类名或者其他
输入
第一行输入时钟的当前时间时、分、秒
第二行输入t表示有t个示例
第三行输入t个整数x,如果x为正整数,则表示执行调快操作,使用重载运算符++;如果x为负整数,则表示执行调慢操作,使用重载运算符--
每次的调快或调慢操作都是承接上一次调整后的结果进行,例如先调快10秒,再调慢2秒,那么调慢2秒是接着调快10秒后的结果进行的
输出
每行输出每个时钟调整操作后的时分秒
输入样例1
11 58 46
4
5 70 -22 -55
输出样例111:58:51
0:0:1
11:59:39
11:58:44
#include <iostream> using namespace std; class shizhong { private: int hour, minute, second; public: shizhong(int h, int m, int s) :hour(h), minute(m), second(s) { } friend shizhong operator++(shizhong& s); friend shizhong operator--(shizhong& s,int); void print() { cout << hour << ":" << minute << ":" << second << endl; } }; shizhong operator++(shizhong& s) { s.second++; if (s.second == 60) { s.second = 0; s.minute++; if (s.minute == 60) { s.minute = 0; s.hour++; if (s.hour == 12) { s.hour = 0; } } } return s; } shizhong operator--(shizhong&a,int) { shizhong temp = a; if (a.second == 0) { a.second = 59; a.minute--; if (a.minute == -1) { a.minute = 59; a.hour--; if (a.hour == -1) { a.hour = 11; } } } else { a.second--; } return temp; } int main() { int hour, minute, second, x; int t; cin >> hour >> minute >> second; cin >> t; shizhong s(hour, minute, second); while (t--) { while (cin >> x) { if (x > 0) { for (int i = 0; i < x; i++) { ++s; } } if(x<0) { for (int i = 0; i > x; i--) { s--; } } s.print(); } } }
B. 矩阵(运算符重载)
下面主函数和CMatrix类完成矩阵的输入、输出。请补齐CMatrix类中需要的成员函数。
注意:主函数和CMatrix类已给代码不可修改,只可在CMatrix类中添加新的成员函数,并补充完整未实现的成员函数。修改已知代码不计分。
提示:根据已给代码分析CMatrix需要重载哪些运算符?
输入
测试次数
每组测试数据格式如下:
正整数n、m,分表表示矩阵的行、列
n*m行整数数据
输出
每组测试数据输出矩阵数据两次,具体输出格式见样例。
输入样例1
2
3 3
1 2 3
4 5 6
7 8 9
2 4
10 20 30 40
50 60 70 80
输出样例1matrixA:
1 2 3
4 5 6
7 8 9
matrixB:
1 2 3
4 5 6
7 8 9
matrixA:
10 20 30 40
50 60 70 80
matrixB:
10 20 30 40
50 60 70 80
#include <iostream> using namespace std; // 定义矩阵类 class CMatrix { private: int n, m; // n-行,m-列 int** data; // 存储矩阵数据 public: CMatrix() { n = 0; //防止运行时异常 m = 0; data = NULL; } CMatrix(int n1, int m1) { n = n1; m = m1; // 分配n行m列的二维数组空间 data = new int* [n]; for (int i = 0; i < n; i++) { data[i] = new int[m]; } } ~CMatrix() { // 释放空间 for (int i = 0; i < n; i++) { delete[] data[i]; } delete[] data; } CMatrix(const CMatrix& a) { n = a.n; m = a.m; data = new int* [n]; for (int i = 0; i < n; i++) { data[i] = new int[m]; for (int j = 0; j < m; j++) { data[i][j] = a.data[i][j]; } } } CMatrix& operator = (const CMatrix& a) { //应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝。 if (data != NULL) { for (int i = 0; i < n; i++) { delete[] data[i]; } delete[] data; } n = a.n; m = a.m; //二维数组的深拷贝 data = new int* [n]; for (int i = 0; i < n; i++) { data[i] = new int[m]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { data[i][j] = a.data[i][j]; } } return *this; } int* const operator [](const int k)//指针常量是为了输入 { return data[k];//只用一个[],因为有* } int operator () (int i, int j) //输出二维数组 { return data[i][j]; } }; int main() { int t, n, m, i, j; cin >> t; while (t--) { cin >> n >> m; // 定义矩阵对象matrixA CMatrix matrixA(n, m); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { // 输入第i行第j列的数据 cin >> matrixA[i][j];//这里用到了[]重载 } } // 输出matrixA中的数据 cout << "matrixA:" << endl; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cout << matrixA(i, j) << " ";//()重载 } cout << endl; } // 定义矩阵对象matrixB CMatrix matrixB; matrixB = matrixA; // 输出marixB中的数据 cout << "matrixB:" << endl; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cout << matrixB[i][j] << " "; } cout << endl; } } return 0; }
D. 货币加减(运算符重载)
题目描述
定义CMoney类,包含元、角、分三个数据成员,友元函数重载‘+’、'-',实现货币的加减运算(假设a-b中a的金额始终大于等于b的金额),
重载输入及输出,实现货币的输入,输出。
读入最初的货币值,对其不断进行加、减操作,输出结果。
可根据需要,为CMoney类添加构造函数或其它成员函数。
输入
测试次数
每组测试数据格式如下:
第一行,初始货币:元 角 分
第二行开始,每行一个操作:add 元 角 分(加)、minus 元 角 分(减)、stop(结束)
输出
对每组测试数据,输出操作终止后的货币金额,具体输出格式见样例。
输入样例1
2
0 0 0
add 48 9 0
minus 0 5 3
add 18 6 8
add 12 1 2
stop
10 2 5
add 5 8 0
add 32 1 2
minus 10 5 9
minus 37 5 8
stop
输出样例179元1角7分
0元0角0分
#include<iostream> using namespace std; class Cmoney { private: int yuan, fen, jiao; public: friend Cmoney operator+(Cmoney&, Cmoney&); friend Cmoney operator-(Cmoney&, Cmoney&); friend istream& operator >> (istream& cin, Cmoney& a); friend ostream& operator << (ostream& cout, Cmoney& c); void print() { cout << yuan << "元" << jiao << "角" << fen << "分"; } Cmoney() { yuan = 0, jiao = 0, fen = 0; } Cmoney(int y, int j, int f) :yuan(y), jiao(j), fen(f) { } }; Cmoney operator+(Cmoney& c1, Cmoney& c2) { //由于分不能超过10 角不能超过10 统一换算成分 Cmoney temp; int c1fen = c1.yuan * 100 + c1.jiao * 10 + c1.fen; int c2fen = c2.yuan * 100 + c2.jiao * 10 + c2.fen; int sum = c1fen + c2fen; temp.yuan = sum / 100 ; temp.jiao = (sum % 100) / 10; temp.fen = (sum % 100) % 10; return temp; } Cmoney operator-(Cmoney& c1, Cmoney& c2) { Cmoney temp; int c1fen = c1.yuan * 100 + c1.jiao * 10 + c1.fen; int c2fen = c2.yuan * 100 + c2.jiao * 10 + c2.fen; int sum = c1fen - c2fen; //假设c1-c2中的金额始终大于0 temp.yuan = sum / 100 ; temp.jiao = (sum % 100) / 10; temp.fen = (sum % 100) % 10; return temp; } //全局函数 左移 右移运算符 istream& operator >> (istream& cin, Cmoney& a)//istream& 自己命名一个cin { cin >> a.yuan >> a.jiao >> a.fen; return cin; //一定要返回自己命名的 } ostream& operator << (ostream& cout, Cmoney& c) { cout << c.yuan << "元" << c.jiao << "角" << c.fen << "分" << endl; return cout; } int main() { int t; cin >> t; while (t--) { int yuan, jiao, fen; cin >> yuan >> jiao >> fen; Cmoney c1(yuan, jiao, fen); string caozuo; cin >> caozuo; Cmoney c3; while (caozuo != "stop") { if (caozuo == "add") { cin >> yuan >> jiao >> fen; Cmoney c2(yuan, jiao, fen); c3 = c1 + c2; } if (caozuo == "minus") { cin >> yuan >> jiao >> fen; Cmoney c2(yuan, jiao, fen); c3 = c1 - c2; } c1 = c3; //重置c1为上一个操作后的c3 cin >> caozuo;//继续输入操作 直到输入stop退出循环 } cout << c3; //运用了重载运算 << } }
C. 矩形关系(运算符重载)
题目描述
假设坐标采用二维平面坐标。
定义点类CPoint,包含属性x,y(整型)。方法有:带参构造函数,getX,getY分别返回点的x坐标,y坐标。
定义矩形类CRectangle,包含属性:矩形的左上角坐标leftPoint,右下角坐标rightPoint。类中方法有:
1)带参构造函数,初始化矩形的左上角、右下角
2)重载>运算符,参数为CPoint点对象,假设为p,若p在矩形内,返回true,否则返回false。
3)重载>运算符,第一个矩形若包含第二个矩形(部分边界可以相等),返回true,否则返回false。(要求该函数调用2)实现)
4)重载==运算符,判断两个矩形是否一致,返回true或false。
5)重载*运算符,判断两个矩形是否有重叠部分,返回true或false。
6)重载类型转换运算符,计算矩形的面积并返回,面积是整型。
7)重载<<运算符,输出矩形的两个角坐标,具体格式见样例。
输入2个矩形,计算面积,判断矩形的关系。主函数如下,不可修改。
输入样例1
2
1 4 4 1
2 3 3 2
1 4 4 1
0 3 5 2
输出样例1矩形1:1 4 4 1 9
矩形2:2 3 3 2 1
矩形1包含矩形2矩形1:1 4 4 1 9
矩形2:0 3 5 2 5
矩形1和矩形2相交
#include<iostream> using namespace std; class CPoint { private: int x, y; public: CPoint(int xx, int yy) { x = xx; y = yy; } int getX() { return x; } int getY() { return y; } }; class CRectangle { private: CPoint leftPoint, rightPoint; //左上角坐标 右下角坐标 public: CRectangle(int x1, int y1, int x2, int y2) :leftPoint(x1, y1), rightPoint(x2, y2) { } //重载>运算符,参数为CPoint点对象,假设为p,若p在矩形内,返回true,否则返回false bool operator>(CPoint& p) { if (p.getX() > leftPoint.getX() && p.getX() < rightPoint.getX() && p.getY() < leftPoint.getY() && p.getY() > rightPoint.getY()) { return true; } else { return false; } } //重载>运算符,第一个矩形若包含第二个矩形(部分边界可以相等),返回true,否则返回false bool operator>(CRectangle& cr) //传入的cr是第二个矩形 两个坐标点都要符合在矩形上(包含边界) { if (cr.leftPoint.getX() >= leftPoint.getX() && cr.leftPoint.getX() <= rightPoint.getX() && cr.leftPoint.getY() <= leftPoint.getY() && cr.leftPoint.getY() >= rightPoint.getY() && cr.rightPoint.getX() >= leftPoint.getX() && cr.rightPoint.getX() <= rightPoint.getX() && cr.rightPoint.getY() <= leftPoint.getY() && cr.rightPoint.getY() >= rightPoint.getY() ) { return true; } else { return false; } } //重载==运算符,判断两个矩形是否一致 bool operator==(CRectangle& cr) //两个坐标点一致 { if (cr.leftPoint.getX() == leftPoint.getX()&& cr.leftPoint.getY() == leftPoint.getY() && cr.rightPoint.getX() == rightPoint.getX() && cr.rightPoint.getY() == rightPoint.getY()) { return true; } else { return false; } } //重载*运算符,判断两个矩形是否有重叠部分,返回true或false。 bool operator*(CRectangle& cr) { //不重叠的情况 if (cr.leftPoint.getX() > rightPoint.getX() || cr.leftPoint.getY() < rightPoint.getY() || cr.rightPoint.getX() < leftPoint.getX() || cr.rightPoint.getY() > leftPoint.getY()) { return false; } else { return true; } } //重载类型转换运算符,计算矩形的面积并返回,面积是整型。 operator int() { int area; area = (rightPoint.getX() - leftPoint.getX()) * (leftPoint.getY() - rightPoint.getY()); return area; } friend ostream& operator<<(ostream& stream, CRectangle& cr); }; //全局函数 重载<<运算符,输出矩形的两个角坐标 ostream& operator<<(ostream& stream, CRectangle& cr) { stream << cr.leftPoint.getX() << " " << cr.leftPoint.getY() << " " << cr.rightPoint.getX() << " " << cr.rightPoint.getY(); return stream; } int main() { int t, x1, x2, y1, y2; cin >> t; while (t--) { // 矩形1的左上角、右下角 cin >> x1 >> y1 >> x2 >> y2; CRectangle rect1(x1, y1, x2, y2); // 矩形2的左上角、右下角 cin >> x1 >> y1 >> x2 >> y2; CRectangle rect2(x1, y1, x2, y2); // 输出矩形1的坐标及面积 //用了<<重载 和 int()重载 cout << "矩形1:" << rect1 << " " << (int)rect1 << endl; // 输出矩形2的坐标及面积 cout << "矩形2:" << rect2 << " " << (int)rect2 << endl; if (rect1 == rect2) //==重载 { cout << "矩形1和矩形2相等" << endl; } else if (rect2 > rect1) // >重载 { cout << "矩形2包含矩形1" << endl; } else if (rect1 > rect2) // >重载 { cout << "矩形1包含矩形2" << endl; } else if (rect1 * rect2) //重叠 { cout << "矩形1和矩形2相交" << endl; } else { cout << "矩形1和矩形2不相交" << endl; } cout << endl; } return 0; }