# include <iostream.h>
class Time
{
private:
int hours,minutes,seconds;
public:
void get_time()
{
cin>>hours>>minutes>>seconds;
}
void display_time()
{
cout<<hours<<':'<<minutes<<':'<<seconds<<endl;
}
void add_time(Time & t1,Time & t2)
{
hours=t1.hours+t2.hours;
minutes=t1.minutes+t2.minutes;
seconds=t1.seconds+t2.seconds;
while(seconds>=60)//增加循环判断
{
seconds-=60;
minutes++;
};
while(minutes>=60)//增加循环判断
{minutes-=60;
hours++;
}
}
};
void main()
{Time one,two,three;
cout<<"\nEnter the first time(hours minutes seconds):";
one.get_time();
cout<<"\nEnter the second time(hours minutes seconds):";
two.get_time();
three.add_time(one,two);
cout<<"the result is:"<<endl;
three.display_time();
}
经过测试,结果如下:
①
Enter the first time(hours minutes seconds):
2
34
45
Enter the second time(hours minutes seconds):
1
47
56
the result is:
4:22:41
press any key to continue...
②
Enter the first time(hours minutes seconds):
2
67
100
Enter the second time(hours minutes seconds):
1
56
200
the result is:
5:8:0
press any key to continue...
通过测试