#include "linklist.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #define FALSE 0 #define TRUE 1 typedef struct _message { int id; int tel; int com_tel; char name[20]; char address[30]; }Age; typedef struct _node { struct _message data; struct _node * next; }Node; Node * Create_List(); void interface1(); int information(Node *h); int Insert_Last(Node *h,Node *N); int Find_Element(Node* h); int Delete_Data(Node*h); int outlist(Node *h); Node * Create_List() { Node *list = (Node*)malloc(sizeof(Node)/sizeof(char)); if (list == NULL) return NULL; list->next = NULL; // 空表 return list; } int information(Node *h) { if(h == NULL) { return FALSE; } Node *node = Create_List(); system("clear"); printf("请输入名字(英文):"); scanf("%s",node->data.name); printf("请输入ID:"); scanf("%d",&node->data.id); printf("请输入电话:"); scanf("%d",&node->data.tel); printf("请输入家庭住址:"); scanf("%s",node->data.address); printf("请输入公司电话:"); scanf("%d",&node->data.com_tel); Insert_Last(h,node); free(node); return TRUE; } int Insert_Last(Node *h,Node *N) { if (h == NULL) return FALSE; Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } node->data = N->data; node->next = NULL; Node* tmp = h; while (tmp->next) { tmp = tmp->next; } tmp->next = node; return TRUE; } int Delete_Data(Node* h) { if (h == NULL) return FALSE; Node *tmp = h->next; int count = 0; char name[20]; printf("请输入目标名字:"); scanf("%s",name); while (tmp) { if (strcmp(tmp->data.name,name) == 0) { printf("姓名:%s\n",tmp->data.name); printf("id:%d\n",tmp->data.id); printf("家庭住址:%s\n",tmp->data.address); printf("电话:%d\n",tmp->data.tel); printf("公司电话:%d\n",tmp->data.com_tel); count++; } tmp = tmp->next; } Node *pre = h->next; if (count == 0) return FALSE; if(count == 1) { while (pre) { if(strcmp(pre->data.name,name) == 0) { Node *p = pre; pre = p->next; free(p); return TRUE; } pre = pre->next; } } Node *cur = h; if(count > 1) { int id; printf("请输入ID号:"); scanf("%d",&id); while (cur->next) { if (cur->next->data.id == id) break; cur = cur->next; } Node *p = cur->next; cur = p->next; free(p); } return TRUE; } int Find_Element(Node* h) { if (h == NULL) { printf("该表为空表\n"); return FALSE; } int count = 0; char name[20]; printf("请输入目标名字:"); scanf("%s",name); Node *tmp = h->next; while (tmp) { if (strcmp(tmp->data.name, name)== 0) { printf("姓名:%s\n",tmp->data.name); printf("id:%d\n",tmp->data.id); printf("家庭住址:%s\n",tmp->data.address); printf("电话:%d\n",tmp->data.tel); printf("公司电话:%d\n",tmp->data.com_tel); count++; } tmp = tmp->next; } if(count) return TRUE; else return FALSE; } int outlist(Node *h) { if(h == NULL || h->next == NULL) { return FALSE; } system("clear"); if(h->next->next == NULL) { printf("姓名:%s\n",h->next->data.name); printf("id:%d\n",h->next->data.id); printf("家庭住址:%s\n",h->next->data.address); printf("电话:%d\n",h->next->data.tel); printf("公司电话:%d\n",h->next->data.com_tel); return TRUE; } Node *pre = h->next; Node *cur = pre->next; //排序 while(pre->next) { while(cur) { if(pre->data.id > cur->data.id) { Age tmp= pre->data; pre->data = cur->data; cur->data = tmp; } cur = cur->next; } pre = pre->next; cur = pre->next; } while(h->next) { printf("姓名:%s\n",h->next->data.name); printf("id:%d\n",h->next->data.id); printf("家庭住址:%s\n",h->next->data.address); printf("电话:%d\n",h->next->data.tel); printf("公司电话:%d\n",h->next->data.com_tel); h = h->next; } return TRUE; } void interface1() { system("clear"); printf("\t*******************************************\n"); printf("\t* *\n"); printf("\t* 1.添加好友 2.列表好友信息 *\n"); printf("\t* *\n"); printf("\t* *\n"); printf("\t* 3.搜索好友 4.删除好友 *\n"); printf("\t* *\n"); printf("\t* 0.退出程序 *\n"); printf("\t*******************************************\n"); }
总结:电子通讯录的总体框架就是运用链表,链表的指针域指向下一个节点,而数据域用来存储你要存储的那些数据。int main() { int i,x; Node *head = Create_List(); Node *tmp = head; FILE *fp2; FILE *fp1 = fopen ("通讯录.txt", "ab+"); if(fp1 == NULL) { printf("fopen"); return FALSE; } int count; int ret; ret = fread (&count, sizeof(int), 1, fp1); if(ret != 0) { for (i = 0; i < count; i++) { Node *node = (Node *)malloc(sizeof(Node)/sizeof(char)); int len; fread (&len, sizeof(int), 1, fp1); // 读取数据 fread (&(node->data), len, 1, fp1); node->next = NULL; while (tmp->next) { tmp = tmp->next; } tmp->next = node; } } if(ret ==0 && !feof(fp1)) { perror("fread"); return FALSE; } close(fp1); while(1) { interface1(); printf("\t请输入数字(0~4):"); scanf("%d",&x); switch(x) { case 1: if(information(head)) { printf("\t信息输入成功\n"); } else printf("\t信息输入失败\n"); break; case 2: if(!outlist(head)) { printf("\t没有信息\n"); } break; case 3: system("clear"); if(!Find_Element(head)) { printf("\t未搜索到指定目标!\n"); } break; case 4: system("clear"); if(Delete_Data(head)) { printf("\t删除成功\n"); } else { printf("\t未找到指定好友!\n"); } break; case 0: fp2 = fopen ("通讯录.txt", "wb+"); if(fp2 == NULL) { printf("fopen"); return FALSE; } tmp = head->next; count = 0; while(tmp) { count++; tmp = tmp->next; } fwrite(&count, sizeof(int), 1, fp2); tmp = head; while(tmp->next) { Node *p = tmp->next; tmp->next = p->next; int len = sizeof(p->data); //写入数据的长度 fwrite(&len,sizeof(int), 1, fp2); //写入数据 fwrite(&(p->data),sizeof(Age), 1, fp2); free(p); } close(fp2); exit(0); break; } printf("\t请输入回车键返回主页面\n"); getchar(); getchar(); } return 0; }
c语言之电子通讯录
最新推荐文章于 2024-07-24 17:46:08 发布