7-5 时间换算(运算符重载) (20分)

定义一个时间类Time,内有私有数据成员:hour,minute,second,另有成员函数:构造函数用于初始化数据成员,输出函数,运算符重载+(加号),。编写主函数:创建时间对象,再输入秒数 n,通过运算符重载+(加号),计算该时间再过 n 秒后的时间值,时间的表示形式为:时:分:秒,超过 24 时从 0 时重新开始计时。 测试输入包含若干测试用例,每个测试用例占一行。当读入0 0 0 0时输入结束,相应的结果不要输出。若输入的时、分、秒数据不合法,则输出:Time Error! n为非负整数,否则输出:Input n Error!

输入格式:

多行输入,每行以:小时 分 秒 n 的格式输入,读入0 0 0 0时输入结束 例如: 11 59 40 30 (表示时间为11:59:40,秒数n=30)

输出格式:

时间正确,则以“Time:时:分钟:秒”的格式输出时间 否则输出错误提示信息:Time Error! 或 Input n Error!

输入样例:

在这里给出一组输入。例如:

11 59 40 30
0 0 1 59
23 59 40 3011
24 23 40 34
20 69 45 45
10 23 100 34
10 23 34 -23
0 0 0 0

输出样例:

在这里给出相应的输出。例如:

Time:12:0:10
Time:0:1:0
Time:0:49:51
Time Error!
Time Error!
Time Error!
Input n Error!

代码

#include<iostream>
using namespace std;
class time
{
private:
    int hour;
    int minute;
    int second;
public:
    time(int hour,int minute,int second)
    {
        this->hour=hour;
        this->minute=minute;
        this->second=second;
    }
    time operator+(int second)
    {
        int a;
        a=(this->hour*60*60+this->minute*60+this->second+second)%(24*60*60);
        this->hour=a/3600;
        this->minute=(a%3600)/60;
        this->second=(a%3600)%60;

        return *this;
    }
    int print()
    {
        return hour;
    }
    int print2()
    {
        return  minute;
    }
    int print3()
    {
        return second;
    }
};
int main()
{
    int x,y,z,second;
    while(1)
    {
        cin>>x>>y>>z>>second;
        if(!x && !y && !z && !second)
            break;
        else if (x>=24  || y>60 || z>60 || x<0 || y<0 || z<0)
        {
            cout<<"Time Error!"<<endl;
            continue;
        }
        else if( second<0)
        {
            cout<<"Input n Error!"<<endl;
            continue;
        }
        else{
            class time time1(x,y,z);
             time1=time1+second;
             cout<<"Time:"<<time1.print()<<":"<<time1.print2()<<":"<<time1.print3()<<endl;
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值