第二学期第三周项目3--时间类
2014-03-12 12:02
525 查看
/* * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 作 者: 沈远宏 * 完成日期:2013 年03月12日 * 版 本 号:v1.0 * 问题描述:请在原类基础上,增加下列成员函数,要求前三个在类内定义(将是内置成员函数) , 后三个在类内声明,类外定义。在 main()数中增加适当的调用以展示扩充类定义后的功能。 add_a_sec() //增加 1 秒钟 add_a_minute() //增加 1 分钟 add_an_hour() //增加 1 小时 add_seconds(int) //增加 n 秒钟 add_minutes(int) //增加 n 分钟 add_hours(int) //增加 n 小时 提示: (1)要考虑增加后超出取值范围的情形; (2)增加 n 秒后,秒数可能会超过 60,调整 秒数,并可以调用增加分钟数的成员函数,使时间合法;同理,增加分钟数也有类似问题。 */#include <iostream> using namespace std; class Time { public: void add_a_sec() { int s=sec; int m=minute; int h=hour; s++; if(s==60) { s=0; m++; if(m==60) { m=0; h++; if(h==25) { h=1; } } } cout<<h<<":"<<m<<":"<<s<<endl; } void add_a_minute() { int s=sec; int m=minute; int h=hour; m++; if(m==60) { m=0; h++; if(h==25) { h=1; } } cout<<h<<":"<<m<<":"<<s<<endl; } void add_a_hour() { int s=sec; int m=minute; int h=hour; h++; if(h==25) { h=1; } cout<<h<<":"<<m<<":"<<s<<endl; } void set_time(); void show_time(); void add_seconds(int); void add_minutes(int); void add_hours(int); private: bool is_time(int, int, int); //这个成员函数设置为私有的,是合适的,请品味 int hour; int minute; int sec; }; void Time::set_time( ) { char c1,c2; cout<<"请输入时间(格式hh:mm:ss):"; while(1) { cin>>hour>>c1>>minute>>c2>>sec; if(c1!=':'||c2!=':') cout<<"格式不正确,请重新输入"<<endl; else if (!is_time(hour,minute,sec)) cout<<"时间非法,请重新输入"<<endl; else break; } } void Time::show_time( ) { cout<<hour<<":"<<minute<<":"<<sec<<endl; } bool Time::is_time(int h,int m, int s) { if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60) return false; return true; } void Time::add_seconds(int n) { int s=Time::sec; int m=Time::minute; int h=Time::hour; for(int i=0; i<n; ++i) { s++; if(s==60) { s=0; m++; if(m==60) { m=0; h++; if(h==25) { h=1; } } } } cout<<h<<":"<<m<<":"<<s<<endl; } void Time::add_minutes(int n) { int s=Time::sec; int m=Time::minute; int h=Time::hour; for(int i=0; i<n; ++i) { m++; if(m==60) { m=0; h++; if(h==25) { h=1; } } } cout<<h<<":"<<m<<":"<<s<<endl; } void Time::add_hours(int n) { int s=Time::sec; int m=Time::minute; int h=Time::hour; for(int i=0; i<n; ++i) { h++; if(h==25) { h=1; } } cout<<h<<":"<<m<<":"<<s<<endl; } int main( ) { Time t1; int m,n,t; t1.set_time( ); t1.show_time( ); cout<<"增加1秒后的时间为:"; t1.add_a_sec(); cout<<endl; cout<<"增加1分后的时间为:"; t1.add_a_minute(); cout<<endl; cout<<"增加1小时后的时间为:"; t1.add_a_hour(); cout<<endl; cout<<"请输入一个秒数:"; cin>>m; cout<<"增加n秒后的时间为:"; t1.add_seconds(m); cout<<endl; cout<<"请输入一个分钟数:"; cin>>n; cout<<"增加n分后的时间为:"; t1.add_minutes(n); cout<<endl; cout<<"请输入一个小时数:"; cin>>t; cout<<"增加n秒后的时间为:"; t1.add_hours(t); cout<<endl; return 0; }
运行结果:
心得体会:
真是越小的细节越能决定成败“==”才是等于,自己这个错误从下学期开始就一直犯,太不应该了!!
值得从老师的程序文本中学习的是他那个判断时间。格式是否合法的函数构架,自己在这方面总是会写的相当麻烦而且错误不断!仔细从里面抽取人家的精华。
相关文章推荐
- 大一第二学期第2周【项目3:时间类】
- 第二学期第三周项目4--长方柱类
- 第二学期第三周项目3 三角形类的多文件组织
- 第二学期第三周项目2--旱冰场造价
- 第二学期第12周项目4--日期时间类
- 第二学期第三周 项目1
- 第二学期第五周项目1--三角形类的构造函数4
- 第二学期第11周项目2--职员有薪水了(1)
- 第二学期第六周项目3--用多文件组织多个类的程序
- 第三周 项目3-时间类
- 第二学期第13周项目2--圆的比较
- 大一第二学期第2周【项目2:长方柱类】
- 第二学期第八周项目1--实现复数类中的运算符重载(两个复数的运算友元)
- 第三周项目二时间类2
- 大一第二学期第3周【项目1:三角形类1】
- 第二学期第五周项目1--三角形类的构造函数拓展(矩形)
- 第二学期第六周项目4--静态成员作用
- 第二学期第一周项目1--【用枚举表示对称方式】
- 大一第二学期第3周【项目3:程序的多文件组织】
- 第二学期 第一周项目一 初识对象阅读材料回答问题