C-通讯录
VS2017
#include <stdio.h> #include <windows.h> #include <stdlib.h> #include <conio.h> #pragma warning(disable:4996)
typedef struct Message { int id; //编号 char name[10]; //姓名 char city[10]; //城市 char capital[10]; //省会 char tel[13]; //电话 struct Message *next; }message;
#define LISTO "\n编 号 姓 名 城 市 省 会 电 话\n\n" #define LISTT "%6d %+12s %+13s %+13s %+14s\n\n", M_pp->id, M_pp->name, M_pp->city, M_pp->capital, M_pp->tel int M_save = 0; //保存标志 int M_id = 0; //编号 message *M_pheadnew = NULL; void M_insert(message **M_p); //录入信息 void M_delete(message **M_p); //删除信息 void M_display(message **M_p); //显示信息 void M_search(message **M_p); //查询信息 void M_psave(message **M_p); //保存信息 void M_main(void); //入口 void M_destroy(message **M_p); //销毁链表
int main(void) { message *M_phead = NULL; message *M_pp = NULL; message *M_p = NULL; message *M_ptemp = NULL; FILE *pf = NULL; pf = fopen("message.txt", "rb"); if (pf == NULL) { printf("读取文件失败!\n"); exit(1); } M_phead = (message *)malloc(sizeof(message)); M_phead->next = NULL; M_p = M_phead; M_pheadnew = M_phead; getc(pf); //读取一个字符,辅助下面的WHILE来判断是否到文件尾 if (feof(pf)) { rewind(pf);//将光标跳回到文件开头 } else { rewind(pf);//将光标跳回到文件开头 while (!feof(pf)) //判断文件是否到文件尾 { M_pp = (message *)malloc(sizeof(message)); fread(M_pp, sizeof(message), 1, pf); M_p->next = M_pp; M_ptemp = M_p; M_pp->next = NULL; M_p = M_pp; M_id++; } free(M_pp); M_pp = NULL; M_ptemp->next = NULL; M_id--; } fclose(pf); pf = NULL; M_main(); system("pause"); return 0; }
//入口 void M_main(void) { char key; int i; for (i = 0; i < 30; i++) { printf("*"); } printf(" 通讯录 "); for (i = 0; i < 30; i++) { printf("*"); } printf("\n\n"); printf("* 1> 录入信息 *\n"); printf("* 2> 删除信息 *\n"); printf("* 3> 显示信息 *\n"); printf("* 4> 查询信息 *\n"); printf("* 5> 保存信息 *\n"); printf("* 6> 退出 *\n"); printf("\n"); for (i = 0; i < 68; i++) { printf("*"); } printf("\n"); printf("请输入选项:"); scanf("%c", &key); getchar(); switch (key) { case '1': M_insert(&M_pheadnew); break; case '2': M_delete(&M_pheadnew); break; case '3': M_display(&M_pheadnew); break; case '4': M_search(&M_pheadnew); break; case '5': M_psave(&M_pheadnew); break; case '6': M_destroy(&M_pheadnew); exit(1); break; default: printf("输入有误,按任意键重新输入!\n"); getch(); system("cls"); M_main(); break; } }
//录入信息 void M_insert(message **M_p) { message *M_phead = NULL; message *M_pp = NULL; //分配新的节点 M_phead = *M_p; if (M_phead->next == NULL) { M_pp = (message *)malloc(sizeof(message)); printf("请输入姓名:\n"); scanf("%s", &M_pp->name); getchar(); if (strcmp(M_pp->name, "0") == 0) { free(M_pp); M_pp = NULL; printf("按任意键返回主界面!"); getch(); system("cls"); M_main(); } printf("请输入城市:\n"); scanf("%s", &M_pp->city); getchar(); printf("请输入省会:\n"); scanf("%s", &M_pp->capital); getchar(); printf("请输入电话号码:"); scanf("%s", &M_pp->tel); getchar(); M_id++; M_pp->id = M_id; printf(LISTO); printf(LISTT); M_phead->next = M_pp; M_pp->next = NULL; printf(">>录入成功!\n\n"); M_save = 1; } while (1) { while (M_phead->next != NULL) { M_phead = M_phead->next; } M_pp = (message *)malloc(sizeof(message)); printf("请输入姓名:\n"); scanf("%s", &M_pp->name); getchar(); if (strcmp(M_pp->name, "0") == 0) { free(M_pp); M_pp = NULL; printf("按任意键返回主界面!"); getch(); system("cls"); M_main(); } printf("请输入城市:\n"); scanf("%s", &M_pp->city); getchar(); printf("请输入省会:\n"); scanf("%s", &M_pp->capital); getchar(); printf("请输入电话号码:"); scanf("%s", &M_pp->tel); getchar(); M_id++; M_pp->id = M_id; printf(LISTO); printf(LISTT); M_pp->next = NULL; M_phead->next = M_pp; printf(">>录入成功!\n\n"); M_save = 1; } }
//删除信息 void M_delete(message **M_p) { char M_key[10]; message *M_phead = NULL; message *M_ptemp = NULL; //临时指针,做交换使用 M_phead = (*M_p); if (M_phead->next == NULL) //判断通讯录里有无信息 { printf("通讯录无信息!按任意键返回主界面!\n"); getch(); system("cls"); M_main(); } printf("请输入姓名:"); scanf("%s", M_key); getchar(); if (strcmp(M_key, "0") == 0) { printf("按任意键返回主界面!\n"); getch(); M_main(); } while (M_phead->next != NULL) { if (strcmp(M_phead->next->name, M_key) == 0) //搜索有没对应姓名 { if (M_phead->next->next == NULL) { M_ptemp = M_phead->next; free(M_ptemp); M_ptemp = NULL; M_phead->next = NULL; printf("删除成功!\n"); break; } else { M_ptemp = M_phead->next; M_phead->next = M_ptemp->next; free(M_ptemp); M_ptemp = NULL; M_save = 1; printf("删除成功!\n"); continue; } } M_phead = M_phead->next; } printf("没有可删除的信息!\n"); getch(); system("cls"); M_main(); }
//显示信息 void M_display(message **M_p) { message *M_pp = NULL; M_pp = (*M_p) ->next; if (M_pp == NULL) { printf("通讯录无信息!按任意键返回主界面!\n"); getch(); system("cls"); M_main(); } printf(LISTO); while (M_pp != NULL) { printf(LISTT); M_pp = M_pp->next; } printf("按任意键返回主界面!\n"); getch(); system("cls"); M_main(); }
//查询信息 void M_search(message **M_p) { char M_key[10]; message *M_pp = NULL; M_pp = (*M_p)->next; if (M_pp == NULL) //判断通讯录里有无信息 { printf("通讯录无信息!按任意键返回主界面!\n"); getch(); system("cls"); main(); } printf("请输入姓名:"); scanf("%s", M_key); if (strcmp(M_key, "0") == 0) { printf("按任意键返回主界面!\n"); getch(); system("cls"); main(); } while (M_pp != NULL) { if (strcmp(M_pp->name, M_key) == 0) //搜索有没对应姓名 { printf(LISTO); printf(LISTT); getchar(); } M_pp = M_pp->next; } printf("搜索结束!按任意键返回主界面!\n"); getch(); getch(); system("cls"); M_main(); }
//保存信息 void M_psave(message **M_p) { message *M_phead = NULL; FILE *pf; M_phead = (*M_p)->next; if (M_save == 1) { pf = fopen("message.txt", "wb"); if (pf == NULL) { printf("打开文件失败!按任意键返回主界面!\n"); getch(); main(); } while (M_phead != NULL) { fwrite(M_phead, sizeof(message), 1, pf); M_phead = M_phead->next; } fclose(pf); pf = NULL; printf("保存成功!按任意键返回主界面!\n"); getch(); system("cls"); M_main(); } else { printf("无需保存!按任意键返回主界面!\n"); getchar(); system("cls"); M_main(); } }
//销毁链表 void M_destroy(message **M_p) { message *M_phead = (*M_p); message *M_pp = NULL; while (M_phead != NULL) { M_pp = M_phead; M_phead = M_phead->next; free(M_pp); M_pp = NULL; } }