南京航空航天大学C语言课程设计报告.docx

上传人:b****6 文档编号:13768736 上传时间:2023-06-17 格式:DOCX 页数:18 大小:20.09KB
下载 相关 举报
南京航空航天大学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语言课程设计报告

程序主要功能:

电话薄管理系统要求实现一个电话薄系统的基本管理功能,包括

1.创建电话薄

2.分屏显示电话薄中的所有记录

3.向电话薄中插入一条记录

4.删除一条已经存在的记录项

5.根据用户输入的姓名查找符合条件的记录项

6.从文件读入已有的电话薄

7.将通讯录信息输出到文件

8.逆序存放

9.删除同名记录

#.统计人数

*.按首字母查询

0.退出

分析:

本题以链表为基础,而链表需用指针访问,故链表与指针便贯穿整个程序,成为整个程序最重要的部分。

此外,看似对指针之前所学部分要求不高,但实际却也是此程序得以编写成功必不可少的先决条件。

主要问题及解决方法:

在程序编写过程中遇到了不少问题,特别是编译通过后,却仍有部分功能未能很好实现。

比如:

1与4中对是否继续增加记录和是否删除的控制,程序中虽有scanf语句,但运行时却总是自动跳过,并不执行此语句,经过反复阅读程序、思考,终于发现是前面的scanf语句中输入的回车符被放入了输入缓冲区中,并在下一次scanf语句中被自动输入,因而也就有了scanf语句被跳过的假象。

一个charm;m=getchar();便解决了这一问题。

此外,还有从文件读入记录时,第一条记录总是难以被读入,后来发现原来是因为程序是从head->next开始读入,将之改为head即可。

感想与体会:

编写此程序,花费了不少的时间,但却也让我感受到了编写程序的乐趣,对于我来说,这已经是一种享受。

新增功能:

除了习题册上的附加功能,还增加了统计人数和按首字母查询功能。

源程序:

#include"stdio.h"

#include"string.h"

#include"stdlib.h"

#include"ctype.h"

structstudent

{

charname[10];

charsex[5];

chartelenumber[15];

charemail[30];

structstudent*next;

};

intmenu_select()

{

chari;

do

{system("cls");

printf("1.录入记录\n");

printf("2.显示所有记录\n");

printf("3.插入一条记录\n");

printf("4.删除一条记录\n");

printf("5.查询\n");

printf("6.从文件读入记录\n");

printf("7.写入文件\n");

printf("8.逆序存放\n");

printf("9.删除同名记录\n");

printf("#.统计人数\n");

printf("*.按首字母查询\n");

printf("0.退出\n");

i=getchar();

}while((i<'0'||i>'9')&&i!

='#'&&i!

='*');

return(i-'0');

}

/*录入记录*/

structstudent*Create()

