课程设计报告实验报告_第1页
课程设计报告实验报告_第2页
课程设计报告实验报告_第3页
已阅读5页,还剩31页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+吾言程序设计课程设计报告设计题目:简单万年历专业:网络工程班 级:姓名:杨茂林学号:121124028联系方式录1. 设计目标 32. 设计思想33. 类及函数的实现34. 程序流程图35. 程序源代码116. 调试记录 307. 调试语录338. 总结34简单万年历程序1.1 设计题目描述 本程序是一个简单的实现时间系统功能的万年历程序,此程序是为了遵循 用户的意愿与指示对用户输入的日期进行简单的处理, 输出所要查询的日期的信 息,如:当时的时间,年月日,生肖,天干地支,以及输出所要查询的日期日历 并以日历形式排列出来。1.2 设计目标本程序有以下目标要实现 :

2、用户可以进行选择,是要进行万年历时间校对系统还是万年历的查询 系统,用户可以随时退出系统; :显示本地时间,并征询用户的意见是否要进行时间调整, 如果进行了 时间调整,要输出调整后的时间; :查询某年的万年历,并输出当年的年历,并显示所要查询的年份,显 示出一年的日历、当年的天干地支、生肖、并判断是否为闰年; :将的功能具体到月份; :将的功能具体到日,并判断出用户所要查询的日期当天是什么节日, 以及用户所要查询的节日距今天有多少天; :用户多次进行输入,知道用户满意为止。2. 设计思想 本程序中,因为要用到万年历的时间校对系统,所以,在设计的时候,我 定义了一个时间类: clock 类;由于

3、系统要同步到本地的时间(即为运行时电脑 上显示的时间),这里用到了 struct time 结构体,接下来就是万年历的查询系 统功能里面的函数, 这些函数要实现的有指导用户进行操作的选项的界面, 有计 算该月的 1 号是星期几的函数, 有输出每周即每月的表头函数、 判断是否为闰年、 判断每个月有多少天、 要查询的那天距今天有多少天、 判断所要查询的年份有多 少天、计算指定日期距今天有多少天等函数。其中,共含一个主函数文件、三个 功能文件、一个功能函数头文件、界面文件以及时钟类文件。其中,功能函数是 主要的函数实现部分,时钟类则主要是实现万年历时间系统。3. 类及函数的实现在此只涉及部分函数的实

4、现细节,其他函数详看程序源代码:1.1 本程序中, 我只设计了一个 clock 类,类里面有实现时间系统校对功能的私有量, 在进行时间校对的过程中, 我选择了重载的方法, 以达到自己想要的结果1.2 函数部分的实现则是比较普通的, 没有什么特殊的地方。 其中有如下 函数需要实现:int week(int y,int m);/ 计算该月一号是星期几 void printweek();/ 输出每周表头 void printmonth(int m);/输出每月表头int leep(int y);/ 判断该年是否闰年 int md(int y,int m);/判断该月有多少天void other(in

5、t year);int days1(int y,int m,int d);/ 计算该天时这一年的第几天int days2(int y);/判断该年有多少天int days3(int y);/计算从元年到该之年前总天数int totaldays(int y,int m,int d);/计算指定日期距今天的天数int yi();/ 实现万年历的时间校对功能int er();/ 进入万年历的系统功能int first();/ 实现第一个功能int second();/实现第二个功能void third();/实现第三功能/int tiangandizhi();int jiemian1();/ 界面

6、1int jiemian2();在 jiemian() ,int md() 及 void third() 的实现过程中,我选用了 switch-case 套用 switch-case 语句;在 int week() 函数中,我采用了蔡勒公式用于计算每 个月的星期一是多少,其他函数的实现详看源代码。1.3 在这个程序设计中,有好多知识是我以前没有接触过得,这一点还是比较欣慰的吧,例如,system (“pause”)有着清屏作用,还有就是计算每个月 的 1 号是星期几的问题以及程序运行后背景与字体颜色的变换要用到 system (“ color ” , 9b )4. 程序流程图万年历的三个查询系

