• C++ 编写时间类Time


    【问题描述】

    编写一个程序,定义一个时间类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
    【样例说明】

    • 不要显示多余的提示信息,避免输出判定错误。
    • 输出结束后不要输出任何内容,包括空格和换行。
    • 注意判断输出信息是否符合要求。

     【完整代码如下】

    1. #include
    2. #include
    3. using namespace std;
    4. class Time
    5. {
    6. private:
    7. int hour;
    8. int minute;
    9. int second;
    10. public:
    11. Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s){};
    12. Time(const Time &other) : hour(other.hour), minute(other.minute), second(other.second){};
    13. friend istream &operator>>(istream &input, Time &other)
    14. {
    15. input >> other.hour >> other.minute >> other.second;
    16. return input;
    17. }
    18. friend ostream &operator<<(ostream &output, const Time &other)
    19. {
    20. output << setw(2) << setfill('0') << other.hour << ":" << setw(2) << setfill('0') << other.minute << ":" << setw(2) << setfill('0') << other.second << endl;
    21. return output;
    22. }
    23. const Time operator++(int)
    24. {
    25. Time tmp(*this);
    26. int x = hour * 3600 + minute * 60 + second;
    27. x++;
    28. hour = x / 3600;
    29. x %= 3600;
    30. minute = x / 60;
    31. second = x % 60;
    32. return tmp;
    33. }
    34. const Time operator--(int)
    35. {
    36. Time tmp(*this);
    37. int x = hour * 3600 + minute * 60 + second;
    38. x--;
    39. hour = x / 3600;
    40. x %= 3600;
    41. minute = x / 60;
    42. second = x % 60;
    43. return tmp;
    44. }
    45. Time operator+=(const Time &other)
    46. {
    47. second += other.second;
    48. minute += other.minute;
    49. hour += other.hour;
    50. if (second >= 60)
    51. {
    52. minute += second / 60;
    53. second %= 60;
    54. }
    55. if (minute >= 60)
    56. {
    57. hour += minute / 60;
    58. minute %= 60;
    59. }
    60. if (hour >= 24)
    61. {
    62. hour %= 24;
    63. }
    64. return *this;
    65. }
    66. Time operator-=(const Time &other)
    67. {
    68. if(second
    69. {
    70. second += 60;
    71. second -= other.second;
    72. minute--;
    73. }
    74. else
    75. {
    76. second -= other.second;
    77. }
    78. if(minute
    79. {
    80. minute += 60;
    81. minute -= other.minute;
    82. hour--;
    83. }
    84. else
    85. {
    86. minute -= other.minute;
    87. }
    88. if(hour
    89. {
    90. hour += 24;
    91. hour -= other.hour;
    92. }
    93. else
    94. {
    95. hour -= other.hour;
    96. }
    97. return *this;
    98. }
    99. Time& operator++()
    100. {
    101. int x = hour * 3600 + minute * 60 + second;
    102. x++;
    103. hour = x / 3600;
    104. x %= 3600;
    105. minute = x / 60;
    106. second = x % 60;
    107. return *this;
    108. }
    109. Time& operator--()
    110. {
    111. int x = hour * 3600 + minute * 60 + second;
    112. x--;
    113. hour = x / 3600;
    114. x %= 3600;
    115. minute = x / 60;
    116. second = x % 60;
    117. return *this;
    118. }
    119. };
    120. int main()
    121. {
    122. Time t1, t2;
    123. cin >> t1;
    124. cin >> t2;
    125. cout << (t1 += (t2++));
    126. cout << (t1 -= t2);
    127. cout << ++t2;
    128. cout << (t2 += (t1--));
    129. cout << --t1;
    130. cout << (t2-=t1);
    131. }

  • 相关阅读:
    个人记录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