定义一个时间类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;
}
}
}