7、统功能的实现查询系统的第一个功能界面输入要查询的月份输出所要查询的年份的天干地支,生肖,是否为闰年 定义月份变量m,并付初始值m=1判断该月天数 m+根据年月由蔡勒公式计算出该月1号是星期几输出月份和每周的表头用循环输出前面空白的部分用循环输出该月的日历第一个功能结束o第三个功能结束5. 程序源代码#include<iostream> #include<fstream>/ #include<stdlib.h>/ #include<iomanip>/ #include <time.h> /文件输出清屏控制宽度这两个头文件是为了调用系统时

8、间的#include <stdio.h> #include<conio.h> #include<sstream>using namespace std;int week(int y,int m);/计算该月一号是星期几void printweek();/ 输出每周表头void printmonth(int m);/ 输出每月表头int leep(int y);/ 判断该年是否闰年int md(int y,int m);/判断该月有多少天void other(int year);/ 以下六个函数是第三个功能里的int days1(int y,int m,int

9、 d);/计算该天时这一年的第几天int days2(int y);/判断该年有多少天int days3(int y);/计算从元年到该之年前总天数int totaldays(int y,int m,int d);/计算指定日期距今天的天数int yi();/ 实现万年历的时间校对功能int er();/ 进入万年历的系统功能int first();/ 实现第一个功能int second();/实现第二个功能void third();/实现第三功能/int tiangandizhi();int jiemian1();/ 界面 1int jiemian2();int jiemian()syste

10、m("cls");system("color 9d");/ 控制程序背景和字体的颜色,均为 16进制, 9 背景 颜色, d 字体颜色cout<<setw(6)<<" ="<<endl;cout<<setw(6)<<" "<<"= =*="vve ndl;cout<<setw(6)vv" "<<"+ - +"<<e ndl;cout<<s

11、etw(6)<<""<<"T+"<<e ndl;cout<<setw(6)<<""<<"T欢迎进入万年历的页面丁"<<endl;cout<<setw(6)<<""<<"TTT"<<endl;cout<<setw(6)<<""<<"TT"<<endl;cout&

12、lt;<setw(6)<<""<<"TTT"<<endl;cout<<setw(6)<<""<<"JLT丁"<<endl;cout<<setw(6)<<""<<"T+"<<e ndl;cout<<setw(6)<<""<<"TT*该万年历有以下功能供您选择 *TT"

13、<<endl;cout<<setw(6)<<""<<"T+"<<e ndl;cout<<setw(6)<<""<<"TT* *1.万年历的时间校对系统* *<7 U TT"<<endl;cout<<setw(6)<<"HnS JL"<<" T *.2. 万 年历 的查 询系统 *T "<<endl;cout<&

14、lt;setw(6)<<"hnS JL"<<" T* *.0. 退出*T "<<endl;cout<<setw(6)<<""<<"JLT.TT"<<endl;cout<<setw(6)<<""<<"=WvWvW WvWvW="<<endl;cout<<endl<<endl;cout<<" 请输入您要使

15、用的功能的序号(回车键确认) : cout<<endl;int n;cin>>n;switch(n)case 1:yi();break;case 2:er();break;case 0:exit(0);system("pause");system("cls");return 0;class Clockpublic:Clock(int a=0,int b=0,int c=0):hour(a),minute(b),second(c)void settime(int a=0,int b=0,int c=0)hour=a;minute=b

16、;second=c;void showtime()cout<<" 现在时间为 "<<hour<<":"<<minute<<":"<<second<<endl; cout<<endl<<endl;void operator+(int)second+;if(second=60)second=0;minute+;if(minute=60)minute=0;hour+;if(hour=24)hour=0;void operator-(i

17、nt)second-;if(second=-1)second=59;minute-;if(minute=-1)minute=59;hour-;if(hour=-1)hour=23;private:int hour,minute,second;int yi()char a,i,temp30;int b,c,d,shi,fen,miao;time_t t;time(&t);struct tm *now; /tm结构体,得到本地时间now=localtime(&t);strftime(temp,30,"%H",now);istringstream input3(t

18、emp);input3>>shi;strftime(temp,30,"%M",now);istringstream input4(temp); input4>>fen;strftime(temp,30,"%S",now);istringstream input5(temp);input5>>miao;Clock myclock;myclock.settime(shi,fen,miao);myclock.showtime();<<endl;m,否cout<<" 是否要调整时间, 如果是请

