C语言课程设计报告.docx

上传人:b****1 文档编号:595572 上传时间:2023-04-29 格式:DOCX 页数:18 大小:29.42KB
下载 相关 举报
C语言课程设计报告.docx_第1页
第1页 / 共18页
C语言课程设计报告.docx_第2页
第2页 / 共18页
C语言课程设计报告.docx_第3页
第3页 / 共18页
C语言课程设计报告.docx_第4页
第4页 / 共18页
C语言课程设计报告.docx_第5页
第5页 / 共18页
C语言课程设计报告.docx_第6页
第6页 / 共18页
C语言课程设计报告.docx_第7页
第7页 / 共18页
C语言课程设计报告.docx_第8页
第8页 / 共18页
C语言课程设计报告.docx_第9页
第9页 / 共18页
C语言课程设计报告.docx_第10页
第10页 / 共18页
C语言课程设计报告.docx_第11页
第11页 / 共18页
C语言课程设计报告.docx_第12页
第12页 / 共18页
C语言课程设计报告.docx_第13页
第13页 / 共18页
C语言课程设计报告.docx_第14页
第14页 / 共18页
C语言课程设计报告.docx_第15页
第15页 / 共18页
C语言课程设计报告.docx_第16页
第16页 / 共18页
C语言课程设计报告.docx_第17页
第17页 / 共18页
C语言课程设计报告.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C语言课程设计报告.docx

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

C语言课程设计报告.docx

C语言课程设计报告

C语言课程设计报告

030910216顾臣风

程序主要功能:

用单向链表结构实现简单的学生成绩管理功能,具有链表建立、链表输出、结点有序插入、结点删除、数据查询等功能。

用户在主菜单界面输入选项,即按照功能列表0-8输入任意数字,回车后执行该功能。

CreateList(建立有序单向链表)

从键盘上输入一个学生的姓名及成绩,以姓名为序建立有序链表。

插入一条记录后,显示提示信息:

确认——继续输入记录;取消——退出输入功能。

DisplayAllRecord(显示所有结点记录)

按顺序显示链表中所有记录,每屏显示10条记录。

每显示10条,按回车继续。

InsertaRecord(插入一条结点记录)

在以姓名为序排列的链表中插入一条记录,插入后,链表仍然有序。

输出成功信息。

DeleteaRecord(按姓名查找,删除一条结点记录)

输入待删除记录的姓名,显示信息确认是否要删除。

确认后删除该记录。

Query

输入姓名,查找记录并显示学生成绩。

AddRecordsfromaTextFile

从纯文本文件添加数据到链表。

WritetoaTextFile

将链表中数据写入文件(纯文本文档),键入文件名后建立该文件。

DeletetheSameRecord

删除相同姓名的记录。

Quit

退出系统并释放动态存储空间。

程序分析及感想

这个学生管理程序主要设计结构体,指针,链表这三章知识。

通过创立动态列表来确定存储空间,根据具体问题申请,极大的节省了空间!

这次课程设计把我学习的结构体,指针,链表整合成一个整体,让我体会到程序设计结构的重要性!

这个学生管理程序中式通过调用函数来实现要求,这样整个程序就显得简单,明了!

易于调试,检验,给编程者很大的方便。

当然作为初学者,在编程时还是遇到一些问题.

比如第六个函数,从文件中整批输入数据,由于对数据文件的使用还不是很熟练,所以请教了同学,并反复研究了书本的例11—7,通过比较分析这两个函数的异同,最后解决了问题!

还有就是附加要求中的删除相同姓名记录,原先考虑到时有序链表,准备只比较相邻两个数据,后来发现到特殊情况,就采用固定其中一个,然后依次比较的方法!

对附加要求中的逆序存放,由于存在一定疑问就没有编写!

通过这次课程设计,我对结构体,指针,链表有了更深的理解。

 

源程序及其注释

#include

#include

#include

#include/*调用库函数*/

structstud/*定义结构体*/

{charName[20];

intScore;

structstud*next;

};

typedefstructstudStudent;

intmenu_select();/*函数说明*/

Student*Create();

voidDisplay(Student*head);

Student*Insert(Student*head,Student*p0);

Student*Insert_a_Record(Student*head);

Student*Delete(Student*head,char*name);

Student*Delete_a_Record(Student*head);

Student*Query(Student*head,char*name);

voidQuery_a_record(Student*head);

Student*AddfromText(Student*head,char*fileame);

voidWritetoText(Student*head,char*fileame);

Student*Delete_the_same_Record(Student*head);

voidQuit(Student*head);

intn=0;

main()/*主函数*/

{charname[20];Student*head=NULL;

for(;;)

{

switch(menu_select())

{case1:

printf("ExecutionofCreateList\n");

head=Create();

system("pause");/*暂停程序执行,按任意键后继续执行*/

break;

case2:

printf("ExecutionofDisplayAllRecord\n");

Display(head);

system("pause");

break;

case3:

printf("ExecutionofInsertaRecord\n");

head=Insert_a_Record(head);

system("pause");

break;

case4:

printf("ExecutionofDeleteaRecord\n");

head=Delete_a_Record(head);

system("pause");

break;

case5:

printf("ExecutionofQuery\n");

Query_a_record(head);

system("pause");

break;

case6:

printf("ExecutionofAddRecordsfromaTexlFile\n");

printf("Pleaseinputthenameofthefile:

\n");

scanf("%s",name);

head=AddfromText(head,name);

system("pause");

break;

case7:

printf("ExecutionofWritetoaTexlFile\n");

printf("Pleaseinputthenameofthefile:

\n");

scanf("%s",name);

WritetoText(head,name);

system("pause");

break;

case8:

printf("ExecutionofDeletethesameRecord\n");

Delete_the_same_Record(head);

system("pause");

break;

case0:

printf("ExecutionofQuit\n");

Quit(head);

system("pause");

exit(0);

}

}

}

intmenu_select()/*显示菜单内容*/

{intc;

do{system("cls");/*清屏*/

printf("1.CreateList\n");

printf("2.DisplayAllRecord\n");

printf("3.InsertaRecord\n");

printf("4.DeleteaRecord\n");

printf("5.Query\n");

printf("6.AddRecordsfromaTexlFile\n");

printf("7.WritetoaTexlFile\n");

printf("8.Deletethesamerecord\n");

printf("0.Quit\n");

printf("Input0-7:

\n");

scanf("%d",&c);

}while(c<0||c>7);

return(c);

}

Student*Create()/*创建链表*/

{Student*p,*head=NULL;/*P为新开辟的空间的首地址*/

charname[20];inta,b;

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

printf("Pleaseinputanumber(yes1,no0):

\n");

scanf("%d",&a);

while(a==1)/*控制数据的输入*/

{

printf("Pleaseinputstudent'snameandgrade:

\n");

scanf("%s%d",name,&b);/*输入人名,成绩*/

p=(Student*)malloc(sizeof(Student));/*申请空间*/

strcpy(p->Name,name);

p->Score=b;

head=Insert(head,p);/*调用结点的插入函数*/

printf("Pleaseinputanotherstudent'sdata:

yes1no0");

scanf("%d",&a);

}

return(head);/*返回头指针*/

}

voidDisplay(Student*head)/*输出链表数据*/

{

intn=1;

while(head!

=NULL)/*判断是否输出完毕*/

{

printf("%s",head->Name);

printf("%d\n",head->Score);

if(n++==10)/*每十个暂停一下*/

{

printf("ContinueDisplaying,pressENTER");

system("pause");

system("cls");

n=1;

}

head=head->next;

}

}

Student*Insert(Student*head,Student*p)/*有序插入指针s所对应的结点到链表*/

{

Student*p1,*p2;

if(head==NULL)/*判断链表是否为空*/

{

head=p;

p->next=NULL;

return(head);

}

p2=p1=head;

while(strcmp(p->Name,p1->Name)>0&&p1->next!

=NULL)/*寻找插入点*/

{

p2=p1;p1=p1->next;

}

if(strcmp(p->Name,p1->Name)<=0)/*插在p1之前*/

{

p->next=p1;

if(head==p1)head=p;/*插在链表首部*/

elsep2->next=p;/*插在链表中央*/

}

else/*插在链表尾结点之后*/

{

p1->next=p;

p->next=NULL;

}

return(head);/*返回头指针*/

}

Student*Insert_a_Record(Student*head)/*有序插入数据*/

{

Student*p;

charb[20];ints;

printf("Pleaseinputastudent'sdata\n");

scanf("%s%d",b,&s);

p=(Student*)malloc(sizeof(Student));/*申请空间*/

strcpy(p->Name,b);

p->Score=s;

head=Insert(head,p);/*调用Insert函数插入*/

printf("Therecordhasbeeninsertedsuccessfully\n");

returnhead;/*返回头指针*/

}

Student*Delete(Student*head,char*name)/*删除姓名为name的记录*/

{Student*p1,*p2;/*定义两个指针变量*/

if(head==NULL)/*判断链表是否为空*/

{printf("Listisnull.\n");

returnNULL;

}

p1=head;

while(strcmp(p1->Name,name)!

=0&&p1->next!

=NULL)/*查找要删除的记录*/

{p2=p1;

p1=p1->next;

}

if(strcmp(p1->Name,name)==0)/*删除符合条件的记录*/

{if(p1==head)

head=p1->next;

else

p2->next=p1->next;

printf("Thestudent%sisdeleted.\n",p1->Name);

free(p1);/*释放被删除记录的空间*/

}

Else/*没有找到符合条件的记录*/

printf("Student%sisnotfound.\n",name);

returnhead;/*返回头指针*/

}

Student*Delete_a_Record(Student*head)

{charname[20];/*定义一个字符型数组存放姓名*/

Student*p;

p=(Student*)malloc(sizeof(Student));/*申请空间*/

scanf("%s",name);

strcpy(p->Name,name);

head=Delete(head,name);/*调用结点删除函数*/

printf("Successfuldelete");

returnhead;/*返回链表头指针*/

}

Student*Query(Student*head,char*name)/*查询一条记录*/

{Student*p;

p=head;

while(strcmp(p->Name,name)!

=0&&p->next!

=NULL)p=p->next;

if(strcmp(p->Name,name)==0)returnp;/*如果有要查找的结点,P为该结点的地址*/

if(p->next==NULL)

returnNULL;/*不存在返回NULL*/

}

voidQuery_a_record(Student*head)

{Student*p;

charname[20];

scanf("%s",name);/*输入要查找的姓名*/

p=Query(head,name);/*调用结点查询函数查找*/

if(p!

=NULL)/*判断是否有符合条件的记录*/

{printf("Successfulquery");

printf("%s%d",p->Name,p->Score);/*输出记录*/

}

else

printf("failedquery");/*若返回值为NULL,则没有符合条件记录*/

}

Student*AddfromText(Student*head,char*filename)/*从文件输入数据*/

{FILE*fp;Student*p;/*p为新开辟结点的地址*/

fp=fopen(filename,"r");/*以读的方式打开文件*/

while(!

feof(fp))/*判断文件是否到达结尾处*/

{

p=(Student*)malloc((sizeof(Student)));/*申请空间*/

fscanf(fp,"%s\t%d",p->Name,&p->Score);

head=Insert(head,p);/*调用插入函数将文件中数据插入链表*/

}

printf("Addsuccess!

\n");

fclose(fp);/*关闭文件*/

returnhead;/*返回头指针*/

}

voidWritetoText(Student*head,char*filename)/*将链表记录写入文件*/

{FILE*fp;Student*p;

fp=fopen(filename,"w”);

p=head;

if(p!

=NULL)

{fprintf(fp,"%s%f\n",p->Name,p->Score);/*输出记录*/

p=p->next;

}

fclose(fp);/*关闭文件*/

}

Student*Delete_the_same_Record(Student*head)/*删除相同姓名的记录*/

{Student*p,*q,*r;

p=head;

if(p!

=NULL)

{while(p->next!

=NULL)

{q=p;

while(q->next!

=NULL)

{if(strcmp(p->Name,q->next->Name)==0)/*判断是否是相同姓名*/

{r=q->next;/*将要删除的记录地址赋给指针r*/

q->next=q->next->next;

free(r);/*释放相同记录的空间*/

}

elseq=q->next;

}

p=p->next;

}

}

}

voidQuit(Student*head)/*退出管理系统*/

{Student*p;

while(head!

=NULL)

{p=head;

head=head->next;

free(p);/*释放动态存储空间*/

}

}

函数调用关系图

管理程序

退出管理系统

删除同名记录

记录写入文件

从文件输入数据

结点数据查询

结点删除

结点有序插入

输出链表数据

创建有序链表

Insert函数

Query函数

Delete函数

Insert函数

Main函数:

使用for和switch语句控制调用哪个函数;

menu_select函数:

用do-while语句输出菜单;

Create函数:

使用while语句控制是否输入;

Display函数:

使用while函数输出各结点;

Insert函数:

用if函数判断链表是否为空;用while函数确定插入的位置;

Delete函数:

用while函数确定要删除的结点位置;

Query函数:

用while函数依次比较各结点是否与要查询的记录相符合;

AddfromText函数:

feof判断是否到达文件结尾,没有继续输出记录;

WritetoText函数:

fopen打开文件,if判断是否到达NULL;

Delete_the_same_Record函数:

if函数用来固定其中一个地址,然后依次比较,用两个while函数比较固定的结点与后面的结点;

测试流程

1:

输入“1“,输出“ExecutionofCreateList

Createanincreasinglist...

Pleaseinputanumber(yes1,no0)“

创建链表;

步骤:

输入“1“,cheng80ENTER;

输入“1“,xiao90ENTER;

输入“1“,yu98ENTER;

输入“0“结束输入;

2:

输入“2”

输出:

ExecutionofDisplayAllRecord

cheng80

xiao90

yu98;

3:

输入“3”

输出“ExecutionofInsertaRecord

Pleaseinputastudent'sdata”;

输入“guyan99”;

输出“Therecordhasbeeninsertedsuccessfully“;

4:

输入“4“

输出“ExecutionofDeleteaRecord“

输入“xiao”;

输出“Successfuldelete”;

5:

输入“5”

输出“ExecutionofQuery“

输入“yu”;

输出“Successfulquery;

Yu98”;

6:

输入“6”

输出“ExecutionofAddRecordsfromaTexlFile

Pleaseinputthenameofthefile“

输入“data.txt”

输出“Addsuccess!

7:

输入“2”

输出“ExecutionofDisplayAllRecord

a20

b25

c30

cheng80

d35

e40

f45

g50

guyan99

h55

i60

l65

yu98;

8:

输入“7“

输出“ExecutionofWritetoaTexlFile

Pleaseinputthenameofthefile:

输入“records.txt”;

9:

输入“0“

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

当前位置:首页 > 总结汇报 > 学习总结

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

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