{

structstudent*Insert(structstudent*,structstudent*);

structstudent*p,*head=NULL;

charc='Y',m;

charstu_name[10],stu_sex[5],stu_telenumber[15],stu_email[20];

while(c=='Y'||c=='y')

{

printf("请录入记录:

\n姓名\t性别\t电话\t邮箱\n");

scanf("%s%s%s%s",stu_name,stu_sex,stu_telenumber,stu_email);

p=(structstudent*)malloc(sizeof(structstudent));

strcpy(p->name,stu_name);

strcpy(p->sex,stu_sex);

strcpy(p->telenumber,stu_telenumber);

strcpy(p->email,stu_email);

head=Insert(head,p);

m=getchar();

printf("是否继续录入记录?

(Y/N)\n");

c=getchar();

}

returnhead;

}

/*显示所有记录*/

voidDisplay(structstudent*head)

{

inti;

structstudent*p;

p=head;

printf("输出如下:

\n");

for(i=1;;i++)

{

if(p!

=NULL)

{

printf("%s\t%s\t%s\t%s\n",p->name,p->sex,p->telenumber,p->email);

p=p->next;

}

else

{

printf("输出完毕\n");

break;

}

if(i%10==0)

{

system("pause");

system("cls");

}

}

}

structstudent*Insert(structstudent*head,structstudent*p)

{

structstudent*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)

{

p->next=p1;

if(head==p1)

head=p;

else

p2->next=p;

}

else

{

p1->next=p;

p->next=NULL;

}

return(head);

}

/*插入一条记录*/

structstudent*Insert_a_record(structstudent*head)

{

structstudent*p;

p=(structstudent*)malloc(sizeof(structstudent));

printf("请输入待插入记录:

\n姓名\t性别\t电话\t邮箱\n");

scanf("%s%s%s%s",p->name,p->sex,p->telenumber,p->email);

head=Insert(head,p);

return(head);

}

structstudent*Delete(structstudent*head,chars[10])

{

structstudent*p1,*p2;

if(head==NULL)

{

printf("链表为空\n");

return(NULL);

}

p1=head;

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

=0&&p1->next!

=NULL)

{

p2=p1;

p1=p1->next;

}

if(strcmp(p1->name,s)==0)

{

if(p1==head)

head=p1->next;

else

p2->next=p1->next;

printf("记录%s删除成功!

",s);

}

else

printf("记录%s不存在!

",s);

return(head);

}

/*删除一条记录*/

structstudent*Delete_a_record(structstudent*head)

{

printf("请输入待删除记录的姓名.\n");

charm[10];

scanf("%s",m);

printf("确认删除数据%s?

(Y/N)\n",m);

chari,t;

t=getchar();

i=getchar();

if(i=='Y'||i=='y')

head=Delete(head,m);

else

printf("操作已取消\n");

return(head);

}

structstudent*Query(structstudent*head,chars[10])

{

structstudent*p;

p=head;

while(p!

=NULL)

{

if(strcmp(p->name,s)==0)

{

printf("查找成功:

%s\t%s\t%s\t%s\n",p->name,p->sex,p->telenumber,p->email);

returnp;

}

p=p->next;

}

printf("查找失败:

无此记录\n");

returnNULL;

}

/*查询*/

voidQuery_a_record(structstudent*head)

{

printf("请输入待查找记录的姓名\n");

charm[10];

scanf("%s",m);

Query(head,m);

}

/*从文件读入记录*/

structstudent*AddfromText(structstudent*head)

{

FILE*fp;

fp=fopen("data.txt","r");

if(fp==NULL)

{

printf("无法打开文件!

\n");

exit

(1);

}

charrecord[100];

do

{

structstudent*p;

p=(structstudent*)malloc(sizeof(structstudent));

fscanf(fp,"%s",p->name);

fscanf(fp,"%s",p->sex);

fscanf(fp,"%s",p->telenumber);

fscanf(fp,"%s",p->email);

head=Insert(head,p);

}while(fgets(record,100,fp)!

=NULL);

fclose(fp);

return(head);

}

/*写入文件*/

voidWritetoText(structstudent*head)

{

FILE*fp;

fp=fopen("record.txt","w");

if(fp==NULL)

{

printf("无法打开文件!

\n");

exit

(1);

}

structstudent*p;

p=head;

while(p!

=NULL)

{

fprintf(fp,"%s\t",p->name);

fprintf(fp,"%s\t",p->sex);

fprintf(fp,"%s\t",p->telenumber);

fprintf(fp,"%s",p->email);

fprintf(fp,"\n");

p=p->next;

}

fclose(fp);

}

/*退出*/

voidQuit(structstudent*head)

{

structstudent*p;

while(head!

=NULL)

{

p=head;

head=head->next;

free(p);

}

}

/*逆序存放*/

structstudent*Reverse(structstudent*head)

{

structstudent*newhead=NULL,*p;

while(head!

=NULL)

{

p=head;

head=head->next;

p->next=newhead;

newhead=p;

}

returnnewhead;

}

/*删除同名记录*/

structstudent*DeleteSame(structstudent*head)

{

structstudent*p1,*p2,*p3;

p1=head;

while((p1->next)!

=NULL)

{

p2=p1->next;

p3=p1;

while(p2!

=NULL)

{

if(strcmp(p1->name,p2->name)==0&&strcmp(p1->sex,p2->sex)==0&&strcmp(p1->telenumber,p2->telenumber)==0&&strcmp(p1->email,p2->email)==0)

{

p3->next=p2->next;

return(head);

}

else

p3=p2;

p2=p2->next;

}

p1=p1->next;

}

return(head);

}

/*统计人数*/

voidCount(structstudent*head)

{

structstudent*p;

p=head;

inti;

for(i=0;p!

=NULL;i++)

{

p=p->next;

}

printf("您的电话薄中共有%d个联系人\n",i);

}

/*按首字母查询*/

voidQuery_by_first_letter(structstudent*head)

{

structstudent*p;

p=head;

charc[2];

printf("请输入要查找联系人姓名的首字母!

\n");

scanf("%s",c);

printf("查找如下:

\n");

while(p!

=NULL)

{

if(strncmp(p->name,c,1)==0)

printf("%s\t%s\t%s\t%s\n",p->name,p->sex,p->telenumber,p->email);

p=p->next;

}

}

voidmain()

{

structstudent*head=NULL;

for(;;)

{

switch(menu_select())

{

case1:

head=Create();

system("pause");

break;

case2:

Display(head);

system("pause");

break;

case3:

head=Insert_a_record(head);

system("pause");

break;

case4:

head=Delete_a_record(head);

system("pause");

break;

case5:

Query_a_record(head);

system("pause");

break;

case6:

head=AddfromText(head);

system("pause");

break;

case7:

WritetoText(head);

system("pause");

break;

case8:

head=Reverse(head);

system("pause");

break;

case9:

head=DeleteSame(head);

system("pause");

break;

case-13:

Count(head);

system("pause");

break;

case-6:

Query_by_first_letter(head);

system("pause");

break;

case0:

printf("再见!

\n");

system("pause");

exit(0);

}

}

}

调用关系图:

1创建电话薄

mainmenu_selectCreateInsert

2.分屏显示电话薄中的所有记录

mainmenu_selectDisplay

3.向电话薄中插入一条记录

mainmenu_selectInsert_a_recordInsert

4.删除一条已经存在的记录项

mainmenu_selectDelete_a_recordDelete

5.根据用户输入的姓名查找符合条件的记录项

mainmenu_selectQuery_a_recordQuery

6.从文件读入已有的电话薄

mainmenu_selectAddfromText

7.将通讯录信息输出到文件

mainmenu_selectWritetoText

8.逆序存放

mainmenu_selectReverse

9.删除同名记录

mainmenu_selectDeleteSame

#.统计人数

mainmenu_selectCount

*.按首字母查询

mainmenu_selectQuery_by_first_letter

0.退出

mainmenu_selectQuit

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

当前位置:首页 > 经管营销 > 经济市场

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

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