19、按 y ,按其他键继续返回主菜单 cout<<endl;a=getch();if(a='y')cout<<" 请问是要将现在的时间调大还是调小,如果调大请按 者按 n"<<endl;doi=getch();if(i='m')cout<<" 请输入要增加小时的个数: " cout<<endl;cin>>b; b*=3600;while(b-) myclock+;cout<<" 请输入要增加分钟的个数: " cout<

20、<endl;cin>>c;c*=60;while(c-) myclock+;cout<<" 请输入要增加的秒数: "cout<<endl;cin>>d;while(d-) myclock+;myclock.showtime(); system("pause");请输入要减少小时的个数:else if(i='n') cout<<" cin>>b; b*=3600;while(b-) myclock-; cout<<" 请输入要减少分

21、钟的个数: cin>>c; c*=60;while(c-) myclock-; cout<<" 请输入要减少的秒数: " cin>>d;while(d-) myclock-; myclock.showtime(); system("pause");else cout<<" 输入有误,请重新输入 "<<endl; while(i!='m'&&i!='n'); jiemian();int er()cout<<jiemia

22、n1()<<endl;cout<<jiemian2()<<endl;int main()cout<<jiemian()<<endl;/ 调用 jiemian 函数,到达页面 cout<<yi()<<endl;system("cls");return 0;int jiemian1()cout<<endl<<endl; cout<<setw(6)<<"cout<<setw(6)<<""<&

23、lt;endl;<<*cout<<setw(6)<<"+"<<e ndl;cout<<setw(6)vv" "<<"十 统十"<<e ndl;)现在进入万年历查询系系cout<<setw(6)<<"+"<<e ndl;cout<<setw(6)vv" "<<"+择+ "<<e ndl;cout<<setw(6)

24、<<"+"<<e ndl;cout<<setw(6)<<"II该系统有以下功能可供您选<<""<<endl;cout<<endl<<endl<<endl; system("pause");system("cls");jiemian2();int jiemian2()cout<<endl;cout<<setw(6)<<" endl;"<&l

25、t;cout<<setw(6)<<"cout<<setw(6)<<"JL"<<"十十"<<e ndl;cout<<setw(6)<<" "<<" 十系统功能十"<<e ndl;cout<<setw(6)<<" "<<" 十1、查询某年的日历十"<<e ndl;cout<<setw(6)&l

26、t;<" "<<" 十2 、查询某年某月的日历十"<<e ndl;cout<<setw(6)<<" "<<" 十3 、查询某年某月某日的信息十"<<e ndl;cout<<setw(6)<<" "<<" 十4、 返 回十"<<e ndl;cout<<setw(6)<<"JL"<<"十II

27、<<"endl;十"<<e ndl;cout<<setw(6)<<"endl;cout<<endl<<endl;cout<<" 请输入您要使用的功能的序号(回车键确认) : int n;docin>>n;switch(n)case 1:first();break;case 2:second();break;case 3:third();break;case 4:jiemian();break;default:cout<<" 输入有误,请重

28、新输入 "<<endl;while(n<1|n>4);cout<<endl;cout<<" 点任意键返回主菜单 "<<endl; system("pause");system("cls");jiemian();int first()/ 第一个功能,查询某年的日历ofstream outfile(" 第一个功能 .txt",ios:out);/ 定义一个文件输出流对 象 outfileint y,m,w,d,i,day;cout<<&q

29、uot; 请输入你要查询的年份: "cin>>y;cout<<" 您所查询的内容如下: "<<endl;cout<<endl;cout<<y<<" 年日历 "<<endl;other(y); outfile<<y<<" 年日历 "<<endl;/ 输出到文件 for(m=1;m<=12;m+)int m1,y1;/m1 是指月份,y1是指年份m1=m;y1=y;if(m1<3)m1+=12;y1

