01./*
02.02.*Copyright (c)2016,烟台大学计算机与控制工程学院
03.03.*All rights reserved.
04.04.*文件名称:main.cpp
05.05.*作 者:田志伟
06.06.*完成日期:2016年4月5日
07.07.*版 本 号:v1.0
08.08.*
09.09.*问题描述:在原有基础上在类内增加成员函数
10.10.
11.11. 输入描述:小时分钟秒
12.12.*输出描述:小时分钟秒
13.13.*/
14.
15.<p>
16.#include <iostream>
17.using namespace std;
18.class Time
19.{
20.public:
21. void set_time();
22. void show_time();
23. void add_sec(int );
24. void add_minute(int );
25. void add_hour(int );
26.private:
27. bool is_time(int,int,int);
28. int hour;
29. int minute;
30. int sec;
31.};
32.void Time::add_hour(int x)
33.{
34. int a;
35. a=x;
36. hour=hour+a;
37.}
38.void Time::add_minute(int y)
39.{
40. int b;
41. b=y;</p><p> minute=minute+b;
42.}
43.void Time::add_sec(int z)
44.{
45. int c;
46. z=c;
47. sec=sec+c;
48.}
49.void Time::set_time()
50.{
51. char c1,c2;
52. cout<<"请输入时间(格式 hh:mm:ss)";
53. while(1)
54. {
55. cin>>hour>>c1>>minute>>c2>>sec;
56. if(c1!=':'||c2!=':')
57. cout<<"格式不正确重新输入"<<endl;
58. else if(!is_time(hour,minute,sec))
59. cout<<"时间非法,请重新输入"<<endl;
60. else
61. break;
62. }
63.}
64.void Time::show_time()
65.{
66. if(sec>60)
67. {
68. minute=minute+sec/60;
69. sec=sec%60;
70. }
71. else
72. sec=sec;</p><p>
73. if(minute>60)
74. {
75. hour=hour+minute/60;
76. minute=minute%60;
77. }
78. else
79. minute=minute;</p><p> if(hour>24)
80. hour=hour%24;
81. else
82. hour=hour;
83.cout<<hour<<":"<<minute<<":"<<sec<<endl;</p><p>}
84.bool Time::is_time(int h,int m,int s)
85.{
86. if(h<0||h>24||m<0||m>60||s<0||s>60)
87. return false;
88. else
89. return true;
90.}
91.int main()
92.{
93. Time t1;
94. int x,y,z;
95. t1.set_time();
96. cout<<"请输入要增加的 小时数 和 分钟数 以及 秒数"<<endl;
97. cin>>x>>y>>z;
98. t1.add_hour(x);
99. t1.add_minute(y);
100. t1.add_sec(z);
101. t1.show_time();
102. return 0;
103.}
程序运行: