logo资料库

c语言课程设计报告书万年历.doc

第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
资料共9页,剩余部分请下载后查看
c语言课程设计报告书
c 语言课程设计报告书 电子万年历及记事本 一 设计目的 本程序旨在呈现一个由公元元年开始的电子万年历。该系统在初始时能够根 据系统当前时间显示当前年月情况,包括日期,星期,月份,年份。并可由用户 通过直接输入年份和月份重复查找指定月份的详细情况。此外,用户可根据需要 在日历的任意日期内写入文字作为待办事项或纪录并在需要时可以通过调用已 保存的文件察看或对其进行修改。本程序的目的在于帮助使用者方便地查询当前 日期并能够较好的管理和规划时间,提高工作效率。 二 程序组成图 (上层图) 万年历 日期显示函数 文挡编译函数 总日期计算函数 月份显示函数 文字输入函数 文本调用输出函数 三 函数流程图 1 日期显示函数 用户 输入年月 计算 输出 月份图 重新输入年月
2 文档编译函数 用户 选择日期 输入文本 调用文本输入函数 保存 调用\修改 四 详细设计 主要函数说明及调用 日期显示函数(主函数)为一个循环体,以便多次查找日期。在首次执行 循环时调用系统时间函数输出当前日期为默认值并调用月份显示函数(月份对应 名称)显示指定月份并输出月历。而后将用户指定日期传递给日期计算函数(判 别闰年并从公元 1 年开始叠加)计算从元年至当前年份总天数。并对给定年份是 否为润年进行判定,显示月历。 文档输入、读取函数于主函数中调用,由用户在主函数给出的日历内选取 日期并调用输入函数并可以在其中写入文档并在关闭程序时储存在 DATA 文件 中。此后,当用户需要显示文档时系统能够通过调用硬盘中的文档显示文本,并 可由用户修改文档并再次储存于硬盘中。 五 调试与测试 调试过程主要是根据程序的设计目的带入数据以检验程序的功能完整性和 程序稳定性。 经过测试,数据完全符合预期,程序运行基本稳定。但是在文件输入过程中 有一定几率自动跳过第一步的操作,以至于无法写入日记。经过反复尝试我通过 添加了让用户在输入日记前键入回车的方法解决了这个问题。并在测试后改善了 文件显示时无法分行显示的问题,使得日记更加美观。 六 程序清单及注释 #include #include #include #include #include int IsLeapYear(int); int month_vis(int month,int year); int clendar(void); void filewright(FILE *fp); void fileread(FILE *fp); int i; int day; int year;
int temp; int month; int temp_i; long int Year_days = 0; int Year_Start = 1; int Per_Year_Days; int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29}; /*每月日期数组*/ struct tm *aa; FILE *fp; struct logdata{ long logid; char logdate[100];}; void main() {textbackground(7); textcolor(0); clrscr(); clendar(); getch(); } /*主函数*/ /*设置程序背景*/ int clendar(void) /*日历输出函数*/ { char choice; int daynum; time_t t; time(&t); aa=gmtime(&t); year=aa->tm_year+1900; month=aa->tm_mon+1; day=aa->tm_mday; do { Year_days = 0; Year_Start = 1; month_vis(month,year); /*调用系统当前日期*/ /*循环输出日历*/ printf("Mon Tue Wed Thu Fri Sat Sun\n"); while(Year_Start < year) { if( IsLeapYear( Year_Start ) ) Per_Year_Days = 366; else
Per_Year_Days = 365; Year_days = Year_days + Per_Year_Days; Year_Start++; } for( temp=1; temp<=12; temp++ ) { if(temp==month){ i = Year_days % 7; if( i != 0 ) for( temp_i = 0; temp_i < i; temp_i++) printf(" "); } /*输入起点空格*/ day = 1; if( IsLeapYear(year) && temp== 2) /*二月的情况*/ while( day <= month_day[12] ) { if(temp==month) { if( day >1 ) if( Year_days % 7 == 0 ) printf("\n"); if( day >= 10 ) printf("%d ",day); else printf("%d ",day); } Year_days++; day++; } else while (day <= month_day[temp-1]) /*非二月*/ { if(temp==month) { if( day > 1 ) if( Year_days % 7 == 0 ) printf("\n"); if( day >=10 ) printf("%d ",day); else printf("%d ",day); } Year_days++;
day++; } } printf("\n"); sign1:printf(" Input '1' to input the year and month\n Input '2' to wright note\n Input '3' to read the note\n Other to exit\n"); /*菜单*/ choice=getch(); if(choice!='1'&&choice!='2'&&choice!='3') exit(0); else if(choice=='2') { } clrscr(); filewright(fp); goto sign1; else if(choice=='3') { } clrscr(); fileread(fp); goto sign1; clrscr(); /*调用写文件函数*/ /*调用读文件函数*/ printf("Please enter year and month: "); scanf("%d%d",&year,&month); if(year<=0||month<=0) { if(year<=0&&month>=0) { } printf("The year is wrong! \n"); if(year>=0&&month<=0) { } printf("The month is wrong! \n"); if(year<=0&&month<=0) printf("The year and month are wrong! \n"); goto sign1; }
}while(1); } int month_vis(int month,int year) /*月份显示函数*/ { switch( month ) { case 1: printf(" January(%d)\n",year); break; case 2: printf(" February(%d)\n",year); break; case 3: printf(" March(%d)\n",year); break; case 4: printf(" April(%d)\n",year); break; case 5: printf(" May(%d)\n",year); break; case 6: printf(" June(%d)\n",year); break; case 7: printf(" July(%d)\n",year); break; case 8: printf(" August(%d)\n",year); break; case 9: printf(" September(%d)\n",year); break; case 10: printf(" October(%d)\n",year); break; case 11: printf(" November(%d)\n",year); break;
case 12: printf(" December(%d)\n",year); break; } return 0; } int IsLeapYear( int year ) /*计算是否为闰年*/ { if ((year %4 == 0) && (year % 100 != 0) ||(year % 400 == 0) ) return 1; else return 0; } void filewright(FILE *fp) /*写日记函数*/ { char a[]={""},note[80]={""},date[20]={""},space[]={" "},sign[]={": "}; if((fp=fopen("data.txt","a+"))==NULL){ printf("\nFile can not open!"); exit(0); } printf("Now you can input the note\nPress enter\n"); gets(a); clrscr(); /*防止可能出现的无法输入日记的 bug*/ printf("Input the date : "); gets(date); printf("Input the note : \n"); gets(note); fputc('\n',fp); fputs(date,fp); fputs(sign,fp); fputs(note,fp); fputs(space,fp); fclose(fp); printf("Press any key to go back:\n"); getch(); clrscr(); }
void fileread(FILE *fp) /*读日记函数*/ { char s[800]={""}; if((fp=fopen("data.txt","a+"))==NULL){ printf("\nFile can not open!"); exit(0); } printf("Your note:\n"); while(!feof(fp)) { fgets(s,800,fp); puts(s); } fclose(fp); printf("Press any key to go back:\n"); getch(); clrscr(); } 七 运行结果 主菜单及当前日历: 写日记过程:
分享到:
收藏