标题:[原创]两时间相加
取消只看楼主
qilindh
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-10-7
 问题点数:0 回复次数:0 
[原创]两时间相加

# 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...

通过测试

搜索更多相关主题的帖子: 相加 时间 
2007-08-25 10:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-165316-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.367246 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved