C语言程序设计课程设计Word格式.docx
《C语言程序设计课程设计Word格式.docx》由会员分享,可在线阅读,更多相关《C语言程序设计课程设计Word格式.docx(57页珍藏版)》请在冰豆网上搜索。
数据格式:
测试数据,以ASCII码文件studf.txt提供。
studf.txt文件结构:
学号姓名性别年月日数学英语C总分
studf.txt文件数据:
20015101张明男198210216072760
20015103李红英女198382510096890
20015102成杰男198311209570870
20015106许民光男19841267887740
20015104陈富来男198312158674920
20015107成杰男19841108478900
插入数据以ASCII码文件istudf.txt提供。
istudf.txt文件结构:
20015112成杰男198311209570870
20015109许民光男19841267887740
20015111陈富来男198312158674920_
⑵程序功能1)建立按学号递增的单向链表数据从ASCII码文件studf.txt读入2)显示学生信息3)插入若干学生 插入数据从ASCII码文件istudf.txt读入4)删除若干名学生 按学号查找需删除的学生信息5)按学号查学生分数和平均分数6)按姓名查学生分数和平均分数7)按总分查学生分数和平均分数8)查各门课程平均分9)查学生平均年龄10)查男女学生人数11)显示学生信息表
.程序总体设计
数据结构依据给定的学生信息和数据格式,链表结点必须用结构实现。
结构类型的层次结构:
structstudent学号字符串charno[9]姓名字符串charname[9](汉字)性别字符串charsex[3](汉字)出生日期日期结构structdate年intyear月intmonth日intday三门课成绩和总分整型数组intscore[4]结构类型定义为全局标识符:
structdate/*日期结构*/{intyear;
/*年*/charmonth;
/*月*/charday;
/*日*/};
structstudent/*学生信息结构*/{charno[9];
/*学号*/charname[9];
/*姓名*/charsex[3];
/*性别*/structdatebirthday;
/*出生日期*/intscore[4];
/*三门课成绩和总分*/};
⑵模块划分依据程序的数据结构和功能,遵照“自顶向下”原则,采用基于函数的逐步求精法,描述该程
序的层次结构。
图1显示出该程序的层次结构,共三层。
图1程序的层次结构
函数原形清单:
11建立学生信息单向链表createstructstudent*create(structstudent*head,int*n)12按学号链表递增排序sort_lstructstudent*sort_l(structstudent*head)13插入学生信息structstudent*insert(structstudent*head,int*n)14按学号删除学生信息deletestructstudent*delete(structstudent*head,int*n)15按学号查学生信息和平均成绩srch_novoidsrch_no(structstudent*head)16按姓名查学生信息和平均成绩srch_namevoidsrch_name(structstudent*head)
17按总分查学生信息和平均成绩srch_tscore
voidsrch_tscore(structstudent*head)
18查各门课的平均分数find_ave
voidfind_ave(structstudent*head,intn)
19查学生的平均年龄find_age
voidfind_age(structstudent*head,intn)
1A查男女学生的人数find_pnum
voidfind_pnum(structstudent*head)
1B显示学生信息表printtable
voidprinttable(structstudent*head)
21显示学生信息printsi
voidprintsi(structstudent*head)
22显示插入或删除后的学生信息print_inf
voidprint_inf(structstudent*head)
⑶程序总体框架
模块层次结构,只确定了模块之间的关系和函数原型,不是程序的执行步骤。
程序总体框架是该程序的总体流程图。
该程序不是顺序连续地执行全部功能,而是在某一时刻有选择地执行一种或多种功能。
因此,选用菜单方式是较佳的方案。
程序总体框架如图2所示。
图2程序总体框架
程序组织
程序采用多源程序文件组织,由hstud_1.c主控源程序文件和stfun_1.c函数源程序文件组成。
三.功能块函数设计和调试
建立学生信息单向链表
函数原型、功能和形参说明
函数原型:
structstudent*create(structstudent*head,int*n)
函数功能:
参数说明:
structstudent*head链表头结点指针
int*n学生人数指针
程序清单
{
FILE*fp;
structstudent*p,*p1,*p2;
if((fp=fopen("
studf.txt"
"
r"
))==NULL)
{
printf("
不能打开studf.txt文件!
\n"
);
exit
(1);
}
while(!
feof(fp))
(*n)++;
p=(structstudent*)malloc(sizeof(structstudent));
fscanf(fp,"
%s%s%s%d%d%d%d%d%d%d"
p->
no,p->
name,p->
sex,
&
p->
birthday.year,&
birthday.month,
birthday.day,
score[0],&
score[1],&
score[2],
score[3]);
score[3]=p->
score[0]+p->
score[1]+p->
score[2];
if(head==NULL)
{
head=p;
p1=p;
}
else
p1->
next=p;
p2=p1;
p2->
next=NULL;
free(p);
(*n)--;
fclose(fp);
returnhead;
}
运行结果
按学号链表递增排序
structstudent*sort_l(structstudent*head)
structstudent*phead,*p1,*p2,*p;
/*建立辅助结点*/
phead=(structstudent*)malloc(sizeof(structstudent));
phead->
next=head;
head=head->
next;
next->
/*排序*/
while(head!
=NULL)
p2=phead;
p1=phead->
p=head;
head=head->
p->
while(strcmp(p->
no,p1->
no)>
0&
&
p1!
p1=p1->
if(p1!
p->
next=p1;
p2->
head=phead->
free(phead);
插入学生信息
structstudent*insert(structstudent*head,int*n)
在已建立的链表中插入学生信息
structstudent*p2,*p1,*p,*p3;
inti;
clrscr();
printf("
插入前学生信息:
print_inf(head);
istudf.txt"
&
p3=head;
i=0;
while(p3!
{
if(strcmp(p->
no,p3->
no)==0)
{
printf("
%s该学生的记录已插入\n"
p->
name);
i++;
}
p3=p3->
}
if(i!
=0)
continue;
if(head==NULL)
p1=head;
while(p1!
=NULL&
strcmp(p->
p2=p1;
p1=p1->
if(p1==head)
head=p;
elseif(p1!
p2->
else
插入后的学生信息:
按学号删除学生信息
structstudent*delete(structstudent*head,int*n)
在已建立的链表中按学号删除学生信息
charno[9];
structstudent*p1=NULL,*p2;
学生信息:
输入删除学生的学号(以ctrl+z结束):
scanf("
%s"
no);
if(strcmp(no,"
exit"
)==0)
returnhead;
feof(stdin))
printf("
空表!
returnhead;
strcmp(p1->
no,no)!
if(p1==NULL)
printf("
\n%s无此学号学生!
elseif(p1==head)/*删除头节点*/
head=p1->
free(p1);
(*n)--;
elseif(p1->
next!
=NULL)/*删除中间节点*/
next=p1->
else/*删除尾节点*/
scanf("
p1=head;
删除后的学生信息:
按学号查学生信息和平均成绩
voidsrch_no(structstudent*head)
函数流程图
flagfor循环变量pwhile循环变量
intfound,flag;
charno[9],ch;
structstudent*p;
for(flag=1;
flag;
)
found=0;
输入查找学生的学号:
"
while(p!
if(strcmp(p->
no,no)==0)
if(found==0)
学号姓名性别年月日数学英语C总分平均分数\n"
%-8s%-8s%-2s%4d%2d%2d%4d%4d%4d%5d%6.1f\n"
p->
birthday.year,p->
score[0],p->
score[1],
score[2],p->
score[3],
score[3]/3.0);
found=1;
p=p->
if(found==0)
姓名:
%s未找到!
继续查找[Y/N]:
getchar();
ch=getchar();
if((ch=='
N'
)||(ch=='
n'
))
flag=0;
按姓名查学生信息和平均成绩
voidsrch_name(structstudent*head)
charNAME[9],ch;
p=head;
{
输入查找学生的姓名:
NAME);
while(p!
name,NAME)==0)
按总分查学生信息和平均成绩
voidsrch_tscore(structstudent*head)
intfound,SCORE,flag;
charch;
found=0,SCORE=0;
输入查找学生的总分:
%d"
&
SCORE);
if(SCORE==p->
score[3])
found=1;
if(foun