| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 652 人关注过本帖
标题:[原创]两时间相加
取消只看楼主 加入收藏
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
快速回复:[原创]两时间相加
数据加载中...
 
   



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

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