学生通讯录管理系统,源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//自动根据名字的字典序排序
#include
#include
#include
typedef struct peoples
{
int qunzuhao;
char name[20];
char tel[20];
char addr[20];
char email[20];
struct peoples *next;
}people;
struct qunzu
{
char qunzuname[20];
people *next;
};
struct qunzu qunzunode[100];
int sum_qunzu = 0; // 标记有多少个群组
void init()
{
FILE *ep;// 分别存储学生、群组名的文件
people *r , *p , *q;
int i = 1;
char xy[20];
ep = fopen("c:\\VC6\\qunzu.in" , "r");
fseek(ep , 0 , 2); //判断文件是否一开始就为空
int sizeep = ftell(ep);
rewind(ep);
//feof函数无法一开始就判断一个文件是空文件 , 它先调用一次这个文件之后 , 才能开始判断这个文件是否是空的
if(sizeep > 0)
{
while(!feof(ep))
{
qunzunode[i].next = NULL;
fscanf(ep , "%s" , xy);
strcpy(qunzunode[i].qunzuname , xy);
// fread(&qunzunode[i] , sizeof(qunzunode[1]) , 1 , ep);
qunzunode[i].next = (people *)malloc(sizeof(people));
qunzunode[i].next->next = NULL;
i += 1;
sum_qunzu += 1;
}
}
fclose(ep);
FILE *fp;
fp = fopen("c:\\VC6\\people.in" , "r");
fseek(fp , 0 , 2);
int sizefp = ftell(fp);
rewind(fp); //指到文件头
//printf("%d\n" , feof(fp));
if(sizefp > 0)
{
while(!feof(fp))
{
p = (people *)malloc(sizeof(people));
p->next = NULL;
fscanf(fp , "%d %s %s %s %s" , &p->qunzuhao , p->name , p->tel , p->addr , p->email);
int x = p->qunzuhao;
//printf("%d\n" , x);
r = qunzunode[x].next;
if(r->next == NULL) r->next = p;
else
{
while(1)
{
// printf("1\n");
if(strcmp(r->next->name , p->name) == 1)
{
q = r->next;
r->next = p;
p->next = q;
break;
}
if(r->next->next == NULL) { r->next->next = p; p->next = NULL; break;}
r = r->next;
}
}
}
}
fclose(fp);
/*int i;
for(i = 1; i <= sum_qunzu; i++)
{
qunzunode[i].next = (people *)malloc(sizeof(people));
qunzunode[i].next->next = NULL;
}
strcpy(qunzunode[1].qunzuname , "朋友");
strcpy(qunzunode[2].qunzuname , "同事");
strcpy(qunzunode[3].qunzuname , "家人");
strcpy(qunzunode[4].qunzuname , "同学");
strcpy(qunzunode[5].qunzuname , "其他");*/
}
void addpeople()
{
int i;
for(i = 1; i <= sum_qunzu ; i++)
{
printf("****%d:%s\n" , i , qunzunode[i].qunzuname);
}
peoples *p , *r , *q;
p = (people *)malloc(sizeof(people));
p->next = NULL;
printf("\t选择群组:");
scanf("%d" , &p->qunzuhao);
printf("\t名字:");
scanf("%s", p->name);
printf("\t手机:");
scanf("%s", p->tel);
printf("\t地址:");
scanf("%s", p->addr);
printf("\t邮箱:");
scanf("%s", p->email);
for(i = 1; i <= sum_qunzu ; i++) //判断是否存在重复的信息
{
// printf("1\n");
r = qunzunode[i].next;
r = r->next;
while
(r != NULL)
{
if(strcmp(p->addr , r->addr) == 0) {printf("在记录中存在相同的地址!\n"); return ;}
if(strcmp(p->addr , r->name) == 0) {printf("在记录中存在相同的名字!\n"); return ;}
if(strcmp(p->addr , r->tel) == 0) {printf("在记录中存在相同的电话号码!\n"); return ;}
if(strcmp(p->addr , r->email) == 0) {printf("在记录中存在相同的email!\n"); return ;}
r = r->next;
}
}
FILE *fp;//写入文件中
fp = fopen("c:\\VC6\\people.in" , "a"); //在原文件末尾追加
fseek(fp , 0 , 2);
fprintf(fp , "\n%5d %5s %5s %5s %5s" , p->qunzuhao , p->name , p->tel , p->addr , p->email);
// printf("1\n");
r = qunzunode[p->qunzuhao].next;
// qunzunode[qunzuname].sumpeople += 1;
if(r->next == NULL) r->next = p;
else
{
while(1)
{
// printf("1\n");
if(strcmp(r->next->name , p->name) == 1)
{
q = r->next;
r->next = p;
p->next = q;
break;
}
if(r->next->next == NULL) { r->next->next = p; p->next = NULL; break;}
r = r->next;
}
}
fclose(fp);
}
void delpeople()
{
int pre , i;
char xy[20];
int qun;
people *r , *p , *q;
printf("***1:删除1条记录\n");
printf("***2:删除1个群组\n");
printf("***3:删除所有记录\n");
printf("请输入选择:");
scanf("%d" , &pre);
if(pre == 1) // 如果有某个信息相同时
{
while(1)
{
printf("****1:通过名字查找删除\n");
printf("****2:通过电话号码查找删除\n");
printf("****3:通过地址查找删除\n");
printf("请输入选择:");
scanf("%d" , &pre);
if(pre == 1)
{
printf("名字:");
scanf("%s" , xy);
for(i = 1; i <= sum_qunzu; i++)
{
r = qunzunode[i].next;
if(r == NULL) continue ;
while(r->next != NULL)
{
if(strcmp(r->next->name , xy) == 0)
{
p = r->next->next;
r->next = p;
break;
}
}
}
}
else if(pre == 2)
{
printf("****请输入电话号码:");
scanf("%s" , xy);
for(i = 1; i <= sum_qunzu; i++)
{
r = qunzunode[i].next;
if(r == NULL) continue ;
while(r->next != NULL)
{
if(strcmp(r->next->tel , xy) == 0)
{
p = r->next->next;
r->next = p;
break;
}
}
}
}
else if(pre == 3)
{
printf("****请输入地址:");
scanf("%s" , xy);
for(i = 1; i <= sum_qunzu; i++)
{
r = qunzunode[i].next;
if(r == NULL) continue ;
while(r->next != NULL)
{
if(strcmp(r->next->addr , xy) == 0)
{
q = r->next;
p = r->next->next;
r->next = p;
free(q);
break;
}
}
}
}
else
{
printf("输入有误:\n");
printf("1:返回重新输入!\n");
printf("2:退出查询系统!\n");
int cf;
printf("请输入选择:");
scanf("%d" , &cf);
if(cf == 1) continue ;
else return ;
}
FILE *fp;
fp = fopen("c:\\VC6\\people.in" , "wb"); //重新打开文件 , 并把原记录都删除
for(i = 1; i <= sum_qunzu ; i++) //重新写入文件
{
// printf("1\n");
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
fprintf(fp , "%5d %5s %5s %5s %5s" , r->qunzuhao , r->name , r->tel , r->addr , r->email);
r = r->next;
}
}
fclose(fp);
printf("删除记录成功!\n");
return ;
}
}
else if(pre == 2)
{
printf("请输入群组:");
scanf("%d" , &qun);
if(qunzunode[qun].next == NULL) { printf("该群组无记录!\n") ;return ;}
r = qunzunode[qun].next->next;
qunzunode[qun].next = NULL;
while(r != NULL)
{
p = r;
r = r->next;
free(p);
}
free(r);
FILE *fp;
fp = fopen("c:\\VC6\\people.in" , "wb"); //重新打开文件 , 并把原记录都删除
for(i = 1; i <= sum_qunzu ; i++) //重新写入文件
{
// printf("1\n");
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
fwrite(r , sizeof(people) , 1 , fp);
r = r->next;
}
}
fclose(fp);
}
else if(pre == 3)
{
for(i = 1; i <= sum_qunzu; i++)
{
if(qunzunode[i].next == NULL) continue ;
r = qunzunode[i].next->next;
qunzunode[i].next = NULL;
while(r != NULL)
{
p = r;
r = r->next;
free(p);
}
free(r);
}
FILE *fp;
fp = fopen("c:\\VC6\\people.in" , "wb"); //重新打开文件 , 并把原记录都删除
fclose(fp);
printf("删除记录成功!\n");
//这里之所以不要重新写入 , 是因为全部都删除了 , 已经没有记录了
}
else printf("输入有误!");
}
void addqunzu()
{
char name[20];
printf("***请输入要增加的群组名:");
scanf("%s" , name);
sum_qunzu += 1;
strcpy(qunzunode[sum_qunzu].qunzuname , name);
qunzunode[sum_qunzu].next = (people *)malloc(sizeof(people));
FILE *fp;
fp = fopen("c:\\VC6\\qunzu.in" , "a");
fseek(fp , 0 , 2);
fprintf(fp , "\n%s" , name);
fclose(fp);
}
void findpeople()
{
printf("***1:模糊查询\n");
printf("***2:精确查询\n");
int pre , i ;
char xy[20];
people *r;
scanf("%d" , &pre);
if(pre == 1)
{
printf("****1:通过名字查找\n");
printf("****2:通过电话号码查找\n");
printf("****3:通过地址查找\n");
printf("****4:通过email查找\n");
scanf("%d" , &pre);
int bz = 0;
if(pre == 1)
{
printf("请输入模糊查找的名字:");
scanf("%s" , xy);
for(i = 1; i < sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(strncmp(xy , r->name , strlen(xy)) == 0)
{
printf("群组: %s\n" , qunzunode[i].qunzuname);
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:
%s\n" , r->addr);
printf("email: %s\n" , r->email);
bz += 1;
}
r = r->next;
}
}
if(bz == 0)
printf("没有相应的记录!\n");
}
if(pre == 2)
{
printf("请输入模糊查找的电话号码:");
scanf("%s" , xy);
for(i = 1; i < sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(strncmp(xy , r->tel , strlen(xy)) == 0)
{
printf("群组: %s\n" , qunzunode[i].qunzuname);
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:%s\n" , r->addr);
printf("email: %s\n" , r->email);
bz += 1;
}
r = r->next;
}
if(bz == 0)
printf("没有相应的记录!\n");
}
}
if(pre == 3)
{
printf("请输入模糊查找的学校地址:");
scanf("%s" , xy);
for(i = 1; i < sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(strncmp(xy , r->addr , strlen(xy)) == 0)
{
printf("群组: %s\n" , qunzunode[i].qunzuname);
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:%s\n" , r->addr);
printf("email: %s\n" , r->email);
bz += 1;
}
r = r->next;
}
if(bz == 0)
printf("没有相应的记录!\n");
}
}
if(pre == 4)
{
printf("请输入模糊查找的email:");
scanf("%s" , xy);
for(i = 1; i < sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(strncmp(xy , r->email , strlen(xy)) == 0)
{
printf("群组: %s\n" , qunzunode[i].qunzuname);
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:%s\n" , r->addr);
printf("email: %s\n" , r->email);
bz += 1;
}
r = r->next;
}
if(bz == 0)
printf("没有相应的记录!\n");
}
}
}
else if(pre == 2)
{
printf("****1:通过名字查找\n");
printf("****2:通过电话号码查找\n");
printf("****3:通过地址查找\n");
printf("****4:通过email查找\n");
scanf("%d" , &pre);
int bz = 0;
if(pre == 1)
{
printf("请输入精确查找的名字:");
scanf("%s" , xy);
for(i = 1; i < sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(strcmp(xy , r->name) == 0)
{
printf("群组: %s\n" , qunzunode[i].qunzuname);
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:%s\n" , r->addr);
printf("email: %s\n" , r->email);
bz += 1;
}
r = r->next;
}
}
if(bz == 0)
printf("没有相应的记录!\n");
}
if(pre == 2)
{
printf("请输入精确查找的电话号码:");
scanf("%s" , xy);
for(i = 1; i < sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(strcmp(xy , r->tel) == 0)
{
printf("群组: %s\n" , qunzunode[i].qunzuname);
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:%s\n" , r->addr);
printf("email: %s\n" , r->email);
bz += 1;
}
r = r->next;
}
if(bz == 0)
printf("没有相应的记录!\n");
}
}
if(pre == 3)
{
printf("请输入精确查找的学校地址:");
scanf("%s" , xy);
for(i = 1; i < sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(strcmp(xy , r->addr) == 0)
{
printf("群组: %s\n" , qunzunode[i].qunzuname);
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:%s\n" , r->addr);
printf("email: %s\n" , r->email);
bz += 1;
}
r = r->next;
}
if(bz == 0)
printf("没有相应的记录!\n");
}
}
if(pre == 4)
{
printf("请输入精确查找的email:");
scanf("%s" , xy);
for(i = 1; i < sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(strcmp(xy , r->email ) == 0)
{
printf("群组: %s\n" , qunzunode[i].qunzuname);
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:%s\n" , r->addr);
printf("email: %s\n" , r->email);
bz += 1;
}
r = r->next;
}
if(bz == 0)
printf("没有相应的记录!\n");
}
}
}
}
void disp()
{
int pre , i , qun;
people *r;
printf("1:群组显示\n");
printf("2:所有记录都显示\n");
printf("请输入选择:");
scanf("%d" , &pre);
if(pre == 1)
{
for(i = 1; i <= sum_qunzu ; i++)
{
printf("****%d:%s\n" , i , qunzunode[i].qunzuname);
}
printf("请输入要显示的群组:");
scanf("%d" , &qun);
r = qunzunode[qun].next;
r = r->next;
if(r == NULL) {printf("该群组没有记录!\n"); return ;}
while(r != NULL)
{
printf("姓名: %s\n" , r->name);
printf("电话号码:%s\n" , r->tel);
printf("学校地址:%s\n" , r->addr);
printf("email: %s\n" , r->email);
r = r->next;
}
}
else if(pre == 2)
{
for(i = 1; i <= sum_qunzu ; i++)
{
r = qunzunode[i].next;
r = r->next;
if(r == NULL) continue;
printf("%s\n" , qunzunode[i].qunzuname);
while(r != NULL)
{
printf("****姓名: %s\n" , r->name);
printf("****电话号码:%s\n" , r->tel);
printf("****学校地址:%s\n" , r->addr);
printf("****email: %s\n" , r->email);
r
= r->next;
}
printf("\n");
}
}
}
void modi() //修改学生信息
{
printf("****1:修改一个同学的名字\n");
printf("****2:修改一个同学的电话号码\n");
printf("****3:修改一个同学的地址\n");
printf("****4:修改一个同学的email\n");
int pre , i;
scanf("%d" , &pre);
char xy[20] , gh[20];
people *r;
if(pre == 1)
{
printf("输入原来的名字:");
scanf("%s" , xy);
printf("输入修改了的名字:");
scanf("%s" , gh);
for(i = 1; i <= sum_qunzu; i++)
{
r = qunzunode[i].next;
r = r->next;
if(r == NULL) continue ;
while(r != NULL)
{
if(strcmp(r->name , xy) == 0)
{
strcpy(r->name , gh);
break;
}
r = r->next;
}
if(r != NULL) break;
}
}
else if(pre == 2)
{
printf("输入原来的电话号码:");
scanf("%s" , xy);
printf("输入修改了的电话号码:");
scanf("%s" , gh);
for(i = 1; i <= sum_qunzu; i++)
{
r = qunzunode[i].next;
r = r->next;
if(r == NULL) continue ;
while(r != NULL)
{
if(strcmp(r->tel , xy) == 0)
{
strcpy(r->tel , gh);
break;
}
r = r->next;
}
if(r != NULL) break;
}
}
else if(pre == 3)
{
printf("输入原来的地址:");
scanf("%s" , xy);
printf("输入修改了的地址:");
scanf("%s" , gh);
for(i = 1; i <= sum_qunzu; i++)
{
r = qunzunode[i].next;
r = r->next;
if(r == NULL) continue ;
while(r != NULL)
{
if(strcmp(r->addr , xy) == 0)
{
strcpy(r->addr , gh);
break;
}
r = r->next;
}
if(r != NULL) break;
}
}
else if(pre == 4)
{
printf("输入原来的email:");
scanf("%s" , xy);
printf("输入修改了的email:");
scanf("%s" , gh);
for(i = 1; i <= sum_qunzu; i++)
{
r = qunzunode[i].next;
r = r->next;
if(r == NULL) continue ;
while(r != NULL)
{
if(strcmp(r->email , xy) == 0)
{
strcpy(r->email, gh);
break;
}
r = r->next;
}
if(r != NULL) break;
}
}
FILE *fp;
fp = fopen("c:\\VC6\\people.in" , "w+"); //重新打开文件 , 并把原记录都删除
//在这里不能用"wb"的方式读写 , 因为"wb"读写无法识别"\n"等转义字符
fseek(fp , 0 , 2);
int bz = 0; //标记是不是写入文件的第一组数据
for(i = 1; i <= sum_qunzu ; i++) //重新写入文件
{
// printf("1\n");
r = qunzunode[i].next;
r = r->next;
while(r != NULL)
{
if(bz == 0)
{
fprintf(fp , "%5d %5s %5s %5s %5s" , r->qunzuhao , r->name , r->tel , r->addr , r->email);
bz = 1;
}
else
fprintf(fp , "\n%5d %5s %5s %5s %5s" , r->qunzuhao , r->name , r->tel , r->addr , r->email);
r = r->next;
}
}
fclose(fp);
}
void quit()
{
printf(" ****** 谢谢再见! ****** \n");
exit(0);
}
int main()
{
FILE *fp , *ep; // 分别存储学生
、群组名的文件
if((fp = fopen("c:\\VC6\\people.in" , "r")) == NULL || (ep = fopen("c:\\VC6\\qunzu.in" , "r")) == NULL)
{
printf("文件不能打开\n");
exit(0);
}
sum_qunzu = 0; // 初始化群组的数量
init();
printf("系统初始化完成!\n");
int pre;
while(1)
{
printf("学生通讯管理系统!\n");
printf("1、增加记录! 2、增加群组!\n");
printf("3、删除记录! 4、显示记录!\n");
printf("5、查找记录! 6、修改信息!\n");
printf("0、退出通讯录!\n");
scanf("%d" , &pre) ;
switch(pre)
{
case 1: addpeople(); break;
case 2: addqunzu(); break;
case 3: delpeople(); break;
case 4: disp(); break;
case 5: findpeople(); break;
case 6: modi(); break;
case 0: quit(); return 0;
}
}
addpeople();
fclose(fp) ;
fclose(ep) ;
return 0;
}