C语言课程设计示例.docx

上传人:b****4 文档编号:6010375 上传时间:2023-05-09 格式:DOCX 页数:22 大小:95.04KB
下载 相关 举报
C语言课程设计示例.docx_第1页
第1页 / 共22页
C语言课程设计示例.docx_第2页
第2页 / 共22页
C语言课程设计示例.docx_第3页
第3页 / 共22页
C语言课程设计示例.docx_第4页
第4页 / 共22页
C语言课程设计示例.docx_第5页
第5页 / 共22页
C语言课程设计示例.docx_第6页
第6页 / 共22页
C语言课程设计示例.docx_第7页
第7页 / 共22页
C语言课程设计示例.docx_第8页
第8页 / 共22页
C语言课程设计示例.docx_第9页
第9页 / 共22页
C语言课程设计示例.docx_第10页
第10页 / 共22页
C语言课程设计示例.docx_第11页
第11页 / 共22页
C语言课程设计示例.docx_第12页
第12页 / 共22页
C语言课程设计示例.docx_第13页
第13页 / 共22页
C语言课程设计示例.docx_第14页
第14页 / 共22页
C语言课程设计示例.docx_第15页
第15页 / 共22页
C语言课程设计示例.docx_第16页
第16页 / 共22页
C语言课程设计示例.docx_第17页
第17页 / 共22页
C语言课程设计示例.docx_第18页
第18页 / 共22页
C语言课程设计示例.docx_第19页
第19页 / 共22页
C语言课程设计示例.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C语言课程设计示例.docx

《C语言课程设计示例.docx》由会员分享,可在线阅读,更多相关《C语言课程设计示例.docx(22页珍藏版)》请在冰点文库上搜索。

C语言课程设计示例.docx

C语言课程设计示例

课程设计示例

程设计的目的是将课本上的理论知识和实际运用有机的结合起来,锻炼学生运用所学知识分析解决实际问题的能力。

在设计过程中,要求学生养成良好的编程习惯,学会分析简单的实际问题,并能利用所学的知识建立系统的逻辑结构。

本章以职工信息管理系统为例,阐述了程序开发的一般流程,以起到抛砖引玉的作用。

1需求分析

1.1编写目的

通过对用户需求进行调查分析,写出需求分析文档。

需求分析文档可作为项目设计的基本准则要求,也可作为系统分析员进行系统分析和测试人员进行测试时的手册。

1.2需求概述

设计一个职工信息管理系统,使之能提供以下功能:

(1)职工信息录入功能

(2)职工信息浏览功能

(3)职工信息查询(或排序)功能

(4)职工信息删除功能

(5)职工信息修改功能

1.3需求说明

(1)职工信息包括职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等,并且要求职工号不重复;

(2)录入的职工信息要求用文件形式保存,并可以对其进行浏览、查询、修改、删除等基本操作;

(3)职工信息的显示要求有一定的规范格式;

(4)对职工信息应能够分别按工资及按学历两种方式进行,要求能返回所有符合条件的职工的信息;

(5)对职工信息的修改应逐个地进行,一个职工信息的更改不应影响其他的职工记录;

(6)所设计的系统应以菜单方式工作,应为用户提供清晰的使用提示,依据用户的选择来进行各种处理,并要求在此过程中能够尽可能的兼容用户使用过程中的异常情况。

2总体设计

2.1编写目的

根据需求分析文档,初步提出问题的解决方案,以及软件系统的体系结构和数据结构的设计方案并写出书面文档总体设计说明书,为下一步进行详细设计做准备。

2.2总体设计

1.功能划分

该系统可以按功能进行模块划分,如图1所示:

图1系统模块图

其中:

信息输入模块完成职工信息的输入功能,输入信息包括职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等;

信息浏览模块完成已录入职工信息的显示;

信息查询模块完成职工信息的查询,查询时对应有按工资查询和按学历查询两种方式;

信息修改模块完成职工信息的修改功能;

信息删除模块完成职工信息的删除功能;

菜单选择模块完成用户命令的接收,此模块也是职工信息管理系统的入口,用户所要进行的各种操作均需在此模块中进行选择并进而调用其他模块实现相应的功能。

