通讯录

来源:互联网 发布:决战武林雨扇进阶数据 编辑:程序博客网 时间:2024/05/22 04:26
/**********************************************************File Name:         通讯录项目Author:            xxx      Date:2016-12-04Description: 实现通讯录功能Fuction List:       show()    显示最初布局Init_list() 添加好友信息Print() 列表好友信息Search() 搜索好友Delete()  删除好友************************************************************/#include#include#define ok          0#define error      -1#define malloc_error  -2#define N             20typedef struct node{int ID;char name[N];char tel[N ];char address[N];char company_phone[N];struct node *next;}Address;typedef Address* PAddress;//显示最初布局void show(){system("clear");printf("\t\t*********************************************\n");printf("\t\t* \twelcome to address book             *\n");printf("\t\t*                                           *\n");printf("\t\t*\tA) Add the information of friends   *\n");printf("\t\t*\tB) Show all friends information     *\n");printf("\t\t*\tC) Search for the friend            *\n");printf("\t\t*\tD) Remove the friend           *\n");printf("\t\t*                                           *\n");printf("\t\t*********************************************\n");printf("                                         \n");printf("                                         \n");printf("                                         \n");printf("                                         \n");printf("         please input your choice:");}//添加好友信息int Init_list(PAddress h,int i){if(h == NULL){return error;}PAddress p = (PAddress)malloc(sizeof(Address)/sizeof(char));if (p == NULL){return malloc_error;}system("clear");p->ID = i;                             printf("\nplease input the name:");scanf("%s", p->name);                             //输入姓名printf("\nplease input the tel:");scanf("%s", p->tel);                           //输入手机号int j = 0;int len = 0;while(p->tel[j++] != '\0'){len++;}while(len != 11)                //判断输入的手机号码是不是11位的{printf("\nplease input 11 numbers,input again:");scanf("%s", p->tel);len = 0;j = 0;while(p->tel[j++] != '\0'){len++;}}printf("\nplease input the address:");scanf("%s", p->address);printf("\nplease input the company phone:");scanf("%s", p->company_phone);j = 0;len = 0;while(p->company_phone[j++] != '\0'){len++;}while(len != 8)             //判断输入的电话号码是不是8位的{printf("\nplease input 8 numbers,input again:");scanf("%s", p->company_phone);len = 0;j = 0;while(p->company_phone[j++] != '\0'){len++;}}p->next = NULL;PAddress temp = h;while (temp->next){temp = temp->next;}temp->next = p;return ok;}//列表好友信息void Print(PAddress h){if(h == NULL){return;}if(h->next == NULL)                  //判断链表是否为空{printf("\n\n");printf("There is no friend!\n");printf("\n\n");}PAddress temp = h->next;while(temp){printf("Num:%d  name:%s  tel:%s  address:%s  company phone:%s\n", temp->ID, temp->name, temp->tel, temp->address, temp->company_phone);temp = temp->next;}printf("\n");}//搜索好友int Search(PAddress h,int i){if(h == NULL){return error;}int flag = 1;PAddress temp = h->next;while(temp){if(temp->ID == i){flag = 0;printf("\n\n");printf("Num:%d  name:%s  tel:%s  address:%s  company phone:%s\n", temp->ID, temp->name, temp->tel, temp->address, temp->company_phone);printf("\n\n");}temp = temp->next;}if(flag){printf("\n\n");printf("Don't have the friend\n");printf("\n\n");}return ok;}//删除好友int Delete(PAddress h, int i){if(h == NULL){return error;}PAddress temp = h;if(temp->next == NULL)                //空表        {return error;}if(temp->next->next == NULL)             //一个结点的表{if(temp->next->ID == i){PAddress tmp = temp->next;temp->next = NULL;free(tmp);printf("\n\n");printf("Deleted successfully\n");printf("\n\n");return ok;}else{return error;}} while(temp)                     // 此处temp 不能写temp->next  要考虑删除的是最后一个结点{if(temp->next->ID == i){PAddress p = temp->next;temp->next = p->next;free(p);printf("\n\n");printf("Deleted successfully\n");printf("\n\n");return ok;}if(temp->next->next == NULL )         //考虑i超出了表中元素的时候{return error;}temp = temp->next;}}int main(){char option[2];                              //定义一个字符串存放输入的选项  去除回车键对循环的影响int back = 1;int idd = 1;PAddress head_node = (PAddress)malloc(sizeof(Address)/sizeof(char));     //定义头结点if(head_node == NULL){return malloc_error;}head_node->next = NULL;while(back--){show();scanf("%s", option); switch (option[0]) { case 'A' : {Init_list(head_node,idd++);system("clear");printf("\n\n\n\n\n\n");printf("Congratulations on your success to create contacts!\n");printf("\n\n\n\n\n\n");printf("If you want to back,please input (1):");scanf("%d", &back);break;}case 'B' :{back = 0;system("clear");printf("\n\n\n\n\n\n");Print(head_node);printf("If you want to back,please input (1):");scanf("%d", &back);break;}case 'C' :{int id;back = 0;system("clear");printf("\n\n\n\n\n\n");printf("if you want to search your friend,please input ID:");scanf("%d",&id);Search(head_node,id);printf("If you want to back,please input (1):");scanf("%d", &back);break;}case 'D' :{int id;back = 0;system("clear");printf("\n\n\n\n\n\n");printf("if you want to delete your friend,please input ID:");scanf("%d",&id);if (Delete(head_node,id) != ok){printf("\n\n");printf("num %d friend is not exist!", id);printf("\n\n");}printf("If you want to back,please input (1):");scanf("%d", &back);break;}default :{printf("\n\n");system("clear");printf("\n\n\n\n\n\n");printf("\t\tplaese input A、B、C or D!\n");printf("\n\n");printf("If you want to back,please input (1):");scanf("%d", &back);break;} }}return 0;}
原创粉丝点击