为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

c语言编写的简易通讯录 源代码.txt

2018-01-05 15页 doc 34KB 32阅读

用户头像

is_562397

暂无简介

举报
c语言编写的简易通讯录 源代码.txtc语言编写的简易通讯录 源代码.txt #include #include #include #include #include #define LEN sizeof(struct mylist) struct mylist { char name[10]; char num[15]; struct mylist* next; }; int n; ///用于记录总人数,保存时也为第一个参数 /*-----------------------------------共9+2个功能函数---------...
c语言编写的简易通讯录 源代码.txt
c语言编写的简易通讯录 源代码.txt #include #include #include #include #include #define LEN sizeof(struct mylist) struct mylist { char name[10]; char num[15]; struct mylist* next; }; int n; ///用于记录总人数,保存时也为第一个参数 /*-----------------------------------共9+2个功能函数-------------------------------------*/ struct mylist* Creat() ///////改为void { struct mylist* head=(struct mylist*)malloc(LEN); struct mylist *p1,*p2,*p3; head->next=NULL; n=0; p1=(struct mylist*)malloc(LEN); p1->next=NULL; p2=p1; p3=head; printf("请输入姓名:\n"); scanf("%s",p1->name); //printf("%s",p1->name); while(strcmp(p1->name,"000")!=0) //修改判断条 件 { n=n+1; if(n==1)head->next=p1; else {printf("及相应号码:\n"); scanf("%s",p1->num); p1=(struct mylist*)malloc(LEN); p3=p2; p2->next=p1; p2=p1; printf("请输入姓名:\n"); scanf("%s",&p1->name); }//else }//while n--; free(p2); p3->next=NULL; ////// return head; ///return(head)? } void Display(struct mylist* head) //显示函数 { struct mylist *p; printf("通讯录:(当前总人数:%d)\n",n); // p=head; if(head!=NULL) do { p=p->next; printf("%s %s\n",p->name,p->num); ///输出制符,可能有问题 }while(p->next!=NULL); } void Del(struct mylist *head,struct mylist *dp) //删除函数:选中--传 值--删除 { struct mylist *p=head; //p用于寻找dp的前一 个节点 while(p->next!=dp) {p=p->next;} //寻找dp的前一个节 点 p->next=dp->next; free(dp); printf("删除成功!\n"); //删除不会乱序,因此无需排序 n--; } void Insert(struct mylist* head) //插入函数:选中--传值--插入 { struct mylist *nw; nw=(struct mylist*)malloc(LEN); printf("请输入要添加的姓名:"); scanf("%s",&nw->name); printf(" 及号码:"); scanf("%s",&nw->num); nw->next=head->next; head->next=nw; n++; //只插入,需后跟排序函数 } void Insert2(struct mylist* head,char* name,char *num) //插入函数,只用于读出函数 { struct mylist *nw; nw=(struct mylist*)malloc(LEN); strcpy(nw->name,name); strcpy(nw->num,num); nw->next=head->next; head->next=nw; n++; //只插入,需后跟排序函数 } /*struct mylist* paixu(struct mylist* head) //排序函数,用于初始排序和插入后排序 { int i,j=0; struct mylist *p=head->next; //p用于移动寻找数据 struct mylist *q=(struct mylist*)malloc(LEN); //q用于交换时作为中间变量,同时用来建立新的链表,记得用完还要释放~~~ struct mylist *newhead=(struct mylist*)malloc(LEN); //用一个新的头结点串联起来排好序的链表 struct mylist *r1; //r1,r2用于新头的操作 struct mylist *r2; newhead->next=NULL; q->next=NULL; r1=r2=newhead; if(p->next!=NULL) { while(jname,p->next->name)<0) {strcpy(q->name,p->name); strcpy(q->num,p->num); strcpy(p->name,p->next->name); //普通的中间变量交换法,只交换数据 strcpy(p->num,p->next->num); strcpy(p->next->name,q->name); strcpy(p->next->num,q->num); p=p->next;}//if }//for r1=(struct mylist*)malloc(LEN); strcpy(r1->name,p->name); strcpy(r1->num,p->num); r2->next=r1; r2=r1; free(p); p=head->next; //将p置于头部 n--; //每循环一次,原链表就少一个元素 j++; }//while r1=(struct mylist*)malloc(LEN); strcpy(r1->name,p->name); //此时p应为head->next strcpy(r1->num,p->num); r1->next=NULL; //将最后一个元素 置空 r2->next=r1; free(p); free(head); free(q); return newhead; }//if else {return head;} }*/ void paixu(struct mylist* head) //排序函数,用于初始排序和插入后排序 { int i,j=0; int _n=n; struct mylist *p=head->next; //p用于移动寻找数据 struct mylist *q=(struct mylist*)malloc(LEN); //q用于交换时作为中间变量,同时用来建立新的链表,记得用完还要释放~~~ //struct mylist *newhead=(struct mylist*)malloc(LEN); //用一个新的头结点串联起来排好序的链表 //struct mylist *r1; //r1,r2用于新头的操作 //struct mylist *r2; //newhead->next=NULL; q->next=NULL; //r1=r2=newhead; if(p->next!=NULL) { while(jname,p->next->name)>0) {strcpy(q->name,p->name); strcpy(q->num,p->num); strcpy(p->name,p->next->name); //普通的中间变量交换法,只交换数据 strcpy(p->num,p->next->num); strcpy(p->next->name,q->name); strcpy(p->next->num,q->num);}//if p=p->next; }//for p=head->next; _n--; j++; }//while }//if //r1=(struct mylist*)malloc(LEN); //strcpy(r1->name,p->name); //此时p应为head->next //strcpy(r1->num,p->num); //r1->next=NULL; //将最后一个 元素置空 //r2->next=r1; //free(p); //free(head); free(q); } void Modify(struct mylist *p) //修改函数 { printf("请输入要修改后的姓名:"); scanf("%s",&p->name); printf(" 及号码:"); scanf("%s",&p->num); } void Seek(struct mylist *head,char* a) { struct mylist *p; int ctrl=0; p=head->next; while(p!=NULL) { if(strcmp(p->name,a)==0){printf("查询成功~姓名:%s 号码:%s\n",p->name,p->num);ctrl=1;} p=p->next; } if(!ctrl){printf("查无此人~");} } void menu2(struct mylist *head,struct mylist *p) //菜单2应实现基本操作的选择 {int op; if(p==NULL){printf("未正确选择,系统将退出~");exit(0);} printf("************************\n"); printf("** 请选择要进行的操作 *\n"); printf("** 1插入 *\n"); printf("** 2删除 *\n"); printf("** 3修改 *\n"); printf("** 4退出程序并保存 *\n"); printf("************************\n"); scanf("%d",&op); switch(op) { case 1:{Insert(head);paixu(head);printf("插入成功~\n");break;} case 2:{Del(head,p);printf("已删除~\n");break;} case 3:{printf("当前信息:姓名:%s号码:%s\n",p->name,p->num);Modify(p);paixu(head);printf("修改信息成功~\n");break;} case 4:{writeFile(head);exit(0);} } } void menu1(struct mylist *head) //菜单1应实现图标的运动,指针也应随之运动 {char a; //a用来控制方向输入 int i=0,j=0; //i用于指定指示符的位置,j用来记录循环次数 char sign=2; char mark=3; char*_name[10]; struct mylist *p,*_menu2; //_menu2用来向menu2()传递参数 _menu2=NULL; printf("\n%c提示:用“w”和“d”选择,k键确定:%c",mark,mark); printf("\n%c 查找请按“c” %c\n\n",mark,mark); Display(head); a=_getch(); //fflush(stdin); while(a!='k') { switch(a) { case 'w': { system("cls"); i--; printf("\n%c提示:用“w”和“d”选择,k键确定:%c",mark,mark); printf("\n%c 查找请按“c” %c\n\n",mark,mark); printf("通讯录:(当前总人数:%d)\n",n); p=head->next; while(p!=NULL) {j++; printf("%s %s",p->name,p->num); ///想输出制表符,可能有问题 if(i!=j)printf("\n"); else {printf(" %c\n",sign);_menu2=p;} p=p->next; }//while break; } case 's': { system("cls"); i++; printf("\n%c提示:用“w”和“d”选择,k键确定:%c",mark,mark); printf("\n%c 查找请按“c” %c\n\n",mark,mark); printf("通讯录:(当前总人数:%d)\n",n); // p=head->next; while(p!=NULL) {j++; printf("%s %s",p->name,p->num); ///想输出制表符,可能有问题 if(i!=j)printf("\n"); else {printf(" %c\n",sign);_menu2=p;} p=p->next; }//while break; }//case2 case 'c': system("cls"); printf("请输入要查找的姓名:"); scanf("%s",_name); Seek(head,_name); return; }//switch j=0; //循环次数清零 a=_getch(); //fflush(stdin); }//while1 menu2(head,_menu2); //最终操作是选中并打开一条信息 } int writeFile(struct mylist *head) //int代替bool { struct mylist *p; FILE *fp = fopen("phoneBook.txt", "w+"); if(NULL == fp) { printf("open the file failure!\n"); return 0; } for(p=head->next; NULL!=p; p=p->next) { fwrite(p->name, strlen(p->name), 1, fp); fseek(fp, (sizeof(p->name) - strlen(p->name)), SEEK_CUR); fwrite(p->num , strlen(p->num), 1, fp); fseek(fp, (sizeof(p->num) - strlen(p->num)), SEEK_CUR); fputc('\n',fp); } fclose(fp); return 1; } int readFile(struct mylist *head) { char name[10], num[15]; FILE *fp = fopen("phoneBook.txt", "r+"); if(NULL == fp) { printf("open the file failure!\n"); return 0; } while(fread(name, sizeof(name), 1, fp) > 0) { fread(num, sizeof(num), 1, fp); fseek(fp, 2, SEEK_CUR); Insert2(head,name,num); } fclose(fp); return 1; } void main() {char s; struct mylist *head=(struct mylist*)malloc(LEN); head->next=NULL; readFile(head); paixu(head); if(0==n){printf("通讯录为空,请初始化...(以姓名“000”结束)\n");head=Creat();paixu(head);writeFile(head);} do { system("cls"); menu1(head); printf("\n任意键返回,结束编辑并保存请输入'x'\n"); s=getch(); }while(s!='x'); writeFile(head); printf("谢谢使用~\n"); }
/
本文档为【c语言编写的简易通讯录 源代码&#46;txt】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索