




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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;stati
6、c 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);inl
7、ine 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 getFlagMore
8、Day()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 >= (
9、const 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 cla
11、ss 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.Sec
13、onds;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"<<Se
14、conds<<"."<<endl;else if(Seconds<10)cout<<"The time is: "<< Hours<<":"<<Minutes<<":0"<<Seconds<<"."<<endl;else if(Minutes<10)cout<<"The time is: "<< Hours<<
15、":0"<<Minutes<<":"<<Seconds<<"."<<endl;elsecout<<"The time is: "<< Hours<<":"<<Minutes<<":"<<Seconds<<"."<<endl;break;case 2:if(Hours<=12)if(Seconds&
16、lt;10 && Minutes<10)cout<<"The time is: "<<Hours<<":0"<<Minutes<<":0"<<Seconds<< " AM."<<endl;else if(Minutes<10)cout<<"The time is: "<<Hours<<":0"<<Minut
17、es<<":"<<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&
19、lt;< " 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-1
20、2<<":"<<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"<<Secon
22、ds<<"."<<endl;else if(Seconds<10)cout<< Hours<<":"<<Minutes<<":0"<<Seconds<<"."<<endl;else if(Minutes<10)cout<< Hours<<":0"<<Minutes<<":"<<Seconds<
23、<"."<<endl;elsecout<< Hours<<":"<<Minutes<<":"<<Seconds<<"."<<endl;break;case 2:if(Hours<=12)if(Seconds<10 && Minutes<10)cout<<Hours<<":0"<<Minutes<<":0&
24、quot;<<Seconds<< " AM."<<endl;else if(Minutes<10)cout<<Hours<<":0"<<Minutes<<":"<<Seconds<< " AM."<<endl;else if(Seconds<10)cout<<Hours<<":"<<Minutes<<":0&
25、quot;<<Seconds<< " AM."<<endl;else cout<<Hours<<":"<<Minutes<<":"<<Seconds<< " AM."<<endl;else if (Seconds<10 && Minutes<10)cout<<Hours-12<<":0"<<Minutes<&
26、lt;":0"<<Seconds<< " PM."<<endl; else if (Minutes<10)cout<<Hours-12<<":0"<<Minutes<<":"<<Seconds<< " PM."<<endl;else if (Seconds<10)cout<<Hours-12<<":"<<Min
27、utes<<":0"<<Seconds<< " PM."<<endl;elsecout<<Hours-12<<":"<<Minutes<<":"<<Seconds<< " PM."<<endl;break;default:cout<<"The format is not ok"<<endl; /error (if form
28、at 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 <= (con
29、st 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)
30、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)return true;if(Hours=T.Hours)if(Minutes>T.Minutes)return true;if(
31、Minutes=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 != function
32、return !( (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 - fun
35、ctionint 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)/s
36、ame 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) &am
37、p;& (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 - funct
38、ionif ( (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,
40、SecondTemp) );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,Secon
41、dTemp) );/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,N
42、ovember,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);in
43、line 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 m
44、onth,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&am
45、p; 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 ostre
47、am& 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 &qu
48、ot;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",&q
49、uot;August","September","October","November","December" ;char cDate_t:DDays10 = "Saturday","Sunday","Monday","Tuesday","Wensday","Thursday","Friday" ;/-cDate_t:cDate_t() /构造函数,赋值当前时间Set
50、Error();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=tr
51、ue;/-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 objectError
52、1();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<<"/&q
53、uot;<<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()<&
55、lt;""<<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<< "."<<&quo
57、t; "<<GetDayOfWeekName()<<""<<endl;break;case 3: /用英文月份形式代替数字cout<<Day<<"/"<<str<<"/"<<Year<< "."<<" "<<GetDayOfWeekName()<<""<<endl;break;default:cout<&l
58、t;"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;/-
59、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 /重载>运算符/ 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 /重载>=运算符/
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025小鸭苗买卖服务合同
- 智能手机在传染病防控中的应用指南
- 骨科亮点护理实践体系
- 青年医学教师授课比赛实施要点
- 人教版小学一年级语文上册第八单元测试题
- 造口疝气规范化护理要点
- 二手房交易方式之委托交易
- 学校下学期质量管理工作总结模版
- 2024年09月26日更新【Attest】2024年美国媒体使用报告
- 服装合作协议书
- 2025年5G网络在无人机领域的应用可行性研究报告
- 2025四川爱众集团第一批次招聘10人笔试参考题库附带答案详解
- 工业用地开发项目成本分析与资金筹措方案
- 2025年初中地理学业水平考试模拟试卷:地图与地球知识综合训练试题卷及答案
- (人教2024版)英语七年级下册Unit7.4 Section B 1a-2d课件(新教材)
- 2025年广东嘉城建设集团有限公司及其下属公司招聘笔试参考题库含答案解析
- 2025年湖北荆州市监利市畅惠交通投资有限公司招聘笔试参考题库含答案解析
- 酒店入股合同协议书
- 银行sql考试题及答案
- 隔离技术知识试题及答案
- 2025三方贸易协议合同范本 贸易合同范本
评论
0/150
提交评论