第十二周项目——时间类

一时失言乱红尘 2021-06-10 20:41 539阅读 0赞

问题及代码:

  1. /*copyright(c)2016.烟台大学计算机学院
  2. * All rights reserved,
  3. * 文件名称:text.Cpp
  4. * 作者:李一波
  5. * 完成日期:2016年5月19日
  6. * 版本号:vc++6.0
  7. *
  8. * 问题描述: 时间类
  9. * 输入描述:
  10. * 程序输出:
  11. */
  12. #include<iostream>
  13. using namespace std;
  14. class CTime
  15. {
  16. private:
  17. unsigned short int hour; // 时
  18. unsigned short int minute; // 分
  19. unsigned short int second; // 秒
  20. public:
  21. CTime(int h=0,int m=0,int s=0);
  22. void setTime(int h,int m,int s);
  23. //输入输出运算的重载
  24. friend istream &operator>>(istream &in,CTime &t);
  25. friend ostream &operator<<(ostream &out,CTime t);
  26. //比较运算符(二目)的重载
  27. bool operator > (CTime &t);
  28. bool operator < (CTime &t);
  29. bool operator >= (CTime &t);
  30. bool operator <= (CTime &t);
  31. bool operator == (CTime &t);
  32. bool operator != (CTime &t);
  33. //二目运算符的重载
  34. CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
  35. CTime operator-(CTime &c);//对照+理解
  36. CTime operator+(int s);//返回s秒后的时间
  37. CTime operator-(int s);//返回s秒前的时间
  38. //一目运算符的重载
  39. CTime operator++(int);//后置++,下一秒
  40. CTime &operator++();//前置++,下一秒
  41. CTime operator--(int);//后置--,前一秒
  42. CTime &operator--();//前置--,前一秒
  43. //赋值运算符的重载
  44. CTime &operator+=(CTime &c);
  45. CTime &operator-=(CTime &c);
  46. CTime &operator+=(int s);//返回s秒后的时间
  47. CTime &operator-=(int s);//返回s秒前的时间
  48. };
  49. //构造函数
  50. CTime::CTime(int h,int m,int s)
  51. {
  52. hour=h;
  53. minute=m;
  54. second=s;
  55. }
  56. // 设置时间
  57. void CTime::setTime(int h,int m,int s)
  58. {
  59. hour=h;
  60. minute=m;
  61. second=s;
  62. }
  63. // 重载输入运算符>>
  64. istream &operator>>(istream &in,CTime &t)
  65. {
  66. char ch1,ch2;
  67. while(1)
  68. {
  69. cout<<"请输入时间(hh:mm:ss) ";
  70. cin>>t.hour>>ch1>>t.minute>>ch2>>t.second;
  71. if (ch1==':' && ch2==':')
  72. if (t.hour>-1 && t.hour<24 && t.minute>-1 && t.minute<60 && t.second>-1 && t.second<60) break;
  73. cerr<<"时间格式不正确! 请重新输入\n";
  74. }
  75. return cin;
  76. }
  77. // 重载输出运算符<<
  78. ostream &operator<<(ostream &out,CTime t)
  79. {
  80. out<<t.hour<<':'<<t.minute<<':'<<t.second;
  81. return out;
  82. }
  83. //比较运算符的重载
  84. bool CTime::operator > (CTime &t) // 判断时间t1>t2
  85. {
  86. if (hour>t.hour) return true;
  87. if (hour<t.hour) return false;
  88. if (minute>t.minute) return true;
  89. if (minute<t.minute) return false;
  90. if (second>t.second) return true;
  91. return false;
  92. }
  93. bool CTime::operator < (CTime &t)// 判断时间t1<t2
  94. {
  95. if (hour<t.hour) return true;
  96. if (hour>t.hour) return false;
  97. if (minute<t.minute) return true;
  98. if (minute>t.minute) return false;
  99. if (second<t.second) return true;
  100. return false;
  101. }
  102. bool CTime::operator == (CTime &t)// 判断时间t1==t2
  103. {
  104. if (*this<t || *this>t) return false;
  105. return true;
  106. }
  107. bool CTime::operator != (CTime &t) // 判断时间t1!=t2
  108. {
  109. if (*this==t) return false;
  110. return true;
  111. }
  112. bool CTime::operator >= (CTime &t)// 判断时间t1>=t2
  113. {
  114. if (*this<t) return false;
  115. return true;
  116. }
  117. bool CTime::operator <= (CTime &t) // 判断时间t1<=t2
  118. {
  119. if (*this>t) return false;
  120. return true;
  121. }
  122. //二目运算符的重载
  123. // 计算时间之和, 返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
  124. CTime CTime::operator + (CTime &t)
  125. {
  126. int h,m,s;
  127. s=second+t.second;
  128. m=minute+t.minute;
  129. h=hour+t.hour;
  130. if (s>59)
  131. {
  132. s-=60;
  133. m++;
  134. }
  135. if (m>59)
  136. {
  137. m-=60;
  138. h++;
  139. }
  140. while (h>23) h-=24;
  141. CTime t0(h,m,s);
  142. return t0;
  143. }
  144. //返回s秒后的时间
  145. CTime CTime::operator+(int s)
  146. {
  147. int ss=s%60;
  148. int mm=(s/60)%60;
  149. int hh=s/3600;
  150. CTime t0(hh,mm,ss);
  151. return *this+t0;
  152. }
  153. // 计算时间之差
  154. CTime CTime::operator - (CTime &t)
  155. {
  156. int h,m,s;
  157. s=second-t.second;
  158. m=minute-t.minute;
  159. h=hour-t.hour;
  160. if (s<0)
  161. {
  162. s+=60;
  163. m--;
  164. }
  165. if (m<0)
  166. {
  167. m+=60;
  168. h--;
  169. }
  170. while (h<0) h+=24;
  171. CTime t0(h,m,s);
  172. return t0;
  173. }
  174. //返回s秒前的时间
  175. CTime CTime::operator-(int s)
  176. {
  177. int ss=s%60;
  178. int mm=(s/60)%60;
  179. int hh=s/3600;
  180. CTime t0(hh,mm,ss);
  181. return *this-t0;
  182. }
  183. //一目运算符的重载
  184. CTime CTime::operator++(int)//后置++,下一秒
  185. {
  186. CTime t=*this;
  187. *this=*this+1;
  188. return t;
  189. }
  190. CTime &CTime::operator++()//前置++,下一秒
  191. {
  192. *this=*this+1;
  193. return *this;
  194. }
  195. CTime CTime::operator--(int)//后置--,前一秒
  196. {
  197. CTime t=*this;
  198. *this=*this-1;
  199. return t;
  200. }
  201. CTime &CTime::operator--()//前置--,前一秒
  202. {
  203. *this=*this-1;
  204. return *this;
  205. }
  206. //赋值运算符的重载
  207. CTime &CTime::operator+=(CTime &c)
  208. {
  209. *this=*this+c;
  210. return *this;
  211. }
  212. CTime &CTime::operator-=(CTime &c)
  213. {
  214. *this=*this-c;
  215. return *this;
  216. }
  217. CTime &CTime::operator+=(int s)//返回s秒后的时间
  218. {
  219. *this=*this+s;
  220. return *this;
  221. }
  222. CTime &CTime::operator-=(int s)//返回s秒前的时间
  223. {
  224. *this=*this-s;
  225. return *this;
  226. }
  227. int main()
  228. {
  229. CTime t1,t2,t;
  230. cout<<"t1为:";
  231. cin>>t1;
  232. cout<<"t2为:";
  233. cin>>t2;
  234. cout<<"下面比较两个时间大小:\n";
  235. if (t1>t2) cout<<"t1>t2"<<endl;
  236. if (t1<t2) cout<<"t1<t2"<<endl;
  237. if (t1==t2) cout<<"t1=t2"<<endl;
  238. if (t1!=t2) cout<<"t1≠t2"<<endl;
  239. if (t1>=t2) cout<<"t1≥t2"<<endl;
  240. if (t1<=t2) cout<<"t1≤t2"<<endl;
  241. cout<<endl;
  242. cout<<"t1= "<<t1<<endl;
  243. cout<<"t2= "<<t2<<endl;
  244. cout<<"t=t1++"<<endl;
  245. t=t1++;
  246. cout<<"t= "<<t<<" t1= "<<t1<<endl;
  247. cout<<"t=++t1"<<endl;
  248. t=++t1;
  249. cout<<"t= "<<t<<" t1= "<<t1<<endl;
  250. cout<<"t1+t2= "<<t1+t2<<endl;
  251. cout<<"t1-t2= "<<t1-t2<<endl;
  252. cout<<"t1+2000= "<<t1+2000<<endl;
  253. cout<<"t1-5000= "<<t1-5000<<endl;
  254. return 0;
  255. }

运行结果:

Center

发表评论

表情:
评论列表 (有 0 条评论,539人围观)

还没有评论,来说两句吧...

相关阅读