2.数据结构

本系统中主要的数据结构就是职工的信息,包含职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等,在处理过程中各项可以作为一个职工的不同属性来进行处理。

3.程序流程

系统的执行应从功能菜单的选择开始,依据用户的选择来进行后续的处理,直到用户选择退出系统为止,其间应对用户的选择做出判断及异常处理。

系统的流程图如图2所示:

图2程序流程图

3详细设计

3.1编写目的

根据项目概要设计说明书,在项目概要设计的基础之上,对系统进行详细设计,以便下一步进行程序编码工作。

3.2详细设计

1.数据结构

(1)性别:

enumSex{male,female};

(2)学历:

enumEducation{high,junior,college,master,doctor};

(3)日期:

structDate

{

intyear;

intmonth;

intday;

};

(4)职工信息:

structInfo

{

charnum[5];/*职工号*/

charname[8];/*姓名*/

enumSexsex;/*性别*/

structDatebirthday;/*出生年月*/

enumEducationeducation;/*学历*/

charduty[8];/*职务*/

doublewage;/*工资*/

charaddr[12];/*地址*/

charphone[8];/*电话*/

};

2.各个功能模块的处理流程

对应于总体设计时的系统功能模块图,各个功能模块的处理流程如下:

(1)信息输入模块

打开职工信息文件;

while(继续输入)

{

读入职工信息;

将读入信息添加到职工信息文件中;

提示是否继续输入;

}

关闭职工信息文件;

(2)信息浏览模块

打开职工信息文件;

while(不到文件结束)

{

从文件中顺序读出一个职工的信息;

按指定格式输出该职工的信息;

}

关闭职工信息文件;

(3)信息查询模块

while(继续查询)

{

if(按编号查询)

{

读入查询的职工编号;

打开职工信息文件;

while(不到文件结束)

{

顺序读出一个职工的信息;

if(该职工信息的编号符合条件)

输出该职工的信息;

}

关闭职工信息文件;

提示共有几条符合条件的记录;

}

elseif(按姓名查询)

{

读入查询的职工姓名;

打开职工信息文件;

while(不到文件结束)

{

顺序读出一个职工的信息;

if(该职工信息的姓名符合条件)

输出该职工的信息;

}

关闭职工信息文件;

提示共有几条符合条件的记录;

}

else

提示输入不合适;

提示是否继续查询;

}

(4)信息修改模块

while(继续修改)

{

打开职工信息文件;

打开临时文件;

提示并读入待修改的职工的编号;

while(不到文件结束)

{

顺序读出一个职工的信息;

if(该职工信息的编号符合条件)

将职工的信息进行修改;

将职工的信息写入临时文件中;

}

关闭原信息文件;

关闭临时文件;

删除原信息文件;

将临时文件的名字改为原信息文件的名字;

}

(5)信息删除模块

while(继续删除)

{

提示并读入待删除的职工号;

打开职工信息文件;

查找是否有符合条件的职工记录;

if(有符合条件的记录)

{

创建一个新的临时文件;

while(原信息文件中记录未读完)

{

读出原信息文件中的下一条记录;

if(此条记录不是待删除记录)

写入临时文件中去;

}

关闭原信息文件;

关闭临时文件;

删除原信息文件;

将临时文件的名字改为原信息文件的名字;

}

}

else

提示没有符合条件的记录存在;

}

(6)菜单选择模块:

给出信息提示;

清屏;

绘制菜单(包含输入、显示、查询、修改、删除、退出);

提示菜单选择并读入到变量;

返回变量的值。

4参考程序

#include

#include

#include

#include

enumSex{male,female};/*性别*/

enumEducation{high,junior,college,master,doctor};/*学历*/

structDate/*日期*/

{

intyear;

intmonth;

intday;

};

structInfo

{

charnum[10];/*职工号*/

charname[15];/*姓名*/

enumSexsex;/*性别*/

structDatebirthday;/*出生年月*/

enumEducationeducation;/*学历*/

charduty[15];/*职务*/

doublewage;/*工资*/

charaddr[30];/*地址*/

charphone[15];/*电话*/

};

 

