C语言程序报告.docx
- 文档编号:30133821
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:28
- 大小:230.07KB
C语言程序报告.docx
《C语言程序报告.docx》由会员分享,可在线阅读,更多相关《C语言程序报告.docx(28页珍藏版)》请在冰豆网上搜索。
C语言程序报告
C
程
序
课
程
设
计
实
验
报
告
专业:
电子商务1101班
组号:
2
组员:
黄文星、王林涛、
安顶、孙伟、
雷晓亮、牧云飞
学生通讯录
1.设计内容及要求:
使用结构,链表
Ø能创建、遍历输出学生通讯录
Ø修改和增删学生通讯录
Ø能够按多种方式进行查询
Ø采用文件存储数据
Ø用2个或2个以上文件(*.c)来存放上面个子函数与主程序(只能一个)。
2.通讯录完整程序能创建、遍历输出学生通讯录
#include
#include
//#defineNULL0
#defineLENsizeof(structmessage)
structmessage//定义结构体
{
charname[15];
charnumber[15];
charphone[15];
charqq[13];
charaddr[25];
structmessage*next;//指向下一数据
};
intn;
voidcreat()//建立通讯录
{
intnum;
charchoice[5]={'2'};
FILE*fp;
structmessage*head;
structmessage*p,*q;
n=0;
p=q=(structmessage*)malloc(LEN);
fp=fopen("通讯录1.txt","wb");
system("cls");
printf("\n请输入姓名:
");
scanf("%s",p->name);
printf("\n请输入电话号码:
");
scanf("%s",p->phone);
printf("\n请输入学号:
");
scanf("%s",p->number);
printf("\n请输入QQ:
");
scanf("%s",p->qq);
printf("\n请输入住址:
");
scanf("%s",p->addr);
fwrite(p,LEN,1,fp);
fclose(fp);
head=0;
num=1;
while(num!
=0)
{
n=n+1;
if(n==1)head=p;
elseq->next=p;
q=p;
p=(structmessage*)malloc(LEN);//分配存储区
while(choice[0]!
='1'&&choice[0]!
='0')//判断是否继续
{
printf("\n1继续输入\n0返回\n");
printf("请选择操作:
");
scanf("%s",choice);
}
switch(choice[0])
{
case'1':
{
fp=fopen("通讯录1.txt","rb");
system("cls");
printf("\n请输入姓名:
");
scanf("%s",p->name);
printf("\n请输入电话号码:
");
scanf("%s",p->phone);
printf("\n请输入学号:
");
scanf("%s",p->number);
printf("\n请输入QQ:
");
scanf("%s",p->qq);
printf("\n请输入住址:
");
scanf("%s",p->addr);
fwrite(p,LEN,1,fp);
fclose(fp);
choice[0]='2';
}continue;
case'0':
num=0;
system("cls");
}
}
q->next=0;
}
voidadd()//添加通讯录
{
intnum;
charchoice[5]={'2'};
FILE*fp;
structmessage*head;
structmessage*p,*q;
n=0;
p=q=(structmessage*)malloc(LEN);//分配空间
fp=fopen("通讯录1.txt","ab");
system("cls");
printf("\n请输入姓名:
");//输入内容
scanf("%s",p->name);
printf("\n请输入电话号码:
");
scanf("%s",p->phone);
printf("\n请输入学号:
");
scanf("%s",p->number);
printf("\n请输入QQ:
");
scanf("%s",p->qq);
printf("\n请输入住址:
");
scanf("%s",p->addr);
fwrite(p,LEN,1,fp);
fclose(fp);
head=0;
num=1;
while(num!
=0)
{
n=n+1;
if(n==1)head=p;
elseq->next=p;
q=p;
p=(structmessage*)malloc(LEN);//分配空间
while(choice[0]!
='1'&&choice[0]!
='0')//判断是否继续
{
printf("\n1继续输入\n0返回\n");
printf("请选择操作:
");
scanf("%s",choice);
}
switch(choice[0])
{
case'1':
{
fp=fopen("通讯录1.txt","rb");
system("cls");
printf("\n请输入姓名:
");
scanf("%s",p->name);
printf("\n请输入电话号码:
");
scanf("%s",p->phone);
printf("\n请输入学号:
");
scanf("%s",p->number);
printf("\n请输入QQ:
");
scanf("%s",p->qq);
printf("\n请输入住址:
");
scanf("%s",p->addr);
fwrite(p,LEN,1,fp);
fclose(fp);
choice[0]='2';
}continue;
case'0':
num=0;
system("cls");
}
}
q->next=0;
}
voidfindname(structmessage*fphead)//按姓名查找
{
structmessage*p;
inta=0;
charfindname[20];
system("cls");
printf("请输入要查找的姓名:
\n");
scanf("%s",findname);
for(p=fphead->next;p!
=NULL;p=p->next)
{
if(strcmp(p->name,findname)==0)
{
printf("姓名电话号码学号QQ住址\n");
printf("%-15s%-15s%-15s%-13s%-25s\n",p->name,p->phone,p->number,p->qq,p->addr);
a=1;
}
}
if(a!
=1)
printf("无此通讯录!
\n");
}
voidfindphone(structmessage*fphead)//按电话查找
{
structmessage*p;
inta=0;
charfindphone[15];
system("cls");
printf("请输入要查找的电话:
\n");
scanf("%s",findphone);
for(p=fphead->next;p!
=NULL;p=p->next)
{
if(strcmp(p->phone,findphone)==0)
{
printf("姓名电话号码学号QQ住址\n");
printf("%-15s%-15s%-15s%-13s%-25s\n",p->name,p->phone,p->number,p->qq,p->addr);
a=1;
}
}
if(a!
=1)
printf("无此通讯录!
\n");
}
voidfindnumber(structmessage*fphead)//按学号查找
{
structmessage*p;
inta=0;
charfindnumber[15];
system("cls");
printf("请键入要查找的学生学号:
\n");
scanf("%s",findnumber);
for(p=fphead->next;p!
=NULL;p=p->next)
{
if(strcmp(p->number,findnumber)==0)
{
printf("姓名电话号码学号QQ住址\n");
printf("%-15s%-15s%-15s%-13s%-25s\n",p->name,p->phone,p->number,p->qq,p->addr);
a=1;
}
}
if(a!
=1)
printf("无此通讯录!
\n");
}
voidmodify(structmessage*fphead)//修改记录
{
FILE*fp;
structmessage*p,*q;
inta=0;
charfindname[15];
system("cls");
printf("请键入要查找的姓名:
\n");
scanf("%s",findname);
for(p=fphead->next;p!
=NULL;p=p->next)
{
if(strcmp(p->name,findname)==0)
{
printf("姓名电话号码学号QQ住址\n");
printf("%-15s%-15s%-15s%-13s%-25s\n\n\n\n",p->name,p->phone,p->number,p->qq,p->addr);
printf("\n请键入新姓名:
");
scanf("%s",p->name);
printf("\n请键入新电话号码:
");
scanf("%s",p->phone);
printf("\n请键入新学号:
");
scanf("%s",p->number);
printf("\n请键入新QQ:
");
scanf("%s",p->qq);
printf("\n请键入新住址:
");
scanf("%s",p->addr);
fp=fopen("通讯录2.txt","wb");
q=fphead->next;//P为链表头指针
while(q!
=0)
{
fwrite(q,LEN,1,fp);
if(q->next!
=0)
q=q->next;
else
q=0;
}
fclose(fp);
unlink("通讯录1.txt");//用新文件覆盖原文件
rename("通讯录2.txt","通讯录1.txt");
printf("修改成功!
");
a=1;
}
}
if(a!
=1)
printf("无此通讯录!
\n");
}
voiddel(structmessage*fphead)//删除记录
{
FILE*fp,*fq;
structmessage*p,*q,*r;
charsname[15];
p=q=(structmessage*)malloc(LEN);
p=fphead;
system("cls");
fp=fopen("通讯录1.txt","rb");
fq=fopen("通讯录2.txt","wb");
printf("请输入欲删除记录的姓名:
");
scanf("%s",sname);
while(strcmp(p->name,sname)!
=0&&p->next!
=0)//查找节点并删除该节点
{
q=p;
p=p->next;
}
if(strcmp(p->name,sname)==0)
{
if(p==fphead)
fphead=p->next;
else
{
q->next=p->next;
}
r=fphead->next;//r为链表头指针
while(r!
=0)
{
fwrite(r,LEN,1,fq);
if(r->next!
=0)
r=r->next;
else
r=0;
}
printf("删除成功!
");
}
else
{
printf("无此通讯录!
");
}
fclose(fp);
fclose(fq);
unlink("通讯录1.txt");//用新文件覆盖原文件
rename("通讯录2.txt","通讯录1.txt");
}
structmessage*open(void)//打开文件,载入链表
{
FILE*fp;
structmessage*p=NULL,*q=NULL,*temp=NULL,*fphead=NULL;
fp=fopen("通讯录1.txt","rb");
fphead=(structmessage*)malloc(LEN);//分配空间
temp=q=fphead;
while(!
feof(fp))//循环读取,载入链表
{
p=(structmessage*)malloc(LEN);
temp=q;
q->next=p;
q=p;
fread(p,LEN,1,fp);
}
temp->next=NULL;
fclose(fp);
return(fphead);//返回链表头指针
}
voidprint()//显示通讯录
{
structmessage*p;
FILE*fp;
system("cls");
if((fp=fopen("通讯录1.txt","rb"))!
=NULL)
{
printf("姓名电话号码学号QQ住址\n");
p=(structmessage*)malloc(LEN);//分配空间
while(!
feof(fp))
{
fread(p,LEN,1,fp);
printf("%-15s%-15s%-15s%-13s%-25s\n",p->name,p->phone,p->number,p->qq,p->addr);
}
fclose(fp);
}
else
{
printf("未建立通讯录!
");
}
}
voidmain()//主函数
{
FILE*fp;
structmessage*fphead;
intbegin=1,f;
charchoice[5];
while(begin==1)
{
printf("\t\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf("\t\t\t\t欢迎使用学生通讯录\n");
printf("\t\t<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n\n");
printf("\t\t\t*主窗口菜单*\n\n");
printf("\t\t\t1:
显示通讯录中所有记录;\n\n");
printf("\t\t\t2:
建立新通讯录;\n\n");
printf("\t\t\t3:
添加新记录;\n\n");
printf("\t\t\t4:
查询通讯录中的记录;\n\n");
printf("\t\t\t5:
修改通讯录中的记录;\n\n");
printf("\t\t\t6:
删除通讯录中的记录;\n\n");
printf("\t\t\t0:
退出通讯录\n\n");
printf("请选择操作:
");
scanf("%s",choice);
switch(choice[0])
{
case'1':
{
print();
printf("按任意键返回主菜单!
\n");
getch();
system("cls");
}continue;
case'2':
{
creat();
}continue;
case'3':
{
add();
}continue;
case'4':
{
if((fp=fopen("通讯录1.txt","rb"))!
=NULL)
{
fclose(fp);
system("cls");
printf("1按姓名查询;\n2按电话查询;\n3按学号查询;\n");
printf("请输入查询方式:
");
scanf("%d",&f);
if(f==1)
{
fphead=open();
findname(fphead);
}
if(f==2)
{
fphead=open();
findphone(fphead);
}
elseif(f==3)
{
fphead=open();
findnumber(fphead);
}
}
else
{
system("cls");
printf("未建立通讯录!
");
}
printf("按任意键返回主菜单!
\n");
getch();
system("cls");
}continue;
case'5':
{
if((fp=fopen("通讯录1.txt","rb"))!
=NULL)
{
fclose(fp);
fphead=open();
modify(fphead);
}
else
{
system("cls");
printf("未建立通讯录!
");
}
printf("按任意键返回主菜单!
\n");
getch();
system("cls");
}continue;
case'6':
{
if((fp=fopen("通讯录1.txt","rb"))!
=NULL)
{
fclose(fp);
fphead=open();
del(fphead);
}
else
{
system("cls");
printf("未建立通讯录!
");
}
printf("按任意键返回主菜单!
\n");
getch();
system("cls");
}continue;
case'0':
exit(0);
default:
{
system("cls");
printf("输入错误,按任意键返回主菜单!
\n");
getch();
system("cls");
}
}
}
system("pause");
}
3.通讯录详细设计:
1.信息建立
2.信息删除
3.信息添加
4.信息查询
(1).姓名查找:
(2).学号查询
Y
5.信息显示
6.信息修改
4.运行结果:
1.创建通讯录
2.增加通讯录
3.修改通讯录
4.删除通讯录
5.查询通讯录
(1)按姓名查找:
(2)按电话查找:
(3)按学好查找:
6.显示通讯录
5.总结:
通过这次课程设计,我们对C语言以有了更深刻的了解,增强了程序的编写能力,巩固了专业知识,对程序的模块化观念也又模糊逐渐变的清晰了。
在程序的运行与调试过程中出现了很多错误,我们通过反复地复习课本上的相关知识,不停地修改与调试,在我们的共同努力下,终于完成了这段程序。
在此期间,我们也认识到了自己的薄弱之处,如对链表相关知识的欠缺,对指针分配类存空间等内容未深刻理解,文件运用的不熟练,在以后的学习中我们都要集中精力、端正态度,争取把知识学得更扎实、更全面。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 程序 报告