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行整数数据