【问题描述】
编写一个程序,定义一个时间类Time,包含三个属性: hour, minute 和 second
要求通过运算符重载实现如下功能:
时间输入输出(>>、<<);
时间增加减少若干(+=、-=),例:Time& operator+=(const Time&);Time& operator-=(const Time&);
时间前、后自增加/减少1秒(++、--),前自增例:Time& operator++(); 后自增例:Time operator++(int);
【输入形式】
输入固定为两个Time实例(time1,time2),每个实例占一行;
Time实例输入格式为:hour minute second。
【输出形式】
Time实例输出格式为:hour:minute:second;
每个输出实例占一行。
依次输出以下表达式的值
time1 += (time2++)
time1 -= time2
++time2
time2 += (time1--)
--time1
time2 -= time1
【样例输入】
21 10 35
10 15 25
【样例输出】
07:26:00
21:10:34
10:15:27
07:26:01
21:10:32
10:15:29
【样例说明】
【完整代码如下】
- #include
- #include
- using namespace std;
- class Time
- {
- private:
- int hour;
- int minute;
- int second;
-
- public:
- Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s){};
- Time(const Time &other) : hour(other.hour), minute(other.minute), second(other.second){};
- friend istream &operator>>(istream &input, Time &other)
- {
- input >> other.hour >> other.minute >> other.second;
- return input;
- }
- friend ostream &operator<<(ostream &output, const Time &other)
- {
- output << setw(2) << setfill('0') << other.hour << ":" << setw(2) << setfill('0') << other.minute << ":" << setw(2) << setfill('0') << other.second << endl;
- return output;
- }
- const Time operator++(int)
- {
- Time tmp(*this);
- int x = hour * 3600 + minute * 60 + second;
- x++;
- hour = x / 3600;
- x %= 3600;
- minute = x / 60;
- second = x % 60;
- return tmp;
- }
- const Time operator--(int)
- {
- Time tmp(*this);
- int x = hour * 3600 + minute * 60 + second;
- x--;
- hour = x / 3600;
- x %= 3600;
- minute = x / 60;
- second = x % 60;
- return tmp;
- }
- Time operator+=(const Time &other)
- {
- second += other.second;
- minute += other.minute;
- hour += other.hour;
- if (second >= 60)
- {
- minute += second / 60;
- second %= 60;
- }
- if (minute >= 60)
- {
- hour += minute / 60;
- minute %= 60;
- }
- if (hour >= 24)
- {
- hour %= 24;
- }
- return *this;
- }
- Time operator-=(const Time &other)
- {
- if(second
- {
- second += 60;
- second -= other.second;
- minute--;
- }
- else
- {
- second -= other.second;
- }
- if(minute
- {
- minute += 60;
- minute -= other.minute;
- hour--;
- }
- else
- {
- minute -= other.minute;
- }
- if(hour
- {
- hour += 24;
- hour -= other.hour;
- }
- else
- {
- hour -= other.hour;
- }
-
- return *this;
- }
- Time& operator++()
- {
- int x = hour * 3600 + minute * 60 + second;
- x++;
- hour = x / 3600;
- x %= 3600;
- minute = x / 60;
- second = x % 60;
- return *this;
- }
- Time& operator--()
- {
- int x = hour * 3600 + minute * 60 + second;
- x--;
- hour = x / 3600;
- x %= 3600;
- minute = x / 60;
- second = x % 60;
- return *this;
- }
- };
-
- int main()
- {
- Time t1, t2;
- cin >> t1;
- cin >> t2;
- cout << (t1 += (t2++));
- cout << (t1 -= t2);
- cout << ++t2;
- cout << (t2 += (t1--));
- cout << --t1;
- cout << (t2-=t1);
- }
-
相关阅读:
个人记录jenkins编译ios过程 xcode是9.4.1
USART串口协议
[JAVEee]SpringBoot项目的创建
Hugging News #0717: 开源大模型榜单更新、音频 Transformers 课程完成发布!
echart柱状图y坐标轴反转问题
java计算机毕业设计高校贫困生信息管理系统源码+mysql数据库+系统+lw文档+部署
线代——求逆矩阵的快捷方法
[CSharpTips]判断两条线段是否相交
c++_learning-基础部分
前端研习录(32)——JavaScript 基于定时器实现防抖、节流
-
原文地址:https://blog.csdn.net/weixin_74287172/article/details/134426201
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU