版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、日期和时间课程设计报告1. 功能1.1课程设计题目功能:定义了日期类、时间类和日期时间综合类,重载了+、-、+、-、=、>=、<=、=、!=等运算符,可以设置时间、日期,比较时间和日期的大小,可以进行时间、日期对象的运算(加减),并按多种格式输出结果。1.2思想和方法:通过重载运算符使运算符实现对对象的加减运算,并在结果输出时自动载入闰年判断程序,实现结果的智能化输出;将菜单输出和结果输出隔开,防止混淆;日期类中将星期五单独定义,实现对错误的处理;通过拷贝函数的运用,实现“+”、“-”的重载。1.3增加的新功能:增加了对运算起始日期时间的修改和各操作菜单的退出功能。2. 详细设计2
2、.1 c_time类:重载了+、-、+、-、=、!=、<、>、<=、>=运算符;将时间是否超过或少于当天设置为静态静态成员函数,设置了两个输出函数;“-”中,优化了结构,以四行将原程序代替,将少于一天的情况在后面列出;“+”中,将数值超过范围的情况的处理在后面列出。2.2 c_date类:重载了+、-、+、-、=、!=、<、>、<=、>=运算符;设置了两个输出函数;简化了“+”运算符,同时修正了错误;“+”“-”中,将数值超过范围的情况的处理在后面列出;在第一个函数中,对当前时间日期进行了赋值,是起始日期时间的设定函数。2.3 tdmanage类
3、:重载了+、-、+、-、=、!=、<、>、<=、>=运算符;设置为c_time和c_date的派生类,可以实现对时间和日期的混合加减比较。2.4 tdglobal类:为输入输出的专门的类,重载了<<,>>运算符。2.5 error类:为专门处理错误的类,包括对原日期时间少于1900的处理,以及在“+”、“-”中结果少于1900的处理,以及输出时日期时间少于1900的处理。2.6 main类:以swith型进行菜单的输出,主要是对所有程序的综合处理,对菜单项的编辑,是最后的运行程序。3. 调试过程3.1出现了编译时无问题,而调试时提示没有相关函数的
4、问题,编译结束后载入相关文件解决了问题。3.2无法有效退出,将函数改为swith型解决问题。3.3出现了输出结果与预期不符的状况,更改了输出变量名解决问题。3.4最开始时没有设置对“+”的错误类进行设置,由于不太可能遇到相关情况,没有注意,后来看课本才发现。4. 输入输出开始界面时间格式的更改与否主操作菜单时间类操作,以“+”为例其后继续为主操作菜单日期类操作,以“-”为例综合类操作,以比较为例5. 总结本程序大量运用了运算符的重载,并且用到了多文件的处理,对以后处理或创建大程序很有帮助。而且运算符重载实际上是有很多技巧的,最一般的“+”“-”重载,需要考虑输入的数据的类型及对输出结果的要求。
5、另外错误处理也是个难关,即“error”类,不仅要考虑原日期时间的处理,还要考虑对运算符错误的处理,很有可能会落下某些情况,需要不断调试以保证每种情况都考虑到了。这个程序对个人对类处理必须相当熟悉,几乎将所有可能的情况都涉及了,而且难度不算太大,对于刚接触c+没有太长时间的我们是个很好的练习课程。6. 附件源程序:/ctime_t.h begin#include <iostream.h>#include <time.h>#ifndef ctime_t_h#define ctime_t_hclass ctime_tstatic int flagmoreday;static
6、 int flaglessday;static int format;int seconds;int minutes;int hours;struct tm *currenttime;time_t time_date;public:ctime_t();ctime_t(const ctime_t &t);ctime_t(int hour,int min=0,int sec=0):seconds(sec),minutes(min),hours(hour) ;ctime_t();const ctime_t& operator = (const ctime_t& t);inli
7、ne void setsec (int sec) seconds=sec;inline void setmin (int min) minutes=min;inline void sethour (int hour) hours=hour;void print() const;void print1() const;inline int getsec () const return seconds;inline int getmin () const return minutes;inline int gethour () const return hours;int getflagmored
8、ay()return flagmoreday;int getflaglessday()return flaglessday;void resetflagmoreday()flagmoreday=0;void resetflaglessday()flaglessday=0;bool operator < (const ctime_t& t)const;bool operator <= (const ctime_t& t)const;bool operator > (const ctime_t& t)const;bool operator >= (c
9、onst ctime_t& t)const;bool operator = (const ctime_t& t)const;bool operator != (const ctime_t& t)const;const ctime_t operator + (const ctime_t& t) const;int operator - (ctime_t& t);const ctime_t operator + (int newminutes);const ctime_t operator - (int newminutes);int getformat1(
10、)return format;void operator + ();void operator - (); static void changeformat() format = (format=1)? 2 : 1 ; /format can be only 1 or 2friend ostream& operator << (ostream &out,const ctime_t &t) ; friend istream& operator >> (istream &in, ctime_t &t) ;friend clas
11、s ctdmanage;#endif/ctime_t.h end/ctime_t.cpp/implementation of ctime_t#include "ctime_t.h"int ctime_t:format=1;int ctime_t:flagmoreday=0; / default values for static data membersint ctime_t:flaglessday=0;/-ctime_t:ctime_t() /empty c'tortime_date=time(0);currenttime=localtime(&time_
12、date);seconds=currenttime->tm_sec;minutes=currenttime->tm_min;hours=currenttime->tm_hour;/-ctime_t:ctime_t(const ctime_t &t) /copy c'torseconds=t.seconds;minutes=t.minutes;hours=t.hours;/-const ctime_t& ctime_t:operator = (const ctime_t& t) /operator = functionseconds=t.seco
13、nds;minutes=t.minutes; / copy relevent fieldshours=t.hours;return *this;/-void ctime_t:print() const /print functionswitch(format)case 1:if(seconds<10 && minutes<10)cout<<"the time is: "<< hours<<":0"<<minutes<<":0"<<sec
14、onds<<"."<<endl;else if(seconds<10)cout<<"the time is: "<< hours<<":"<<minutes<<":0"<<seconds<<"."<<endl;else if(minutes<10)cout<<"the time is: "<< hours<<&
15、quot;:0"<<minutes<<":"<<seconds<<"."<<endl;elsecout<<"the time is: "<< hours<<":"<<minutes<<":"<<seconds<<"."<<endl;break;case 2:if(hours<=12)if(seconds&l
16、t;10 && minutes<10)cout<<"the time is: "<<hours<<":0"<<minutes<<":0"<<seconds<< " am."<<endl;else if(minutes<10)cout<<"the time is: "<<hours<<":0"<<minute
17、s<<":"<<seconds<< " am."<<endl;else if(seconds<10)cout<<"the time is: "<<hours<<":"<<minutes<<":0"<<seconds<< " am."<<endl;else cout<<"the time is: "
18、<<hours<<":"<<minutes<<":"<<seconds<< " am."<<endl;else if (seconds<10 && minutes<10)cout<<"the time is: "<<hours-12<<":0"<<minutes<<":0"<<seconds&l
19、t;< " pm."<<endl; else if (minutes<10)cout<<"the time is: "<<hours-12<<":0"<<minutes<<":"<<seconds<< " pm."<<endl;else if (seconds<10)cout<<"the time is: "<<hours-12
20、<<":"<<minutes<<":0"<<seconds<< " pm."<<endl;elsecout<<"the time is: "<<hours-12<<":"<<minutes<<":"<<seconds<< " pm."<<endl;break;default:cout<
21、<"the format is not ok"<<endl; /error (if format value wasn't one of the above/-void ctime_t:print1() const /print functionswitch(format)case 1:if(seconds<10 && minutes<10)cout<< hours<<":0"<<minutes<<":0"<<second
22、s<<"."<<endl;else if(seconds<10)cout<< hours<<":"<<minutes<<":0"<<seconds<<"."<<endl;else if(minutes<10)cout<< hours<<":0"<<minutes<<":"<<seconds<&
23、lt;"."<<endl;elsecout<< hours<<":"<<minutes<<":"<<seconds<<"."<<endl;break;case 2:if(hours<=12)if(seconds<10 && minutes<10)cout<<hours<<":0"<<minutes<<":0&q
24、uot;<<seconds<< " am."<<endl;else if(minutes<10)cout<<hours<<":0"<<minutes<<":"<<seconds<< " am."<<endl;else if(seconds<10)cout<<hours<<":"<<minutes<<":0&q
25、uot;<<seconds<< " am."<<endl;else cout<<hours<<":"<<minutes<<":"<<seconds<< " am."<<endl;else if (seconds<10 && minutes<10)cout<<hours-12<<":0"<<minutes<&l
26、t;":0"<<seconds<< " pm."<<endl; else if (minutes<10)cout<<hours-12<<":0"<<minutes<<":"<<seconds<< " pm."<<endl;else if (seconds<10)cout<<hours-12<<":"<<minu
27、tes<<":0"<<seconds<< " pm."<<endl;elsecout<<hours-12<<":"<<minutes<<":"<<seconds<< " pm."<<endl;break;default:cout<<"the format is not ok"<<endl; /error (if forma
28、t value wasn't one of the above/-bool ctime_t:operator < (const ctime_t& t)const /operator < functionif (hours<t.hours)return true;if(hours=t.hours)if(minutes<t.minutes)return true;if(minutes=t.minutes)return (seconds<t.seconds);return false;/-bool ctime_t:operator <= (cons
29、t ctime_t &t)const /operator <= functionif (hours<t.hours)return true;if(hours=t.hours)if(minutes<t.minutes)return true;if(minutes=t.minutes)return (seconds<=t.seconds);return false;/-bool ctime_t:operator > (const ctime_t& t)const /operator > functionif (hours>t.hours)r
30、eturn true;if(hours=t.hours)if(minutes>t.minutes)return true;if(minutes=t.minutes)return (seconds>t.seconds);return false;/-bool ctime_t:operator >= (const ctime_t& t)const /operator >= functionif (hours>t.hours)return true;if(hours=t.hours)if(minutes>t.minutes)return true;if(m
31、inutes=t.minutes)return (seconds>=t.seconds);return false;/-bool ctime_t:operator = (const ctime_t &t)const /operator = functionreturn ( (hours=t.hours) && (minutes=t.minutes) && (seconds=t.seconds) );/-bool ctime_t:operator != (const ctime_t &t)const/operator != functionr
32、eturn !( (hours=t.hours) && (minutes=t.minutes) && (seconds=t.seconds) );/-const ctime_t ctime_t:operator + (const ctime_t &t) const /operator + functionint hourtemp,minutetemp,secondtemp;/define 3 temp variables to get time datasecondtemp=seconds+t.seconds;if(secondtemp>=60)/
33、more thrn 1 minutesecondtemp-=60;minutetemp=minutes+t.minutes+1;/so add to minuteelseminutetemp=minutes+t.minutes;if(minutetemp>=60)/more then 1 hourminutetemp-=60;hourtemp=hours+t.hours+1;/add to hourelsehourtemp=hours+t.hours;if(hourtemp>=24)flagmoreday=1; /to add day to date classhourtemp-=
34、24;return (ctime_t (hourtemp,minutetemp,secondtemp) ); /return local time class /-int ctime_t:operator - (ctime_t &t)int newhour,newminute;newhour=hours-t.hours;newminute=minutes-t.minutes;return newhour*60+newminute;/*const ctime_t ctime_t:operator - (const ctime_t &t) const /operaor - func
35、tionint hourtemp,minutetemp,secondtemp;/define 3 temp variables to get time datahourtemp = hours-t.hours;if(hourtemp<0)/t class hour was bigger then this classflaglessday=1; /to cut 1 day form date classhourtemp+=24;/ add 24 hours to previous dayminutetemp=minutes-t.minutes;if(minutetemp<0)/sa
36、me for minutesminutetemp+=60;-hourtemp;secondtemp=seconds-t.seconds;if(secondtemp<0)/same for secondssecondtemp+=60;-minutetemp;return ( ctime_t ( hourtemp,minutetemp,secondtemp) );/return local class */-void ctime_t:operator + () /operator + functionif ( (seconds=59) && (minutes=59) &
37、;& (hours=23) ) / end of a dayflagmoreday=1;/to add day to day classseconds=0;minutes=0;hours=0;else if( (seconds=59) && (minutes=59) )/end of hourseconds=0;minutes=0;+hours;else if( seconds=59 )/end of minuteseconds=0;+minutes;else+seconds;/-void ctime_t:operator - () /operator - functi
38、onif ( (seconds=0) && (minutes=0) && (hours=0) )/start of dayflaglessday=1;/to substruct 1 day from day classseconds=59;minutes=59;hours=23;else if( (seconds=0) && (minutes=0) )/start of hourseconds=59;minutes=59;-hours;else if( seconds=0 )/start of minuteseconds=59;-minutes;
39、else-seconds;const ctime_t ctime_t:operator + (int newminutes)int hourtemp,minutetemp,secondtemp;secondtemp=seconds;hourtemp=hours;minutetemp=minutes+newminutes;while(minutetemp>=60)minutetemp-=60;hourtemp+=1;while(hourtemp>=24)hourtemp-=24;flagmoreday+=1;return (ctime_t (hourtemp,minutetemp,s
40、econdtemp) );const ctime_t ctime_t:operator - (int newminutes)int hourtemp,minutetemp,secondtemp;secondtemp=seconds;hourtemp=hours;minutetemp=minutes-newminutes;while(minutetemp<0)minutetemp+=60;hourtemp-=1;while(hourtemp<0)hourtemp+=24;flaglessday=1;return (ctime_t (hourtemp,minutetemp,second
41、temp) );/ctime_t.cpp end/cdate_t.h begin#include <iostream.h>#include <time.h>#ifndef cdate_t_h#define cdate_t_h#define n 3class cdate_tenum days saturday,sunday,monday,tuesday,wensday,thursday,friday ;enum months none,january,february,mars,april,may,june,july,august,september,october,no
42、vember,december ;static int format;static char mon10;static char ddays10;int day;int month;int year;struct tm *currenttime;time_t time_date;public: bool error;cdate_t();cdate_t(const cdate_t &d);cdate_t(int year,int month,int day);cdate_t()const cdate_t& operator = (const cdate_t& d);inl
43、ine void setday (int day) day=day;inline void setmon (int mon) month=mon;inline void setyear (int year) year=year;void print();void print1();inline int getdayofmonth () const return day;inline int getmonth () const return month;inline int getyear () const return year;int getdayofyear(int year,int mo
44、nth,int day);int getdaysinmonth(int month,int year);int getdayofweek();bool isleapyear(int year);char* getdayofweekname();char* getnameofmonth();int retriveday(int days,int year);int retrivemonth(int days,int year);bool operator < (const cdate_t& d)const;bool operator <= (const cdate_t&
45、; d)const;bool operator > (const cdate_t& d)const;bool operator >= (const cdate_t& d)const;bool operator = (const cdate_t& d)const;bool operator != (const cdate_t& d)const;const cdate_t operator + (const cdate_t& d) ;int operator - (cdate_t& d) ;const cdate_t operator +
46、 (int days) ;const cdate_t operator - (int days) ;int getformat2()return format;void operator + ();void operator - (); static void cdate_t:changeformat() /implementation of static function must be in headerswitch(format) case 1: format=2; break;case 2: format=3; break;case 3: format=1; friend ostrea
47、m& operator << (ostream &out,const cdate_t &d) ; friend istream& operator >> (istream &in, cdate_t &d) ;void seterror() error=false;friend class ctdmanage;#endif/cdate_t.h end/cdate_t.cpp begin/implementation of cdate_t.h#include "cdate_t.h"#include &quo
48、t;error.h"#include <math.h>#define max(a, b) (a) > (b) ? (a) : (b)#define min(a, b) (a) < (b) ? (a) : (b)int cdate_t:format=1;/静态成员变量char cdate_t:mon10 = "janaury","february","mars","april","may","juny","july",&qu
49、ot;august","september","october","november","december" ;char cdate_t:ddays10 = "saturday","sunday","monday","tuesday","wensday","thursday","friday" ;/-cdate_t:cdate_t() /构造函数,赋值当前时间sete
50、rror();time_date=time(0);currenttime=localtime(&time_date); day=currenttime->tm_mday;month=currenttime->tm_mon+1;year=currenttime->tm_year+1900;/-cdate_t:cdate_t(int year,int month,int day):year(year),month(month),day(day) /c'torseterror();if (year<1900) /错误的数字 error1();error=tru
51、e;/-cdate_t:cdate_t(const cdate_t &d) /拷贝的构造函数seterror();day=d.day;month=d.month;year=d.year;if (year<1900)/错误的数字error1();error=true;/-const cdate_t& cdate_t:operator = (const cdate_t& d) /重载=运算符day=d.day;month=d.month;year=d.year;if (year<1900)/bad data from the other objecterror1
52、();error=true;/置错误标志elseseterror();/正确return *this;/-void cdate_t:print() /输出char strn; for(int i=0;i<n;+i)stri=monmonth-1i;/将月份的英文赋值进strstri='0'if(error)/如果日期出错,输出提示信息error2();return;switch(format) /判断以什么形式输出case 1: /欧洲格式cout<<"the date is: "<< day<<"/&qu
53、ot;<<month<<"/"<<year<<"."<<" "<<getdayofweekname()<<""<<endl;break;case 2: /美国格式cout<<"the date is: "<<month<<"/"<<day<<"/"<<year<< ".
54、"<<" "<<getdayofweekname()<<""<<endl;break;case 3: /用英文月份形式代替数字cout<<"the date is: "<<day<<"/"<<str<<"/"<<year<< "."<<" "<<getdayofweekname()<&l
55、t;""<<endl;break;default:cout<<"the format is not ok"<<endl;/-void cdate_t:print1() /输出char strn; for(int i=0;i<n;+i)stri=monmonth-1i;/将月份的英文赋值进strstri='0'if(error)/如果日期出错,输出提示信息error2();return;switch(format) /判断以什么形式输出case 1: /欧洲格式cout<<day<
56、<"/"<<month<<"/"<<year<<"."<<" "<<getdayofweekname()<<""<<endl;break;case 2: /美国格式cout<<month<<"/"<<day<<"/"<<year<< "."<<"
57、; "<<getdayofweekname()<<""<<endl;break;case 3: /用英文月份形式代替数字cout<<day<<"/"<<str<<"/"<<year<< "."<<" "<<getdayofweekname()<<""<<endl;break;default:cout<<
58、;"the format is not ok"<<endl;/-bool cdate_t:operator < (const cdate_t& d)const / 重载<运算符/ return true if this object is < then the secondif (year<d.year) return true;if(year=d.year)if(month<d.month)return true;if(month=d.month)return (day<d.day);return false;/-b
59、ool cdate_t:operator <= (const cdate_t &d)const / 重载<=运算符/ return true if this object is <= then the secondif (year<d.year)return true;if(year=d.year)if(month<d.month) return true;if(month=d.month) return (day<=d.day);return false;/-bool cdate_t:operator > (const cdate_t& d)const /重载>运算符/ return true if this object is > then the secondif (year>d.year)return true;if(year=d.year) if(month>d.month)return true;if(month=d.month)return (day>d.day);return false;/-bool cdate_t:operator >= (const cdate_t &d)const /重载>=运算符/ re
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论