题目:定义一个时间类time,内有数据成员hour,minute,second,另有成员函数:构造函数用于初始化数据成员,输出函数,运算符重载+(加号),。编写主函数:创建时间对象,再输入秒数 n,通过运算符重载+(减号),计算该时间再过 n 秒后的时间值,时间的表示形式为时:分:秒,超过 24 时从 0 时重新开始计时。
测试输入包含若干测试用例,每个测试用例占一行。当读入0 0 0 0时输入结束,相应的结果不要输出。
输入输出示例:括号内为说明
0 0 1 59 (时间为0:0:1,秒数n=59)
11 59 40 30 (时间为11:59:40,秒数n=30)
23 59 40 3011 (时间为23:59:40,秒数n=3011)
0 0 0 0
输出:
time:0:1:0↙(0:0:01加上59秒的新时间)
time:12:0:10↙(11:59:40加上30秒的新时间)
time:0:49:51↙(23:59:40加上3011秒的新时间)
#include<iostream>
using namespace std;
class time{
public:
int hour,minute,second;
time(int h=0,int m=0,int s=0)
{
hour=h;
minute=m;
second=s;
}
set(int a,int b,int c)
{
hour=a;
minute=b;
second=c;
}
};
time operator+(time &t,int add)
{
t.second+=add;
if(t.second>=60)
{
t.minute+=t.second/60;
t.second%=60;
}
if(t.minute>=60)
{
t.hour+=t.minute/60;
t.minute%=60;
}
if(t.hour>=24)
t.hour%=24;
return t;
}
int main()
{
int add=0,h,m,s;
cin>>h>>m>>s>>add;
while(!(h==0&&m==0&&s==0))
{
time t1(h,m,s);
time t2;
t2=t1+add;
cout<<"time:"<<t2.hour<<":"<<t2.minute<<":"<<t2.second<<endl;
cin>>h>>m>>s>>add;
}
return 0;
}
//pta测试显示编译错误:
/*a.cpp:13:1: error: ‘time’ does not name a type
time operator+(time &t,int add)
^~~~
a.cpp: In function ‘int main()’:
a.cpp:36:8: error: expected ‘;’ before ‘t1’
time t1(h,m,s);
^~
a.cpp:36:17: warning: statement is a reference, not call, to function ‘time’ [-Waddress]
time t1(h,m,s);
^
a.cpp:36:17: warning: statement has no effect [-Wunused-value]
a.cpp:37:8: error: expected ‘;’ before ‘t2’
time t2;
^~
a.cpp:37:10: warning: statement is a reference, not call, to function ‘time’ [-Waddress]
time t2;
^
a.cpp:37:10: warning: statement has no effect [-Wunused-value]
a.cpp:38:3: error: ‘t2’ was not declared in this scope
t2=t1+add;
^~
a.cpp:38:3: note: suggested alternative: ‘tm’
t2=t1+add;
^~
tm
a.cpp:38:6: error: ‘t1’ was not declared in this scope
t2=t1+add;
^~
a.cpp:38:6: note: suggested alternative: ‘tm’
t2=t1+add;
^~
tm */