资源描述
*******************
实践教学
*******************
兰州理工大学
计算机与通信学院
2014年秋季学期
面向对象 课程设计
题 目: 万年历的设计
专业班级:
姓 名:
学 号:
指导教师:
成 绩:
42
序言
《面向对象的程序设计》是计算机专业一门重要的专业基础课。此次课程设计的目的是以面向对象程序设计语言为基础,通过完成一些具有一定难度的课程设计题目的编写、调试、运行工作,进一步掌握面向过程和面向对象程序设计的基本方法和编程技巧,巩固所学理论知识,使理论与实际相结合。从而提高自我分析问题、解决问题的能力。通过课程设计,学生在下述各方面的能力应该得到锻炼:
(1)进一步巩固、加深学生所学专业课程《C++语言程序设计》的基本理论知识,理论联系实际,进一步培养学生综合分析问题、解决问题的能力。
(2)全面考核学生所掌握的基本理论知识及其实际业务能力,从而达到提高学生素质的最终目的。
(3)利用所学知识,开发小型应用系统,掌握运用C++语言编写调试应用系统程序,训练独立开发应用系统,进行数据处理的综合能力。
(4)对于给定的设计题目,如何进行分析,理清思路,并给出相应的数学模型。
(5)掌握面向对象的程序设计方法。
(6)进一步掌握在集成环境下如何调试程序、修改程序和程序的测试。
目录
摘 要 2
第一章 系统总体设计 3
一.理论说明 3
二.流程图说明 4
1.总体流程说明图 4
2.部分流程说明图 4
第二章 系统详细设计 7
一. 主要组成部分 7
二. 源程序 9
第三章 系统测试 34
四 软件使用说明书 40
一.系统运行环境 40
二.系统操作提示 40
总 结 41
参考文献 42
致谢 42
摘 要
万年历作为日常中的小工具,具有多方面的功能,能有效帮助人们记录以及计算时间,在极大程度上帮助人们在日期以及时间方面有最直观的体现。
该设计通过C++的基本知识和技能设计出一个万年历程序,该程序需实现 时间和日期的显示以及计算。
一.实现提供时、分、秒组成的时间,并提供时间增加的方法(按秒);
二.实现记录年、月、日的过程,并提供日期增加的方法(按天);
三.通过上述两项,设计出除除具有年、月、日、时、分、秒外,还增加国名和与格林威治时间的差;
四. 输入年、月、日、时、分、秒,在屏幕上模拟显示一电子计时器,不断输出下一秒的时间和日期,以及格林威治的日期和时间。
该设计不仅体现了普通日历的功能,还加入了电子计时器的部分,使得万年历在原本的基础上有了更大的扩充。
关键字:时间,日期,格林威治时间差,电子计时器
第一章 系统总体设计
一.理论说明
关于万年历的基本要求,须体现时间,日期等,所以,定义时间类time,日期类date,完成关于年(year),月(month),日(day),时(hour),分(minute),秒(second)的定义。
1 在计算时间的时候,每达到1秒的时候对second进行加1,然后判断是否达到60秒,达到的话minute就加1,minute到达60hour就加一,同样的方法对hour、day、month和year进行计算。
2在计算日期的时候,在对day计数时,考虑到大小月和闰年,对day进行加1时,判断是哪一月和是否为闰年。在确定年、月、日之后,使用函数Week()进行对星期几的计算,根据1901年每月1号是星期几可以得出现在是星期几。
在这个程序中,还使用了一个对话框类,使用对话框来对时间日期的设置。在菜单栏里添加一个“设置时间”的选项和一个菜单消息,当点击这个选项就会弹出设置时间日期的对话框,对话框中编辑控件设定了每个值的取值范围,从而对输入错误进行排除。
二.流程图说明
1.总体流程说明图
万年历模拟计算
退出系统
信息输入
万年历系统
( 图一)
2.部分流程说明图
(1)
输入年份
结束
365天
366天
28天
29天天
闰年
否
是
二月天数 是
全年天数
(图二)
(2)
判断年份情况
完成日期的定义以及增加方式
完成时间的定义以及增加方式
日期类date
时间类time
Datetime
完成电子时钟的模拟
定义week()
输出日期的星期数
计算两日期相差天数
定义situation()
打印结果
(图三)
另:在本程序中共用到三个类,定义时间的time类,定义日期的date类,以及公有继承了time和date的datetime类,此派生类的作用是定义电子时钟的运行方式。在程序中还运用到一些独立的函数,以实现星期以及格林威治时间差等功能。
二 、系统详细设计
1. 主要组成部分
该程序主要为了实现日期和时间的显示以及格林威治时间差的计算结果,
其中功能时间的显示使用类datetime来实现,功能时差的计算使用类time和类date来实现。下面分别介绍类time,date的详细设计。
时间类time的具体实现流程:先声明后定义,类time定义为时间基类,其中:成员函数time()主要功能为时分秒赋初值,实现方式为:
函数内定义成员函数
int gethour(){return hour;};
int getminute(){return minute;};
int getsecond(){return second;}返回时分秒的值;
成员函数
void sethour(int nhour){hour=nhour;};
void setminute(int nminute){minute=nminute;};
void setsecond(int nsecond){second=nsecond;};
成员函数~time主要功能为在撤销对象占有的内存之前完成清理工作释放内存,实现方式为:,通过对象调用析构函数完成清理工作。
类time具体实现流程为:
class time{
private:
int hour;
int minute;
int second;
public:
time();
time(int nhour,int nminute,int nsecond);
time(time &c);
~time();
int gethour(){return hour;};
int getminute(){return minute;};
int getsecond(){return second;};
void sethour(int nhour){hour=nhour;};
void setminute(int nminute){minute=nminute;};
void setsecond(int nsecond){second=nsecond;};
};
类date定义为:描述日期的基类
其中:成员函数
int getyear(){return year;};
int getmonth(){return month;};
int getday(){return day;};
string getweek(){ return week;};主要功能为返回年月日的值,实现方式为:调用成员函数
成员函数
void setyear(int nyear){year=nyear;};
void setmonth(int nmonth){month=nmonth;};
void setday(int nday){day=nday;};
void setweek(string c){week=c;};主要功能为给私有数据成员赋值,实现方式为通过调用析构函数实现。
类datetime公开继承类time和类date的属性,实现电子时钟的功能,完成时间由秒自增的过程。
定义函数week(),以及situation(),完成日期对应星期数以及时间差的计算过程,完善电子时钟以及万年历的功能。
2. 源程序
#include<iostream>
#include<string>
#include<cmath>
#include<windows.h>
using namespace std;
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isleapyear(int year)//判断是否是闰年
{
return (year%4==0 && year%100!=0)||(year%400==0);
}
int mon_day(int year ,int month)//判断月份对应的天数
{
if (isleapyear(year)&&month==2)
{
return 29;
}
else return mon[month];
}
int getyearday(int year)//返回一年天数
{
if(isleapyear(year))
return 366;
else return 365;
}
class time
{
private:
int hour;
int minute;
int second;
public:
time();
time(int nhour,int nminute,int nsecond);
time(time &c);
~time();
int gethour(){return hour;};
int getminute(){return minute;};
int getsecond(){return second;};
void sethour(int nhour){hour=nhour;};
void setminute(int nminute){minute=nminute;};
void setsecond(int nsecond){second=nsecond;};
};
class date
{
private:
int year;
int month;
int day;
string week;
public:
date();
date(int y,int m,int d);
date(date &t);
~date();
int getyear(){return year;};
int getmonth(){return month;};
int getday(){return day;};
string getweek(){ return week;};
void setyear(int nyear){year=nyear;};
void setmonth(int nmonth){month=nmonth;};
void setday(int nday){day=nday;};
void setweek(string c){week=c;};
void reset(int y=0,int m=0,int d=0);
date operator +(const date &t);
date operator -(const date &t);
};
class datetime:public date,public time
{
private:
public:
datetime();
datetime(int nyear,int nmonth,int nday,int nhour,int nminute,int nsecond);
datetime(int nyear,int nmonth,int nday,time & c);
datetime(date & t,int nhour,int nminute,int nsecond);
datetime addbysecond();
datetime operator=(datetime & t);
datetime operator+(datetime & t);
};
time::time()//判断时间并进行赋值
{
hour=0;
minute=0;
second=0;
}
time::time(int nhour,int nminute,int nsecond)
{
if(nhour>24||nhour<0)
{ hour=0;
}
else hour=nhour;
if(nminute>59||nminute<0)
{
minute=0;}
else minute=nminute;
if(nsecond>59||nsecond<0)
{
second=0;}
else second=nsecond;
}
time::time(time &c)
{
hour=c.hour;
minute=c.minute;
second=c.second;
}
time::~time()//time析构函数
{}
date::date()//date构造函数
{
year=0;
month=0;
day=0;
}
date::date(int y,int m,int d)
{
if(y<1900)
{
y=1900;
}
else year=y;
if(m>12 || m<=0)
{
m=1;
}
else month=m;
int maxday=0;
if(m==2)
{
if(isleapyear(year))
{
maxday=29;
}
else
{
maxday=28;
}
}
else
{
maxday=mon[m];
}
if(d>maxday || d<=0)
{
d=1;
}
day=d;
}
date::date(date & t)
{
year=t.year ;
month=t.year;
day=t.day;
}
date::~date(){};
void date::reset (int y,int m,int d)
{
this->year =y;
this->month =m;
this->day =d;
}
datetime::datetime():date(),time(){};
datetime::datetime(int nyear,int nmonth,int nday,int nhour,int nminute,int nsecond):date(nyear,nmonth,nday),time(nhour,nminute,nsecond)
{}
datetime::datetime(int nyear,int nmonth,int nday,time & c):time(c),date(nyear,nmonth,nday)
{}
datetime::datetime(date & t,int nhour,int nminute,int nsecond):date(t),time(nhour,nminute,nsecond)
{}
datetime datetime::addbysecond()
{
this->setsecond(this->getsecond()+1);
if(this->getsecond()>59)
{
this->setsecond(0);
this->setminute(this->getminute()+1);
if(this->getminute()>59)
{
this->setminute(0);
this->sethour(this->gethour()+1);
if(this->gethour()>23)
{
this->sethour(0);
this->setday(this->getday()+1);
if(this->getday()>mon_day( this->getyear() ,this->getmonth()))
{
this->setday(1);
this->setmonth(this->getmonth()+1);
if(this->getmonth() >12)
{
this->setmonth(1);
this->setyear(this->getyear()+1);
}
}
}
}
}
return *this;
}
datetime datetime::operator=(datetime & t)
{
this->setyear(t.getyear());
this->setmonth(t.getmonth());
this->setday(t.getday());
this->sethour(t.gethour());
this->setminute(t.getminute());
this->setsecond(t.getsecond());
return *this;
}
datetime datetime::operator+(datetime & t)
{
if((this->getyear()+t.getyear())<1900)
{
this->setyear(1900);
}
else this->setyear((this->getyear()+t.getyear()));
if((this->getmonth()+t.getmonth())>12 || (this->getmonth()+t.getmonth())<=0)
{
this->setmonth(1);
}
else this->setmonth((this->getmonth()+t.getmonth()));
int maxday=0;
if((this->getyear()+t.getyear())==2)
{
if(isleapyear((this->getyear()+t.getyear())))
{
maxday=29;
}
else
{
maxday=28;
}
}
else
{
maxday=mon[this->getmonth()+t.getmonth()];
}
if((this->getday()+t.getday())>maxday ||(this->getday()+t.getday())<=0)
{
this->setday(1);
}
this->setday((this->getday()+t.getday()));
return *this;
}
int dayinyear(datetime & t)//日期在年中的天数
{
int i=0,day=0;
for(i=1;i<t.getmonth();i++)
{
day+=mon_day(t.getyear(),i);
}
day+=t.getday();
return day;
}
void getweek( datetime &t)//判断天数对应星期
{
int week=0;
int diffday;
if(t.getyear()==1900)
{
if(t.getmonth()==1)
{
diffday=t.getday()-1;
}
else
{
diffday=dayinyear(t)-1;
}
}
else
{
int d1=getyearday(1900)-1;
int d2=dayinyear(t);
int d3=0;
int year=0;
for(year=1901;year<t.getyear();year++)
{
d3+=getyearday(year);
}
diffday=d1+d2+d3;
}
week=diffday%7;
switch(week)
{
case 0:
t.setweek("星期一");break;
case 1:
t.setweek("星期二");break;
case 2:
t.setweek("星期三");break;
case 3:
t.setweek("星期四");break;
case 4:
t.setweek("星期五");break;
case 5:
t.setweek("星期六");break;
case 6:
t.setweek("星期天");break;
}
cout<<t.getweek() <<" "<<endl;;
}
int getmonthfirst(datetime & t)//返回对应月的第一天星期数
{
datetime temp(t.getyear(),t.getmonth(),1,t.gethour(),t.getminute(),t.getsecond());
getweek( temp);//判断对应星期数
if(temp.getweek()=="星期一")
return 1;
else if(temp.getweek()=="星期二")
return 2;
else if(temp.getweek()=="星期三")
return 3;
else if(temp.getweek()=="星期四")
return 4;
else if(temp.getweek()=="星期五")
return 5;
else if(temp.getweek()=="星期六")
return 6;
else
return 7;
}
void showmonth(datetime & t)//打印当前月日历
{
int i=0;
cout<<"Monday"<<" "<<"Tusday"<<" "<<"Wedday"<<" "<<"Thuday"<<" "<<"Friday"<<" "<<"Satday"<<" "<<"Sunday"<<endl;
int j=0;
int k=1;
int num=getmonthfirst(t);
cout<<" ";
for(k=1;k<=8-num;k++)
cout<<k<<" ";
cout<<endl;
for(i=k;i<=mon_day(t.getyear(),t.getmonth());i++)
{
int flag=1;
int first=k;
if(i<10)
{
cout<<" "<<i<<" ";
}
else cout<<i<<" ";
if((i-k)==6)
{
cout<<endl;
k=i+1;
}
}
}
void stuation1(datetime & t)//计算两日期相差天数
{
int nyear=0;int nmonth=0;int nday=0;
int nhour=0;int nminute=0;int nsecond;
cout<<"请输入 年 月 日:"<<endl;
cin>>nyear>>nmonth>>nday;
cout<<"请输入 时 分 秒:"<<endl;
cin>>nhour>>nminute>>nsecond;
datetime temp ( nyear, nmonth,nday, nhour, nminute,nsecond);
int diffday=0;
if(t.getyear()==nyear)
{
if(t.getmonth()==nmonth)
{
diffday=abs(t.getday()-nday);
}
else
{
diffday=abs(dayinyear(t)-dayinyear(temp));
}
}
else if(nyear<t.getyear())
{
int d1=getyearday(temp.getyear())-dayinyear(temp);
int d2=dayinyear(t);
int d3=0;
int year=0;
for(year=nyear;year<t.getyear();year++)
{
d3+=getyearday(year);
}
diffday=d1+d2+d3;
}
else
{
int d1=getyearday(t.getyear())-dayinyear(t);
int d2=dayinyear(temp);
int d3=0;
int year=0;
for(year=t.getyear();year<nyear;year++)
{
d3+=getyearday(year);
}
diffday=d1+d2+d3;
}
cout<<"与原日期相隔的天数为:"<<diffday<<endl;
}
int monthsurplus(datetime & t)//当月剩余天数
{
return mon_day(t.getyear(),t.getmonth())-t.getday();
}
int yearsurplus(datetime & t)//当年剩余天数
{
return getyearday(t.getyear())-dayinyear(t);
}
void stuation2(datetime & t )
{
int diffday=0;
int lastday=0;
int lastmonth=0;
int lastyear=0;
cout<<" 请输入相隔的天数:"<<endl;
cin>>diffday;
if(diffday>0)
{
if(diffday<=monthsurplus(t))//月未变
{
lastday=diffday+t.getday();
lastmonth=t.getmonth();
lastyear=t.getyear();
}
else if(diffday<=yearsurplus(t))//年未变
{
lastyear=t.getyear();
int k=1;
int i=monthsurplus(t);
while(i<diffday)
{
i+=(mon_day(t.getyear(),t.getmonth()+k));
k++;
}
lastmonth=t.getmonth()+k-1;
int h=0; int total=monthsurplus(t);
for(h=(t.getmonth()+1);h<lastmonth;h++)
{
total+=mon_day(t.getyear(),h);
}
lastday=diffday-total;
}
else//年变了
{
int k=1;
int fmonth=0;
int i=yearsurplus(t);
while(i<diffday)
{
i+=getyearday(t.getyear()+k);
k++;
}
lastyear=t.getyear()+k-1;
int fyear=yearsurplus(t);
for(i=(t.getyear()+1);i<lastyear;i++)
{
fyear+=getyearday(i);
}
int surplusday=diffday-fyear;//所求年的天数
int h=0,j=1;
while(h<=surplusday)
{
h+=mon_day(lastyear,j);
j++;
}
lastmonth=j-1;
int total=0;
int j1=1;
for(;j1<lastmonth;j1++)
{
total+=mon_day(lastyear,j1);
}
lastday=surplusday-total;
}
}
else//diffday<0
{
if (abs(diffday)<t.getday())
{
lastday=t.getday();
lastmonth=t.getmonth();
lastyear=t.getyear();
}
else if(abs(diffday)<=dayinyear(t))
{
lastyear=t.getyear();
int j=0,total=0;
int k=t.getmonth();
展开阅读全文