charmenu()/*菜单选择函数*/

{

charn;/*n记录选择的菜单*/

clrscr();/*清屏*/

puts("\t\tWelcometoemployeemanagementsystem");

puts("\t\t*********************MENU********************\n");

puts("\t\t\t\t1.Appendinform\n");

puts("\t\t\t\t2.Displayinform\n");

puts("\t\t\t\t3.Searchinform\n");

puts("\t\t\t\t4.Modifyinform\n");

puts("\t\t\t\t5.Deleteinform\n");

puts("\t\t\t\t6.Exit\n");

puts("\t\t*********************************************\n");

printf("Choiceyournumber(1-6):

[]\b\b");

while

(1)

{

n=getchar();getchar();

if(n<'1'||n>'6')

printf("Inputerror,pleaseinputagain(1-6):

[]\b\b");

else

break;

}

returnn;

}

voidappend()/*信息添加函数*/

{

structInfoinfo;

FILE*fp;

charch;

chartemp[10];

if((fp=fopen("inform.txt","ab"))==NULL)

{

printf("\tCannotopentheinformfile!

");

getch();

exit

(1);

}

do

{

printf("\tnum:

");gets(info.num);

printf("\tname:

");gets(info.name);

printf("\tsex(maleorfemale):

");gets(temp);

if(!

strcmp(temp,"female"))info.sex=female;

elseinfo.sex=male;

printf("\tbirthday(yyyy/mm/dd):

");

scanf("%d/%d/%d",&info.birthday.year,&info.birthday.month,\

&info.birthday.day);

getchar();

printf("\teducation:

");gets(temp);

if(!

strcmp(temp,"doctor"))info.education=doctor;

elseif(!

strcmp(temp,"master"))info.education=master;

elseif(!

strcmp(temp,"college"))info.education=college;

elseif(!

strcmp(temp,"junior"))info.education=junior;

elseinfo.education=high;

printf("\tduty:

");gets(info.duty);

printf("\twage:

");gets(temp);info.wage=atof(temp);

printf("\taddress:

");gets(info.addr);

printf("\tphone:

");gets(info.phone);

fwrite(&info,sizeof(info),1,fp);

printf("\tAnymore?

(Y/N):

[]\b\b");

ch=getchar();getchar();

}while(ch=='Y'||ch=='y');

fclose(fp);

}

voidprint1()

{

printf("%-8s%-10s%-8s%-12s%-15s%-8s%-15s%-15s%-10s\n","nun",\

"name","sex","birthday","education","duty",\

"wage","address","phone");

}

voidprint2(structInfoinfo)

{

printf("%-8s%-10s",info.num,info.name);

if(info.sex==male)printf("%-8s","male");

elseprintf("%-8s","female");

printf("%-4d/%-2d/%-4d",info.birthday.year,\

info.birthday.month,info.birthday.day);

if(info.education==high)printf("%-15s","high");

elseif(info.education==junior)printf("%-15s","junior");

elseif(info.education==college)printf("%-15s","college");

elseif(info.education==master)printf("%-15s","master");

elseprintf("%-15s","doctor");

printf("%-8s%-15.2lf",info.duty,info.wage);

printf("%-15s%-10s\n",info.addr,info.phone);

}

voiddisplay()/*职工信息显示函数*/

{

structInfoinfo;

FILE*fp;

inttotal=0;

if((fp=fopen("inform.txt","rb"))==NULL)

{

printf("\tCannotopentheinformfile!

");

getch();

exit

(1);

}

while(fread(&info,sizeof(info),1,fp)==1)

{

total++;

if(total==1)print1();

print2(info);

if((total!

=0)&&(total%5==0))

{

printf("\n\n\tPressanykeytocontinue......");

getch();

puts("\n\n");

print1();

}

}

fclose(fp);

printf("\n\n\tThereare%drecordinall!

",total);

getch();

}

voidsearch()/*信息查询函数*/

{

structInfoinfo;

FILE*fp;

intflag;/*flag为1按编号查询,flag为2按姓名查询*/

inttotal=0;/*记录符合条件的记录的个数*/

charch[10];

charf;

if((fp=fopen("inform.txt","rb"))==NULL)

{

printf("\tCannotopentheinformfile!

");

getch();

exit

(1);

}

do

{

rewind(fp);

printf("\n\nSearchby(1:

num2:

name):

[]\b\b");

while

(1)

{

scanf("%d",&flag);getchar();

if(flag<1||flag>2)

printf("Inputerror,pleaseinputagain(1:

num2:

name):

[]\b\b");

else

break;

}

if(flag==1)/*按编号进行查询*/

{

printf("Pleaseinputthenumyouwanttosearch:

");

gets(ch);

total=0;/*符合条件的记录数*/

while(fread(&info,sizeof(info),1,fp)==1)

if(strcmp(ch,info.num)==0)

{

total++;

if(total==1)print1();

print2(info);

}

}

else/*按姓名进行查询*/

{

printf("Pleaseinputthenameyouwanttosearch:

");

gets(ch);

total=0;/*符合条件的记录数*/

while(fread(&info,sizeof(info),1,fp)==1)

if(strcmp(ch,info.name)==0)

{

total++;

if(total==1)print1();

print2(info);

}

}

printf("\n\n\tThereare%drecordincluded!

\n",total);

printf("Searchanymore?

(Y/N):

[]\b\b");

f=getchar();getchar();

}while(f=='Y'||f=='y');

fclose(fp);

}

voidmodify()/*信息修改函数*/

{

structInfoinfo;

FILE*fp1,*fp2;

intflag;

charch[10];

charf;

chartemp[10];

do

{

if((fp1=fopen("inform.txt","rb"))==NULL)

{

printf("\tCannotopentheinformfile!

");

getch();

exit

(1);

}

if((fp2=fopen("temp.txt","wb"))==NULL)

{

printf("\tCannotcreatthetempfile!

");

getch();

exit

(1);

}

printf("Pleaseinputthenumyouwanttomodify:

");

gets(ch);

flag=0;

while(fread(&info,sizeof(info),1,fp1)==1)

{

if(strcmp(ch,info.num)==0)

{

print1();

print2(info);

printf("\n\nPleaseinputthenewinformation:

\n");

printf("\tnum:

");gets(info.num);

printf("\tname:

");gets(info.name);

printf("\tsex(maleorfemale):

");gets(temp);

if(!

strcmp(temp,"female"))info.sex=female;

elseinfo.sex=male;

printf("\tbirthday(yyyy/mm/dd):

");

scanf("%d/%d/%d",&info.birthday.year,&info.birthday.month,\

&info.birthday.day);

getchar();

printf("\teducation:

");gets(temp);

if(!

strcmp(temp,"doctor"))info.education=doctor;

elseif(!

strcmp(temp,"master"))info.education=master;

elseif(!

strcmp(temp,"college"))info.education=college;

elseif(!

strcmp(temp,"junior"))info.education=junior;

elseinfo.education=high;

printf("\tduty:

");gets(info.duty);

printf("\twage:

");gets(temp);info.wage=atof(temp);

printf("\taddress:

");gets(info.addr);

printf("\tphone:

");gets(info.phone);

flag=1;

break;

}

fwrite(&info,sizeof(info),1,fp2);

}

fclose(fp1);

fclose(fp2);

if(flag==1)

{

printf("Modifysucess!

\n");

remove("inform.txt");

rename("temp.txt","inform.txt");

}

else

printf("Cannotfindthisrecord!

\n");

printf("Modifyanymore?

(Y/N):

[]\b\b");

f=getchar();getchar();

}while(f=='Y'||f=='y');

}

voiddelete()/*信息删除函数*/

{

structInfoinfo;

FILE*fp1,*fp2;

intflag;

charch[10];

charf;

chartemp[10];

do

{

if((fp1=fopen("inform.txt","rb"))==NULL)

{

printf("\tCannotopentheinformfile!

");

getch();

exit

(1);

}

if((fp2=fopen("temp.txt","wb"))==NULL)

{

printf("\tCannotcreatthetempfile!

");

getch();

exit

(1);

}

printf("Pleaseinputthenumyouwanttode

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2