时间模拟

匿名 (未验证) 提交于 2019-12-02 23:36:01

题目内容:

给出下面的基类Time的框架如下:
class Time
{
protected:
int second;
int minute;
int hour;
public:
void operator++();

}
建立一个派生类Time_12hours,用于表示十二进制时间,增加以下成员数据:
string type;//标识为12进制时间,type=”12-hours-time”
string interval;//标识为AM或者PM,interval=”AM”或interval=”PM”
增加以下成员函数: void operator++();

建立一个派生类Time_24hours,用于表示二十四进制时间,增加以下成员数据:
string type;//标识为24进制时间,type=”24-hours-time”
增加以下成员函数:
void operator++();

生成上述类并编写主函数,根据输入的初始时间信息、自增或者自减类型、自增或者自减次数,输出其最后时间信息。

输入格式:

测试输入包含多个测试用例,一个测试用例为一行,每行共五个数字,第一个数字为进制,121表示输入为12进制AM时间,122表示输入为12进制PM时间,输入为24表示输入为24进制时间,第二个数字为hour,第三个数字为minute,第四个数字为second,第五个字符为运算类型,+表示自增,-表示自减,第六个数字为运算次数,0表示测试用例结束。

输入样例:

121 11 59 59 + 3
24 11 59 59 + 3
122 11 59 59 + 3
122 00 00 00 - 3
121 00 00 00 - 5
24 00 00 00 - 1
0

输出样例:

PM 00:00:02
12:00:02
AM 00:00:02
AM 11:59:57
PM 11:59:55
23:59:59

Code:

#include<bits/stdc++.h> using namespace std; class Time { protected: 	int second; 	int minute; 	int hour; public: 	void deal() 	{ 		if (second < 0)minute--, second += 60; 		else if (second >= 60)second -= 60, minute++; 		if (minute < 0)hour--, minute += 60; 		else if (minute >= 60)minute -= 60, hour++; 		if (hour >= 24)hour -= 24; 		else if (hour < 0)hour += 24; 	} 	void operator++() 	{ 		second++; 		deal(); 	} 	void operator--() 	{ 		second--; 		deal(); 	} }; class Time_12hours : public Time { private: 	string type; 	string interval; public: 	Time_12hours(string Interval = "AM") :type("12-hours-time"), interval(Interval) {} 	void deal() 	{ 		Time::deal(); 		if (hour < 12)interval = "AM"; 		else interval = "PM"; 	} 	void operator++() 	{ 		second++; 		deal(); 	} 	void operator--() 	{ 		second--; 		deal(); 	} 	friend istream& operator >> (istream& in, Time_12hours& t) 	{ 		in >> t.hour >> t.minute >> t.second; 		if (t.interval == "PM")t.hour += 12; 		t.deal(); 		return in; 	} 	friend ostream& operator << (ostream& out, const  Time_12hours& t) 	{ 		out << t.interval << " " 			<< setw(2) << setfill('0') << t.hour % 12 << ":"  			<< setw(2) << setfill('0') << t.minute << ":"  			<< setw(2) << setfill('0') << t.second; 		return out; 	} }; class Time_24hours : public Time { private: 	string type; public: 	Time_24hours() :type("24-hours-time") {} 	void operator++() 	{ 		second++; 		deal(); 	} 	void operator--() 	{ 		second--; 		deal(); 	} 	friend istream& operator >> (istream& in, Time_24hours & t) 	{ 		in >> t.hour >> t.minute >> t.second; 		return in; 	} 	friend ostream& operator << (ostream& out, const Time_24hours & t) 	{ 		out << setw(2) << setfill('0') << t.hour << ":"  			<< setw(2) << setfill('0') << t.minute << ":"  			<< setw(2) << setfill('0') << t.second; 		return out; 	} }; int main() { 	//freopen("test.in", "r", stdin); 	//freopen("test.out", "w", stdout); 	char ch; 	int type, hour, minute, second, times; 	while (cin >> type && type) 	{ 		switch (type) 		{ 			case 24: 			{ 				Time_24hours p; 				cin >> p >> ch >> times; 				for (int i = 0; i < times; i++) 					if (ch == '+') ++p; 					else --p; 				cout << p << endl; 				break; 			} 			case 121: 			{ 				Time_12hours p("AM"); 				cin >> p >> ch >> times; 				for (int i = 0; i < times; i++) 					if (ch == '+')++p; 					else --p; 				cout << p << endl; 				break; 			} 			case 122: 			{ 				Time_12hours p("PM"); 				cin >> p >> ch >> times; 				for (int i = 0; i < times; i++) 					if (ch == '+')++p; 					else --p; 				cout << p << endl; 				break; 			} 		} 	} 	return 0; } 
文章来源: https://blog.csdn.net/qq_43908569/article/details/90696290
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!