怎么用C语言编写简单的班级通讯录系统的管理与实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用C语言编写简单的班级通讯录系统的管理与实现相关的知识,希望对你有一定的参考价值。
具有如下功能:
1、输入记录功能(从键盘输入:姓名,地址,电话,邮编,E-mail)
2、通讯录编辑功能(添加、删除、修改)。
3、按姓名查询功能。
3、显示记录功能。
4、排序功能(按年龄排序后,显示)
5、统计结果保存在文本文件中。
6、从文本文件中读取数据、显示。
要求:设计一个总菜单,分别调用各个子功能模块。撰写课程设计报告,并打印。
#include "stdlib.h"
#include "string.h"
#include "iostream.h"
#include "fstream.h"
typedef struct goal /*成绩*/
float course1;
float course2;
float course3;
float course4;
float course5;
goal;
typedef struct base /*基本信息*/
int num;
char name[20];
char sex[4];
int age;
long tel;
goal *brother;
base;
typedef struct student
int num;
base *firstchild;
struct student *firstbrother;
student;
student *creatroot(void)
student *root;
root=(student*)malloc(sizeof(student));
root->firstchild=NULL;
root->firstbrother=NULL;
root->num=9999;
return (root);
int number(student *root)
student *p=root;
int num=0;
while(p->firstbrother)
p=p->firstbrother;
num++;
printf("总共%d条记录\n",num);
return num;
int jiemian()
int c;
printf("\n\n**********学生信息管理*********\n\n");
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("0 退出\n");
printf("\n*******************************\n");
printf("\n Enter you choice(0~9):\n");
scanf("%d",&c);
while(c<0||c>9)
printf("\nwrong input again:\n");
scanf("%d",&c);
return(c);
student *bublesort(student *root)
student *q,*p,*r;
r=NULL;
while(r!=root->firstbrother)
p=root->firstbrother;q=root;
while(p->firstbrother!=r)
if(p->num>p->firstbrother->num)
q->firstbrother=p->firstbrother;
p->firstbrother=q->firstbrother->firstbrother;
q->firstbrother->firstbrother=p;
q=q->firstbrother;p=q->firstbrother;
r=p;
return(root);
student *loadbase(student *root) /*基本信息管理*/
int i,n;
student *p1,*p2;
base *q1;
goal *t;
p2=root;
if(root->firstbrother!=NULL)
while(p2->firstbrother)
p2=p2->firstbrother;
printf("要登记多少条学生基本信息?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
p1=(student*)malloc(sizeof(student));
q1=(base*)malloc(sizeof(base));
t=(goal*)malloc(sizeof(goal));
p1->firstchild=q1;
q1->brother=t;
printf("学号\t姓名\t年龄\t性别\t电话\n");
scanf("%d",&q1->num);p1->num=q1->num;
scanf("%s",q1->name);
scanf("%d",&q1->age);
scanf("%s",q1->sex);
scanf("%ld",&q1->tel);
t->course1=t->course2=t->course3=t->course4=t->course5=0;
p2->firstbrother=p1;
p2=p1;
p1->firstbrother=NULL;
bublesort(root);
return(root);
student *modifybase(student *root)
student *p;
base *q;
int num,c;
printf("输入要修改的学号:\n");
scanf("%d",&num);
p=root->firstbrother;
while(p!=NULL&&(p->num!=num))
p=p->firstbrother;
if(p==NULL)
printf("学号不存在!\n");
else
q=p->firstchild;
printf("\n\n***********选择需要修改的记录*********\n\n");
printf("1 学号\n");
printf("2 姓名\n");
printf("3 性别\n");
printf("4 年龄\n");
printf("5 电话\n");
printf("6 返回\n");
printf("\n\n**************************************\n\n");
printf("\n Enter you choice(1~6):\n");
scanf("%d",&c);
while(c<0||c>6)
printf("\nwrong input again:\n");
scanf("%d",&c);
switch(c)
case 1: printf("原学号: %d\t输入新学号:\t",q->num);scanf("%d",&q->num);p->num=q->num;break;
case 2: printf("原姓名: %s\t输入新姓名:\t",q->name);scanf("%s",q->name);break;
case 3: printf("原性别: %s\t输入新性别:\t",q->sex);scanf("%s",q->sex);break;
case 4: printf("原年龄: %d\t输入新年龄:\t",q->age);scanf("%d",&q->age);break;
case 5: printf("原电话: %ld\t输入新电话:\t",q->num);scanf("%ld",&q->tel);break;
case 6: return NULL;
root=bublesort(root);
return(root);
student *delbase(student *root)
int num;
student *p,*q;
p=root->firstbrother;q=root;
printf("输入要删除的学号:\t");
scanf("%d",&num);
while(p!=NULL&&p->num!=num)
q=p;
p=p->firstbrother;
if(p==NULL)
printf("学号不存在!\n");
else
q->firstbrother=p->firstbrother;
free(p);
return(root);
student *insertbase(student *root)
int num;
student *p,*q;
base *t;
p=root;
printf("输入要插入的学号");
scanf("%d",&num);
while((p->firstbrother!=NULL)&&(num>p->firstbrother->num))
p=p->firstbrother;
q=(student*)malloc(sizeof(student));
t=(base*)malloc(sizeof(base));
q->firstchild=t;
t->brother=(goal*)malloc(sizeof(goal));
t->brother->course1=t->brother->course2=t->brother->course3=t->brother->course4=t->brother->course5=0;
printf("学号: %d\n",num);
printf("姓名\t年龄\t性别\t电话\n");
q->num=t->num=num;
scanf("%s",t->name);
scanf("%d",&t->age);
scanf("%s",t->sex);
scanf("%ld",&t->tel);
q->firstbrother=p->firstbrother;
p->firstbrother=q;
return(root);
student *loadgoal(student *root) /*成绩信息管理*/
student *p;
base *q;
goal *t;
if(root->firstbrother==NULL)
printf("请输入学生基本信息:\n");
loadbase(root);
else
p=root->firstbrother;
while(p!=NULL)
q=p->firstchild;
t=q->brother;
printf("学号:%d\n",q->num);
printf("课程1\t课程2\t课程3\t课程4\t课程5\n");
scanf("%f",&t->course1);
scanf("%f",&t->course2);
scanf("%f",&t->course3);
scanf("%f",&t->course4);
scanf("%f",&t->course5);
p=p->firstbrother;
return(root);
student *insertgoal(student *root)
student *p;
base *q;
goal *t;
int num;
printf("请输入学号\n");
scanf("%d",&num);
p=root->firstbrother;
while(p!=NULL&&p->num!=num)
p=p->firstbrother;
if(p==NULL)
printf("学号不存在!\n");
else
q=p->firstchild;
t=q->brother;
printf("学号:%d\n",q->num);
printf("课程1\t课程2\t课程3\t课程4\t课程5\n");
scanf("%f",&t->course1);
scanf("%f",&t->course2);
scanf("%f",&t->course3);
scanf("%f",&t->course4);
scanf("%f",&t->course5);
return(root);
student *modifygoal(student *root)
student *p;
base *q;
goal *t;
int num,c;
printf("输入要修改的学号:\n");
scanf("%d",&num);
p=root->firstbrother;
while(p!=NULL&&p->num!=num)
p=p->firstbrother;
if(!p)
printf("学号不存在!\n");
else
q=p->firstchild;
if(!q->brother)
printf("没有已登记的成绩!\n");
else
t=q->brother;
printf("\n\n***********选择需要修改的记录*********\n\n");
printf("1 课程1\n");
printf("2 课程2\n");
printf("3 课程3\n");
printf("4 课程4\n");
printf("5 课程5\n");
printf("6 返回\n");
printf("\n\n**************************************\n\n");
printf("\n Enter you choice(1~6):\n");
scanf("%d",&c);
while(c<0||c>6)
printf("\nwrong input again:\n");
scanf("%d",&c);
switch(c)
case 1: printf("原成绩: %f\t输入新成绩:\t",t->course1);scanf("%f",&t->course1);break;
case 2: printf("原成绩: %f\t输入新成绩:\t",t->course2);scanf("%f",&t->course2);break;
case 3: printf("原成绩: %f\t输入新成绩:\t",t->course3);scanf("%f",&t->course3);break;
case 4: printf("原成绩: %f\t输入新成绩:\t",t->course4);scanf("%f",&t->course4);break;
case 5: printf("原成绩: %f\t输入新成绩:\t",t->course5);scanf("%f",&t->course5);break;
case 6: return NULL;
return(root);
void find(student *root) /*查询*/
student *p=root->firstbrother;
base *q;
goal *t;
int c=1,num;
printf("输入学号\n");
scanf("%d",&num);
while(p!=NULL&&p->num!=num)
p=p->firstbrother;
if(p==NULL)
printf("学号不存在!\n");
else
q=p->firstchild;
printf("查询(基本信息/成绩)?(1/0)\n");
scanf("%d",&c);
if(c==1)
printf("学号\t姓名\t年龄\t性别\t电话\n");
printf("%d ",q->num);
printf("%s ",q->name);
printf("%d ",q->age);
printf("%s ",q->sex);
printf("%ld\n",q->tel);
else if((c==0))
if(q->brother==NULL)
printf("无成绩,请输入成绩\n");root=loadgoal(root);
t=q->brother;
printf("学号: %d\n",q->num);
printf("课程1\t课程2\t课程3\t课程4\t课程5\n");
printf("%f ",t->course1);
printf("%f ",t->course2);
printf("%f ",t->course3);
printf("%f ",t->course4);
printf("%f \n",t->course5);
void print(student *root) /*浏览*/
student *p=root->firstbrother;
base *q;
if(!p)
printf("无内容!\n");
while(p)
q=p->firstchild;
printf("num :%d\n",p->num);
printf("学号\t姓名\t年龄\t性别\t电话\n");
printf("%d ",q->num);
printf("%s ",q->name);
printf("%d ",q->age);
printf("%s ",q->sex);
printf("%ld\n",q->tel);
p=p->firstbrother;
student *insertsave(void) /*申请新结点*/
student *s1;
base *b1;
goal *g1;
s1=(student*)malloc(sizeof(student));
s1->num=0;
b1=(base*)malloc(sizeof(base));
b1->age=0;b1->num=0;b1->tel=0;strcpy(b1->name,"0");strcpy(b1->sex,"0");
g1=(goal*)malloc(sizeof(goal));
g1->course1=g1->course2=g1->course3=g1->course4=g1->course5=0;
s1->firstchild=b1;
s1->firstbrother=NULL;
b1->brother=g1;
return(s1);
void savenum(int num)
fstream in;
in.open("number.txt",ios::in|ios::out);
if(in.fail())
printf("文件不存在\n");
return;
in<<num;
in.close();
int loadnum(void)
int num=0;
fstream out;
out.open("number.txt",ios::in|ios::out);
if(out.fail())
printf("文件不存在\n");
return 0;
out>>num;
out.close();
return num;
void savetofile(student *root)
student *ps;
int i,num=number(root);
ps=root->firstbrother;
if(ps==NULL)
printf("现在没有学生信息,请先输入学生信息\n\n");
return;
fstream in;
in.open("student.txt",ios::in|ios::out);
if(in.fail())
printf("文件不存在\n");
return;
savenum(num);
for(i=0;i<num;i++)
in<<ps->firstchild->num;
in<<" ";
in<<ps->firstchild->name;
in<<" ";
in<<ps->firstchild->age;
in<<" ";
in<<ps->firstchild->sex;
in<<" ";
in<<ps->firstchild->tel;
in<<" ";
in<<ps->firstchild->brother->course1;
in<<" ";
in<<ps->firstchild->brother->course2;
in<<" ";
in<<ps->firstchild->brother->course3;
in<<" ";
in<<ps->firstchild->brother->course4;
in<<" ";
in<<ps->firstchild->brother->course5;
in<<" ";
ps=ps->firstbrother;
in.close();
student *loadfromfile(void)
student *p,*q,*root;
int i,num=0;
root=creatroot();
fstream out;
out.open("student.txt",ios::in|ios::out);
if(out.fail())
printf("文件不存在\n");
return root;
num=loadnum();
printf("记录数:%d",num);
q=root;
for(i=0;i<num;i++)
p=insertsave();
q->firstbrother=p;
q=p;
out>>q->firstchild->num;
out>>q->firstchild->name;
out>>q->firstchild->age;
out>>q->firstchild->sex;
out>>q->firstchild->tel;
out>>q->firstchild->brother->course1;
out>>q->firstchild->brother->course2;
out>>q->firstchild->brother->course3;
out>>q->firstchild->brother->course4;
out>>q->firstchild->brother->course5;
q->num=q->firstchild->num;
out.close();
return root;
void choice(student *root)
student *t;
for(;;)
loop: switch(jiemian())
case 1: root=loadbase(root);break; /*登记基本信息*/
case 2: t=modifybase(root);if(t==NULL)goto loop;else root=t;break; /*修改基本信息*/
case 3: root=delbase(root);break; /*删除基本信息*/
case 4: root=insertbase(root);break; /*插入基本信息*/
case 5: root=loadgoal(root);break; /*登记成绩*/
case 6: t=modifygoal(root);if(t==NULL)goto loop;else root=t;break; /*修改成绩*/
case 7: insertgoal(root);break; /*插入新成绩*/
case 8: print(root);break; /*浏览*/
case 9: find(root);break; /*查看单条*/
case 0: savetofile(root);free(root);exit(1); /*退出*/
void main()
student *root;
root=loadfromfile();
choice(root);
没有现成的,自己改改
以上是关于怎么用C语言编写简单的班级通讯录系统的管理与实现的主要内容,如果未能解决你的问题,请参考以下文章