30、-;/ 在蔡勒公式中,某年的 1, 2月份要看作是上一年的 13,14 月份 w=week(y1,m1);调用week函数,计算该月一号是星期几 day=md(y,m);调用md函数,判断该月有多少天 printmonth(m);/ 调用 printmonth 函数,输出月份表头 outfilevvmvv"月份"<<endl; 输出到文件printweek();/ 输出每周表头outfile<<setw(8)<<"Sun"<<setw(8)<<"Mon"<<set

31、w(8)<<"Tues"<<setw(8) <<"Wed"<< setw(8)<<"Thurs"<<setw(8)<<"Fri"<<setw(8)<<"Sat"<<endl;/ 输出到文件for(i=0;i<w;i+)若每个月前面有空的,将其空出,w是指每个月的1号是星期几cout<<setw(8)<<" "输出到文件输出该月的

32、日历,输出到文件day 是指每个月的天数outfile<<setw(8)<<" "/ for(d=1;d<=day;d+,w+)/ cout<<setw(8)<<d;outfile<<setw(8)<<d;/ if(w%7=6)cout<<endl;outfile<<endl;/ 输出到文件cout<<endl;cout<<endl;cout<<endl;outfile<<endl<<endl<<end

33、l;/ 输出到文件outfile.close();/ 关闭文件return 0;int second()/ 第二个功能查询某年某月的日历ofstream outfile(" 第二个功能 .txt",ios:out); int y,m,w,d,i,day;cout<<" 请输入你要查询的年月: " cin>>y>>m;cout<<" 您所查询的内容如下: "<<endl;w=week(y,m);day=md(y,m);cout<<endl;other(y);cout

34、vvyvv"年"vvmvv"月"<<endl;outfilevvyvv"年"<<m<<"月 "<<e ndl;printweek();outfile<<setw(8)<<"Sun"<<setw(8)<<"Mon"<<setw(8)<<"Tues"<<setw(8) <<"Wed"<<

35、 setw(8)<<"Thurs"<<setw(8)<<"Fri"<<setw(8)<<"Sat"<<endl;/ 输出到文件for(i=0;i<w;i+)cout<<setw(8)<<" "outfile<<setw(8)<<" "for(d=1;d<=day;d+,w+)cout<<setw(8)<<d;outfile<<se

36、tw(8)<<d;if(w%7=6)/w%7 是判断换行不行的 , 星期日在第一位 , 星期六在最后 一位cout<<endl;outfile<<endl;cout<<endl;outfile<<endl;return 0;void third()/ 第三个功能,查询某年某月某日的信息/cout<<tiangandizhi()<<endl;ofstream outfile(" 第三个功能 .txt",ios:out);int y,m,d,n;cout<<" 请输入您要查

37、询的年月日: "cin>>y>>m>>d;cout<<" 您所查询的内容如下: "<<endl;cout<<endl;n=totaldays(y,m,d);/ 计算指定日期距今天的天数outfilevv"距离今天还有"<<*<"天"<<endl;cout<<endl;outfile<<endl;int month,day;month=(y+y/4+5-42+26*(m+1)/10+d)%7;/ 蔡勒公

38、式,用于计算星期几 switch (m)case 1:switch (d)case 1:cout<<" 这天是 break;break;case 2:switch (d)case 2:cout<<" 这天是 break;case 14:cout<<" 这天是 break;break;case 3:switch (d)case 3:cout<<" 这天是 break;case 5:cout<<" 这天是 break;case 8:cout<<" 这天是 break;

39、case 9:cout<<" 这天是 break;case 12:cout<<" 这天是 break;case 14:cout<<" 这天是 break;case 15:cout<<" 这天是 break;case 21:元旦! "世界湿地日! "情人节! "全国爱耳日! "青年志愿者服务日! "国际妇女节! "保护母亲河日! "中国植树节! " 白色情人节,国际警察日! 世界消费者权益日! "cout<<

40、" break;case 22: cout<<" break;case 23: cout<<" break;case 24: cout<<" break;break;case 4: switch (d) case 1: cout<<" break;case 5: cout<<" break;case 7: cout<<" break;case 22: cout<<" break;case 26: cout<<"

