|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 猪猪虾 于 2021-1-9 13:30 编辑
主程序太长了,可以不用看,看最下面的main函数里面的switch就行,图片在最下方
- #include<winuser.inl>
- #include<stdio.h>
- #include <cstdlib>
- #include<string.h>
- void tail_add(struct People** head);
- struct People
- {
- char name[20];
- char phone[20];
- struct People *next;
- };
- void getInput(struct People* people,int wether_change_phone = 0)
- {
- printf("enter name:");
- scanf_s("%s", people->name, 20);
- if (wether_change_phone == 0)
- {
- printf("enter phone:");
- scanf_s("%s", people->phone, 20);
- }
- else
- {
- printf("enter new phone:");
- scanf_s("%s", people->phone, 20);
- }
- }
- void printfInfo(struct People *head)
- {
- struct People* people;
- people = head;
- printf("name: %s \n", people->name);
- printf("phone:%s \n", people->phone);
- }
- void tail_add(struct People** head)
- {
- struct People* people,*temp;
- people = (struct People*)(malloc(sizeof(struct People))); //动态申请结构体
- if (people == NULL)
- {
- printf("memory failed");
- exit(1);
- }
- getInput(people,0);
- //找尾节点
- if (*head == NULL)
- {
- *head = people;
- people -> next = NULL;
- }
- else
- {
- temp = *head;
- while (temp->next != NULL)
- {
- temp = temp->next;
- }
- temp->next = people;
- people -> next = NULL;
- }
- }
- void find(struct People** head,char find_name[])
- {
- struct People * people;
- people = *head;
- //判断书否为空链表,为空,直接结束
- if (people == NULL)
- {
- printf("no contact people!!!!\n");
- }
- else
- {
- while (people != NULL)
- {
- if (!strcmp( people->name, find_name))
- {
- printfInfo(people);
- break;
- }
- else
- {
- people = people->next;
- }
- }
- }
- //while循环查找是否有符合条件的人或者到达链表的末端,跳出循环,
- //找到相应的人,打印信息
- }
- void chang_info(struct People** head, char find_name[])
- {
- struct People* people;
- people = *head;
- //遍历链表,找到姓名相同的联系人,修改其信息
- if (people == NULL)
- {
- printf("no contact people!!!!\n");
- }
- else
- {
- while (people != NULL)
- {
- if (!strcmp(people->name, find_name))
- {
- getInput(people, 1);
- break;
- }
- else
- {
- people = people->next;
- }
- }
- }
- }
- void delete_info(struct People** head, char delete_name[])
- {
- struct People* previous, * current;
- //直接给previous赋值null,current赋值头节点,后面就不需要再用中间变量来存储之后,再进行交换地址了
- //current此时就是一个空的结构体,头指针给他之后,他指向了链表的头节点
- current = *head;
- previous = NULL;
- //找到删除值的位置的前后节点的位置
- while (current != NULL && current->name != delete_name)
- {
- previous = current;
- current = current->next;
- }
- //改变链表节点的指向,删除节点
- if (current == NULL) //空链表,直接将头指针指向他
- {
- printf("not found anything\n");
- }
- else
- {
- if (previous == NULL) //链表只有一个节点
- {
- *head = current->next;
- }
- else
- {
- previous->next = current->next;//前面用的是current 来判断有没有找到要删除的节点的位置,所以删除就是currennt的节点指向下一个
- }
- free(current);
- }
- }
- void show_all(struct People** head)
- {
- struct People* people;
- people = *head;
- printf("all people in the phonebook is:\n");
- while (people != NULL)
- {
- printfInfo(people);
- people = people->next;
- }
- printf("\n");
-
- }
- int main()
- {
- printf("---欢迎使用通讯录管理程序--\n");
- printf("---1:插入新的联系人-----\n");
- printf("---2:查找已有的联系人---\n");
- printf("---3更改已有的联系人----\n");
- printf("---4:删除已有的联系人---\n");
- printf("---5:显示当前通讯录----\n");
- printf("---6:显退出当前通讯录---\n");
- printf("\n");
- int order;
- char name[20];
- struct People *phonebook = NULL;
- do
- {
- printf("请输入指令:\n");
- scanf_s("%d", &order);
- switch (order)
- {
- case 1:
- tail_add(&phonebook);
- case 2:
- printf("enter the name you want to find:\n");
- scanf_s("%s",name,20);
- find(&phonebook,name);
- case 3:
- printf("enter the name you want to change:\n");
- scanf_s("%s", name, 20);
- chang_info(&phonebook, name);
- case 4:
- printf("enter the name you want to delete:\n");
- scanf_s("%s", name, 20);
- delete_info(&phonebook, name);
- case 5:
- show_all(&phonebook);
- case 6:
- printf("退出界面!!1");
- exit(1);
- default:
- printf("wrong enter !!!!!!");
- }
- } while (order != 6);
- }
复制代码
本帖最后由 jackz007 于 2021-1-9 13:38 编辑
只要程序不退出,每一个 case 语句块的最后一条语句必须是 break,否则的话,就会从进入点开始,顺序执行完遇到的每一条语句,而不会在意这些语句是否属于本 case。
- switch (order)
- {
- case 1:
- tail_add(&phonebook);
- break ;
- case 2:
- printf("enter the name you want to find:\n");
- scanf_s("%s",name,20);
- find(&phonebook,name);
- break ;
- case 3:
- printf("enter the name you want to change:\n");
- scanf_s("%s", name, 20);
- chang_info(&phonebook, name);
- break ;
- case 4:
- printf("enter the name you want to delete:\n");
- scanf_s("%s", name, 20);
- delete_info(&phonebook, name);
- break ;
- case 5:
- show_all(&phonebook);
- break ;
- case 6:
- printf("退出界面!!1");
- exit(1);
- default:
- printf("wrong enter !!!!!!");
- break ;
- }
复制代码
|
-
|