生活随笔
收集整理的這篇文章主要介紹了
C语言同学录(通讯录)的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這個很久以前就完成了,一直沒貼出來。
這個同學錄實際上就是通訊錄,使用鏈表實現(xiàn),也算是對鏈表的一種運用吧。
具體功能看Menu函數(shù)。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Student{char name[20];char tel[20];char qq[20];char address[20];struct Student *next;
}INFO;INFO *head;
void Initiate();//初始化
void Menu ();//主菜單
void Add ();//添加聯(lián)系人
void List ();//顯示通訊錄
void Alter();//修改
void Find ();//查找聯(lián)系人
void Find_Name ();//按姓名查找
void Find_Tel ();//按學號查找
void Delate ();//刪除聯(lián)系人
void Delate_Name ();
void Delate_Tel ();
/* void Delate_Name ();//按姓名刪除
void Delate_Tel ();//按電話刪除 *///初始化
void Initiate ()
{if((head=(INFO*)malloc(sizeof(INFO)))==NULL) exit(1);head->next=NULL;
}//主菜單
void Menu()
{printf ("\n");printf ("\n");printf ("\n");printf("**************歡迎使用通訊錄系統(tǒng)**************");printf("\n");printf("\n");printf("*********** 1.添加好友 ***********\n");printf("*********** 2.查詢好友 ***********\n");printf("*********** 3.修改好友 ***********\n");printf("*********** 4.刪除好友 ***********\n");printf("*********** 5.顯示所有好友 ***********\n");printf("*********** 0.退通訊錄 ***********\n\n\n");}
//添加通訊錄好友
void Add()
{INFO *p,*q;if ((q=(INFO *)malloc(sizeof(INFO)))==NULL) exit(1);printf("請輸入要添加信息!\n");printf("姓名:\n"); //添加信息scanf("%s",&q->name);printf("電話號碼:\n");scanf("%s",q->tel);printf("QQ:\n");scanf("%s",q->qq);printf("地址:\n");scanf("%s",q->address);//保存聊天記錄FILE *file_in = fopen("同學錄.txt", "w");if (file_in == NULL){perror("fopen TXT");return ;}fprintf (file_in, "姓名:\t%s\t", q->name);fprintf (file_in, "號碼:\t%s\t", q->tel);fprintf (file_in, "QQ:\t%s\t", q->qq);fprintf (file_in, "地址:\t%s\t", q->address);fclose (file_in);for (p = head;p->next!=NULL;p=p->next);p->next=q;q->next=NULL;printf("信息已添加完成!\n");}
//查找
void Find ()
{int i;printf ("\n\n\n\n\n\n**************************************\n");printf ("\t\t1按姓名查找\n");printf ("\t\t2按電話號碼查找\n");printf ("**************************************\n");printf ("請選擇:(按其他數(shù)字鍵返回!)");scanf ("%d",&i);switch (i){case 1:{Find_Name ();break;}case 2:{Find_Tel ();break;}}
}void Find_Name ()
{INFO *p;char name[20];if (head->next==NULL) // 如果是一個空鏈表;{printf("通訊錄空!\n");return;}printf("請輸入要查找姓名:\n");scanf("%s",&name);for (p=head->next;p!=NULL;p=p->next)//循環(huán)遍歷鏈表中的節(jié)點,找出此時最小的節(jié)點{if(strcmp(p->name,name)==0){printf("姓名\t電話\tQQ\t地址\n");printf("%s\t%s\t%s\t%s\n",p->name,p->tel,p->qq,p->address);}else if(p->next==NULL){printf("沒有該好友信息!\n");} }
}void Find_Tel ()
{INFO *p;char tel[20];if (head->next==NULL)// 如果是一個空鏈表;{printf("通訊錄空!\n");return;}printf("請輸入要查找電話號碼:\n");scanf("%s",&tel);for (p=head->next;p!=NULL;p=p->next)//循環(huán)遍歷鏈表中的節(jié)點,找出此時最小的節(jié)點{if(strcmp(p->tel,tel)==0){printf("姓名\t電話\tQQ\t地址\n");printf("%s\t%s\t%s\t%s\n",p->name,p->tel,p->qq,p->address);}else if(p->next==NULL){printf("沒有該好友信息!\n");} }
}
//修改通訊錄信息
void Alter()
{char name [50]; //先查找 INFO *p,*p1;if(head->next==NULL){printf("通訊錄空!\n");return;}printf("請輸入要修改姓名:\n");scanf("%s",name);for(p=head->next;p!=NULL;p=p->next){if(strcmp(p->name,name)==0){ break;}else if(p->next==NULL){printf("信息!\n");return;}}p1=(INFO *)malloc(sizeof(INFO));printf("new姓名:\n"); //添加信息scanf("%s",p1->name);strcpy(p->name,p1->name);printf("new電話號碼:\n");scanf("%s",p1->tel);strcpy(p->tel,p1->tel);printf("newQQ:\n");scanf("%s",p1->qq);strcpy(p->qq,p1->qq);printf("new地址:\n");scanf("%s",p1->address);strcpy(p->address,p1->address);printf("信息已修改\n");//顯示修改信息printf("姓名\t電話\tQQ\t地址\n");printf("%s\t%s\t%s\t%s\n",p->name,p->tel,p->qq,p->address);free(p1);
}
//刪除通訊錄信息
void Delate()
{int i;printf ("\n\n\n\n\n\n**************************************\n");printf ("\t\t1按姓名刪除\n");printf ("\t\t2按電話號碼刪除\n");printf ("**************************************\n");printf ("請選擇:(按其他數(shù)字鍵返回!)");scanf ("%d",&i);switch (i){case 1:{Delate_Name ();break;}case 2:{Delate_Tel ();break;}}
}
void Delate_Name()
{char name[20]; //先查找 刪除INFO *p = head->next, *p1 =head->next,*p2;if (head->next == NULL){printf("通訊錄空\n");return;}printf("請輸入要刪除姓名:\n");scanf(" %s",name);while ((strcmp(p->name,name)!=0 )&& p->next != NULL){p1=p;p =p->next;}if (strcmp(name, p->name)==0) //輸刪除信息{if (p == head->next&&p->next!=NULL){ head->next = p->next;} else if(p==head->next&&p->next==NULL){head->next=p->next;printf("好友已全部刪除!\n");return;}elsep1->next = p->next;}else{printf("不存在該好友!\n");return;}printf("信息已刪除!\n");printf("您還有以下好友!\n");printf("姓名\t電話號碼\tQQ\t地址\n");for(p2=head->next;p2!=NULL;p2=p2->next)printf("%s\t%s\t%s\t%s\n",p2->name,p2->tel,p2->qq,p2->address);
} void Delate_Tel()
{char tel[20]; //先查找 刪除INFO *p = head->next, *p1 =head->next,*p2;if (head->next == NULL){printf("通訊錄空\n");return;}printf("請輸入要刪除電話號碼:\n");scanf(" %s",tel);while ((strcmp(p->tel,tel)!=0 )&& p->next != NULL){p1=p;p =p->next;}if (strcmp(tel, p->tel)==0) //輸刪除信息{if (p == head->next&&p->next!=NULL){ head->next = p->next;} else if(p==head->next&&p->next==NULL){head->next=p->next;printf("好友已全部刪除!\n");return;}elsep1->next = p->next;}else{printf("不存在該好友!\n");return;}printf("信息已刪除!\n");printf("您還有以下好友!\n");printf("姓名\t電話號碼\tQQ\t地址\n");for(p2=head->next;p2!=NULL;p2=p2->next)printf("%s\t%s\t%s\t%s\n",p2->name,p2->tel,p2->qq,p2->address);}
//顯示所記錄
void List()
{ INFO *p;if(head->next==NULL){printf("通訊錄記錄!\n");return;}printf("姓名\t電話號碼\tQQ\t地址\n");for(p=head->next;p!=NULL;p=p->next)printf("%s\t%s\t\t%s\t%s\n",p->name,p->tel,p->qq,p->address);} int main()
{ int choice;char yes_no;Initiate();do{Menu();printf("請輸入指令0-5:\n");scanf("%d",&choice);printf("\n");switch(choice){case 1:Add();break;case 2:Find();break;case 3:Alter();break;case 4:Delate();break;case 5:List();break;case 0:printf("************謝您使用************\n");exit(0); //正常退出break;default:printf("輸入誤!請重新輸入\n");break;}printf("是否繼續(xù) Y or N? \n");do{scanf("%c",&yes_no);}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');}while(yes_no=='Y'||yes_no=='y');
}
總結(jié)
以上是生活随笔為你收集整理的C语言同学录(通讯录)的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。