41、 break;break;case 5: switch (d) case 1: cout<<" break;case 3: cout<<" break;case 4: cout<<"这天是 世界水日! "这天是 世界气象日! "这天是 世界防治结核病日! "这天是 愚人节! "这天可能是 清明节! "这天是 世界卫生日! "这天是 世界地球日! "这天是 世界知识产权日! "这天是 国际劳动日!这天是 世界哮喘日!这天是 中国青年节!break;ca

42、se 8:cout<<" 这天是 break;case 12:cout<<" 这天是 break;case 15:cout<<" 这天是 break;case 17:cout<<" 这天是 break;case 20:cout<<" 这天是 break;case 23:cout<<" 这天是 break;case 24:cout<<" 这天是 break;break;case 6:switch (d)case 1:cout<<&q

43、uot; 这天是 break;case 5:cout<<" 这天是 break;case 6:cout<<" 这天是 break;case 17:cout<<" 这天是 break;case 23:cout<<" 这天是 break;case 25:cout<<" 这天是 世界红十字日! "国际护士节! "国际家庭日! "世界电信日! "全国学生营养日! "国际牛奶日! " 世界防治结核病日! "国际儿童节! &qu

44、ot;世界环境日! "全国爱眼日! " 世界防治荒漠化和干旱日! 国际奥林匹克日! "全国土地日! "break; case 26:cout<<" break;break;case 7: switch (d) case 1: cout<<" break;case 7: cout<<" break;case 11: cout<<" break;break;case 8: switch (d) case 1: cout<<" break;case 1

45、2: cout<<" break;break;case 9: switch (d) case 8: cout<<" break;case 10: cout<<" break;case 16: cout<<" break;case 20:这天是 国际禁毒日! "这天是 中国共产党诞生日,国际建筑日! "这天是 中国人民抗日战争纪念日! "这天是 世界人口日! "这天是 中国人民解放军建军节! "这天是 国际青年节! "这天是 国际扫盲日! "

46、;这天是 中国教师节! "这天是中国脑健康日,国际臭氧层保护日。 !cout<<" break;case 21: cout<<" break;case 27: cout<<" break;break;case 10: switch (d) case 1: cout<<" cout<<" break;case 4: cout<<" break;case 5: cout<<" break;case 8: cout<<&quo

47、t; break;case 9: cout<<" break;case 10: cout<<" break;case 14: cout<<" break;case 15: cout<<" break;case 16: cout<<" break;这天是 全国爱牙日!这天是 世界停火日!这天是 世界旅游日!这天是中华人民共和国国庆节,国际音乐节, 国际老年人日! "这天是 世界动物日! "这天是 世界教师日! "这天是 全国高血压日! "这天是 世

48、界邮政日! "这天是 世界精神卫生日! "这天是 世界标准日! "这天是 国际盲人节,世界农村妇女日! "这天是 世界粮食日! "case 17cout<<" 这天是 国际消除贫困日!break;case 24:cout<<" 这天是 break;case 28:cout<<" 这天是 break;case 29:cout<<" 这天是 break;case 31:cout<<" 这天是 break;break;case 11:swit

49、ch (d)case 8:cout<<" 这天是 break;case 9:cout<<" 这天是 break;case 14:cout<<" 这天是 break;case 17:cout<<" 这天是 break;case 25:cout<<" 这天是 break;break;case 12:switch (d)case 1:cout<<" 这天是 break;case 3:cout<<" 这天是 break;联合国日,世界发展新闻日 中国

50、男性健康日! " 国际生物多样性日! " 万圣节! "中国记者节! "消防宣传日! " 世界糖尿病日! " 国际大学生节! "国际消除对妇女的暴力日!世界艾滋病日! " 世界残疾人日! "case 4: cout<<" break;case 9: cout<<" break;case 25: cout<<" break;case 29: cout<<" break; break;这天是 全国法制宣传日!这天是 世界足球

