#include <stdio.h> #include <string.h> #include <stdlib.h> #define LEN sizeof(struct addbook) #define MAX 100 void InitDialBook(void); void UserInterface(void);/*菜单函数*/ void Load(void); void Add(void); void Find(void); void Modify(void); void Delete(void); void ShowAll(void); void Save(void); struct addbook{ int record; char name[15]; char sex[4]; char age[4]; char addr[20]; char phone[15]; char qq[15]; char meg[40]; /*个性签名*/ struct addbook *link; }; FILE *fp; struct addbook *p,*p1,*head,*s,*t; main() { int flag; int choice; if((fp = fopen("zzxx.txt","rb"))==NULL) { clrscr(); printf("\n\n\t\t\t文件不存在!\n"); if((fp = fopen("zzxx.txt","wb"))==NULL) { printf("文件打开错误!\n"); getch(); exit(0); } } if(fgetc(fp)==-1) { printf("\n\t\t通讯录里没有信息,现在您必须添加信息.\n"); InitDialBook(); printf("\n\t\t\t添加成功!\n"); printf("\n\t\t您若想添加更多人的信息需请重新打开程序\n"); getch(); exit(0); } rewind(fp); /* 重建文件指针 */ Load(); fclose(fp); /* printf("\n这是文件里第%d位成员.\n",p->record); */ flag = 1; while(flag) { UserInterface(); printf("请输入您的选择:(0-5):"); scanf("%d",&choice); getchar(); if(choice>=0&&choice<6) { switch(choice) { case 1: Add();break; case 2: Find();break; case 3: ShowAll();break; case 4: Delete();break; case 5: Modify();break; case 0: flag = 0; clrscr(); printf("\n\n\n\t\t\t谢谢使用本通讯录系统!!!"); printf("\n\n\t\t如果你在使用过程中有什么要求或建议请连系:"); printf("\n\n\t\t\tzzxx2001@126.com或QQ:279252614"); getch(); break; } } else{ printf("输入错误,请重新选择!\n"); } } fp = fopen("zzxx.txt","wb"); Save(); fclose(fp); clrscr(); printf("\n\n\n\t\t\t按任意键退出!\n"); getch(); return 0; } void InitDialBook(void)/*初始化文件函数*/ { p->record = 1; printf("\n\n\t\t请输入姓名:"); gets(p->name); printf("\n\t\t请输入性别:"); gets(p->sex); printf("\n\t\t请输入年龄:"); gets(p->age); printf("\n\t\t请输入地址:"); gets(p->addr); printf("\n\t\t请输入电话:"); gets(p->phone); printf("\n\t\t请输入QQ号码:"); gets(p->qq); printf("\n\t\t请输入个性签名:"); gets(p->meg); p->link = NULL; head = p; fwrite(p,LEN,1,fp); fclose(fp); } void UserInterface(void)/*菜单*/ { clrscr(); printf("\n\t\t********************************************\n"); printf("\t\t\t欢迎使用通讯录管理系统"); printf("\n\t\t********************************************\n"); printf("\n\n\t\t\t******请选择你的操作******\n"); printf("\t\t\t +-------------------------------+"); printf("\t\t\t\t\t\t | 1 增加个人信息 |\n"); printf("\t\t\t | 2 查找信息(包含删除\修改信息) |\n"); printf("\t\t\t | 3 显示所有信息 |\n"); printf("\t\t\t | 4 删除信息 |\n"); printf("\t\t\t | 5 修改信息 |\n"); printf("\t\t\t | 0 退出和保存 |\n"); printf("\t\t\t +-------------------------------+\n"); printf("\n\n\t注意!!输入数字(电话号码或QQ)要在英文状态下,中文状态无法输入!!\n\n"); } void Load(void)/*读文件*/ { fread(head,LEN,1,fp); p = head; while(p->link!=NULL){ p1 = (struct addbook*)malloc(LEN); p1 = p->link; fread(p1,LEN,1,fp); p = p->link; } } void Add(void)/*输入函数*/ { p1 = (struct addbook*)malloc(LEN); p1->record = p->record + 1; clrscr(); printf("\n\n\t\t\t-------------------------------------------\n"); printf("\t\t\t\t\t请输入信息\n"); printf("\t\t\t-------------------------------------------\n"); printf("\n\t\t\t这是第%d位成员",p->record+1); printf("\n\t\t\t请输入姓名:"); gets(p1->name); printf("\n\t\t\t请输入性别:"); gets(p1->sex); printf("\n\t\t\t请输入年龄:"); gets(p1->age); printf("\n\t\t\t请输入地址:"); gets(p1->addr); printf("\n\t\t\t请输入电话:"); gets(p1->phone); printf("\n\t\t\t请输入QQ号码:"); gets(p1->qq); printf("\n\t\t\t请输入个性签名:"); gets(p1->meg); p1->link = NULL; p->link = p1; p = p1; } void ShowAll(void)/*显示所有函数*/ { s = head; clrscr(); printf("\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf(" \t序号 姓名 性别 年龄 地址 电话 QQ号码 个性签名"); printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); do{ printf("\t %-6d%-10s%-8s%-7s%-10s%-10s%-s%-s\n",s->record,s->name,s->sex, s->age,s->addr,s->phone,s->qq,s->meg); s = s->link; }while(s!=NULL); printf("\n\n\n\t\t\t按任意键退出!"); getch(); return; } void Find(void)/*查找函数*/ { char c; struct addbook who; s = head; clrscr(); printf("\n\t\t请输入要查找人的姓名:"); gets(who.name); while((s->link!=NULL)&&(strcmp(s->name,who.name))) { s = s->link; } if((s->link==NULL)&&(strcmp(s->name,who.name))) { printf("\"%s\" 不存在!\n",who.name); printf("请查对一下姓名再查找.\n"); getch(); return; } clrscr(); printf("\n\t\t\t\要查找的资料如下所示:\n"); printf("\n******************************************************\n"); printf("\t 序号 姓名 性别 年龄 地址 电话 QQ号码 个性签名"); printf("\n******************************************************\n"); printf("\t %-6d%-10s%-8s%-7s%-8s%-10s%-s%-s\n",s->record,s->name,s->sex,s->age,s->age, s->addr,s->phone,s->qq,s->meg); printf("\n\t\t删除(d)\修改(m)\浏览一下(s)\n"); printf("\n\t请输入您的选择(d/m/s):"); scanf("%c",&c); switch(c) { case 'D': case 'd':Delete();break; case 'M': case 'm':Modify();break; } } void Modify(void) { getchar(); printf("\n\t\t请输入新的姓名:"); gets(s->name); printf("\n\t\t请输入新的性别:"); gets(s->sex); printf("\n\t\t请输入新的年龄:"); gets(s->age); printf("\n\t\t请输入新的地址:"); gets(s->addr); printf("\n\t\t请输入新的电话:"); gets(s->phone); printf("\n\t\t请输入新的QQ号码:"); gets(s->qq); printf("\n\t\t请输入新的个性签名:"); gets(s->meg); printf("\n\t\t修改成功!\n"); getch(); } void Delete(void)/*删除函数*/ { int find_prev; int find_record; char del; char del_member_name[15]; struct addbook *p_del_prev; strcpy(del_member_name,s->name); getchar(); printf("确定要删除%s吗(Y/N)?",del_member_name); scanf("%c",&del); if(del=='n'||del=='N') { return; } find_prev = s->record - 1; find_record = 1; p_del_prev = head; while((p_del_prev->link!=NULL)&&(find_record<find_prev)) { p_del_prev = p_del_prev->link; find_record++; } /* 如果删除{dy}位成员 */ if(s->record==1) { if(s->link!=NULL) { head = head->link; s = head; while(s!=NULL) { s->record = s->record - 1; s = s->link; } printf("%d,%s",p->record,p->name); } else if(s->link==NULL){ /* 如果{dy}位成员是{zh1}一位 */ p = NULL; head = p; if(remove("zzxx.txt")==0){ printf("您已经删除了{zh1}一位成员,通讯录已经清空!\n"); printf("所以,您必须重新建立通讯录!\n"); printf("重新打开程序去建立.\n"); } printf("\n\n\t\t按任意键退出.\n"); getch(); exit(0); } } /* 如果删除{zh1}一位成员 */ else if(s->record==p->record){ p_del_prev->link = NULL; s->link = NULL; p = p_del_prev; } else{ p_del_prev->link = s->link; s->link = NULL; while(p_del_prev->link!=NULL) { p_del_prev = p_del_prev->link; p_del_prev->record = p_del_prev->record -1; } printf("%d\n%d\n",p->record,p->link); } printf("成员 \"%s\" 已经被删除.\n ",del_member_name); getch(); } void Save(void)/*保存文件*/ { s = head; do{ fwrite(s,LEN,1,fp); s = s->link; }while(s!=NULL); } |