51、日! "这天是 圣诞节! "这天是 国际生物多样性!cout<<endl; outfile<<endl;/int tiangandizhi()/class date/public:/int other(int year,int month,int day);/ 判断本年的干支纪年,生肖,是否闰年。/ private:/bool check();/ 判断是否输入的日期有误,若有则返回 1 ,否则返回 0 /int year;/int month;/int day;/;int leep(int y)/闰年的判断 if(y%4=0&&y%1

52、00!=0)|y%400=0) return 1;elsereturn 0;void other(int year)/ 判断本年的干支纪年,生肖,是否闰年,星期。string tian_gan10="IIstring di_zhi12="IIstring sheng_xiao32="IIstring xing_qi7="甲","乙"," 丙"," 丁 "," 戊 ",辛 "," 壬"," 癸"己",&quo

53、t; 庚",子"," 丑 "," 寅"," 卯"," 辰"," 巳 "," 午", 未"," 申 "," 酉"," 戌"," 亥"鼠"," 牛"," 虎 "," 兔"," 龙"," 蛇", 马"," 羊"," 猴 &qu

54、ot;," 鸡"," 狗"," 猪"日"IIII IIIIII四"," 五 "," 六"int years=year-1924;/1924int n_di_zhi=years%12;/int n_tian_gan=years%10;/甲子年,鼠年, 1 月 1 日是星期二。 获得不够一个地支周期的天数。获得不够一个天干周期的天数。if(n_di_zhi<0)n_di_zhi=12+n_di_zhi;if(n_tian_gan<0)n_tian_gan=10+n_t

55、ian_gan;<<"年 "cout<<setw(12)<<sheng_xiaon_di_zhi<<"年 "if(leep(year) cout<<setw(12)<<" 闰年 "else cout<<setw(12)<<" 平年 "cout<<endl;int week(int y,int m)int w;w=(y+y/4+5-42+26*(m+1)/10+1)%7;return(w);void print

56、week() cout<<setw(8)<<"Sun"<<setw(8)<<"Mon"<<setw(8)<<"Tues"<<setw(8)<<"Wed"<<setw(8) <<"Thurs"<<setw(8)<<"Fri"<<setw(8)<<"Sat"<<endl;void p

57、rintmonth(int m)switch(m)月份"<<e ndl;break; 月份 "<<endl;break; 月份"<<e ndl;break; 月份"<<e ndl;break; 月份"<<e ndl;break; 月份"<<e ndl;break; 月份"<<e ndl;break; 月份"<<e ndl;break; 月份"<<e ndl;break;月份"<<

58、;e ndl;break 月份"<<e ndl;break 月份"<<e ndl;breakcase 1:cout<<"1 case 2:cout<<"2 case 3:cout<<"3 case 4:cout<<"4 case 5:cout<<"5 case 6:cout<<"6 case 7:cout<<"7 case 8:cout<<"8 case 9:cout<&

59、lt;"9 case 10:cout<<"10 case 11:cout<<"11 case 12:cout<<"12 int md(int y,int m)int d;switch(m)case 1:case 3:case 5:case 7:case 8:case 10:case 12:d=31;break;case 4:case 6:case 9:case 11:d=30;break;case 2:if(leep(y)=1)d=29;else d=28;break;return d;/ 以下是第三个功能的函数int

60、 days1(int y,int m,int d)/计算该天是这一年的第几天int i,sum1=0,day; for(i=1;i<m;i+)day=md(y,i); sum1=sum1+day;sum1+=d;return sum1;int days2(int y)int yeardays;if(leep(y)yeardays=366;elseyeardays=365;return yeardays;int days3(int y)/ 计算从元年到该之年前总天数 ( 定义部分 )int i,yeardays;long sum3=0;for(i=1;i<y;i+)yeardays=days2(i); sum3+=yeardays;return sum3;int totaldays(int y,int m,int d)/ 计算指定日期距今天的天数 ( 定义部分 ) int y1,m1,d1;long sum=0,localsum=0,finaldays=0;sum=days1(y,m,d)+days3(y);struct tm *local;time_t t;t=time(NULL);local=localtime(&t); y1=local->tm_year+1900; m1=local->tm_mon+1;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论