c语言个人通讯录(功能未全部完成)

来源:互联网 发布:网络与新媒体概论重点 编辑:程序博客网 时间:2024/06/07 07:22
#include <stdio.h>#include <string.h>#include <stdlib.h>#define N 20void initlist(struct Node **phead);void printlist(struct Node **phead);void insertlist(struct Node **phead);void searchList(struct Node **phead);void deletelist(struct Node **phead);void inserttree(struct TreeNode **root,struct Node **phead,int ch);typedef struct Node{char name[N];char phonenumber[N];char kinds[N];char birthday[N];struct Node *next;}Node;//////////////////////////////////////#define MAXLEN 100typedef struct TreeNode{    struct Node *list;struct TreeNode *left;struct TreeNode *right;}TreeNode; void inserttree(struct TreeNode **root,struct Node **phead,int ch){struct TreeNode *hroot = *root;struct Node *p = *phead;while(p != NULL){printf("name == %s\n",p->name);if(hroot == NULL){hroot = (struct TreeNode*)malloc(sizeof(struct TreeNode));hroot->list = p;hroot->left = NULL;hroot->right = NULL;*root = hroot;}else{struct TreeNode *temp = hroot;while(1){if(ch == 1){if(strcmp(temp->list->name,p->name) > 0){printf(">\n");if(temp->left == NULL)break;temp = temp->left;}else if(strcmp(temp->list->name,p->name) <= 0){printf("<\n");if(temp->right == NULL)break;temp = temp->right;}elsebreak;}if(ch == 2){if(strcmp(temp->list->phonenumber,p->phonenumber) > 0){printf(">\n");if(temp->left == NULL)break;temp = temp->left;}else if(strcmp(temp->list->phonenumber,p->phonenumber) <= 0){printf("<\n");if(temp->right == NULL)break;temp = temp->right;}elsebreak;}if(ch == 3){if(strcmp(temp->list->kinds,p->kinds) > 0){printf(">\n");if(temp->left == NULL)break;temp = temp->left;}else if(strcmp(temp->list->kinds,p->kinds) <= 0){printf("<\n");if(temp->right == NULL)break;temp = temp->right;}elsebreak;}if(ch == 4){if(strcmp(temp->list->birthday,p->birthday) > 0){printf(">\n");if(temp->left == NULL)break;temp = temp->left;}else if(strcmp(temp->list->birthday,p->birthday) <= 0){printf("<\n");if(temp->right == NULL)break;temp = temp->right;}elsebreak;}}struct TreeNode *newtree=(struct TreeNode*)malloc(sizeof(struct TreeNode));newtree->list = p;newtree->left = NULL;newtree->right = NULL;if(ch == 1){if(strcmp(temp->list->name,p->name) > 0){temp->left = newtree;}else if(strcmp(temp->list->name,p->name) <= 0){temp->right = newtree;}}if(ch == 2){if(strcmp(temp->list->phonenumber,p->phonenumber) > 0){temp->left = newtree;}else if(strcmp(temp->list->phonenumber,p->phonenumber) <= 0){temp->right = newtree;}}if(ch == 3){if(strcmp(temp->list->kinds,p->kinds) > 0){temp->left = newtree;}else if(strcmp(temp->list->kinds,p->kinds) <= 0){temp->right = newtree;}}if(ch == 4){if(strcmp(temp->list->birthday,p->birthday) > 0){temp->left = newtree;}else if(strcmp(temp->list->birthday,p->birthday) <= 0){temp->right = newtree;}}}p = p->next;}}////////////////////////////////////////////////////////////////////////////void deletelist(struct Node **phead){char name[N];struct Node *h = *phead;struct Node *p = *phead;struct Node *n = h->next;printf("ヌ・菠・靨ェノセウオトミユテ・」コ");scanf("%s",name);while(p->next != NULL){if(strcmp(name,p->name) == 0){struct Node *temp1 = h;p = h = h->next;n = n->next;*phead = h;free(temp1);break;}else if(strcmp(name,n->name) == 0 && strcmp(name,h->name) != 0){struct Node *temp2 = n;n = n->next;p->next = n;free(temp2);break;}p = p->next;n = p->next;}printf("ノセウウノケヲ\n");}void insertlist(struct Node **phead){struct Node *p = NULL;struct Node *temp = *phead;FILE *fp = NULL;p = (struct Node *)malloc(sizeof(struct Node));memset(p,0,sizeof(struct Node));printf("ヌ・菠・ツヤェマオネヒミナマ「\n");printf("ミユテ・」コ");scanf("%s",p->name);printf("オ扈ー 」コ");scanf("%s",p->phonenumber);printf("ラ魍・」コ");scanf("%s",p->kinds);printf("ノ抦ユ 」コ");scanf("%s",p->birthday);while(temp->next != NULL){temp = temp->next;}temp->next = p;fp = fopen("data.h","a");if(fp == NULL){printf("fopen error\n");}fprintf(fp,"%s %s %s %s",p->name,p->phonenumber,p->kinds,p->birthday);fclose(fp);printf("ウノケヲ\n");}void searchList(struct Node **phead){struct Node *p = *phead;char ch;char name[N];printf("ヌ・菠・靨ェイ鰉ッオトチェマオネヒミユテ犾n");scanf("%s",name);while(strcmp(p->name,name) != 0 && p->next != NULL){p = p->next;}if(p != NULL){printf("ミユテ・」コ");printf("%s\n",p->name);printf("オ扈ー 」コ");printf("%s\n",p->phonenumber);printf("ラ魍・」コ");printf("%s\n",p->kinds);printf("ノ抦ユ 」コ");printf("%s\n",p->birthday);printf("\n");printf("ハヌキミミ゙クト Y/N\n");scanf("%c",&ch);if(ch == 'Y'){printf("ミユテ鋗゙クト」ィ%s」ゥ 」コ",p->name);scanf("%s",p->name);printf("オ扈ーミ゙クト (%s)」コ",p->phonenumber);scanf("%s",p->phonenumber);printf("ラ魍゙クト (%s)」コ",p->kinds);scanf("%s",p->kinds);printf("ノ抦ユミ゙クト (%s)」コ",p->birthday);scanf("%s",p->birthday);}}else{printf("テサモミエヒネヒミナマ「\n");}}void initlist(struct Node **phead){struct Node *p = NULL;struct Node *q = NULL;struct Node *temp = *phead;FILE *fp = NULL;char name[N];char phonenumber[N];char kinds[N];char birthday[N];fp = fopen("data.h","r");if(fp == NULL){fp = fopen("data.h","w");fclose(fp);}p = q = (struct Node *)malloc(sizeof(struct Node));if(p == NULL || q == NULL){printf("malloc failed\n");exit(0);}memset(q,0,sizeof(struct Node));fscanf(fp,"%s %s %s %s",name,phonenumber,kinds,birthday);strcpy(p->name,name);strcpy(p->phonenumber,phonenumber);strcpy(p->kinds,kinds);strcpy(p->birthday,birthday);p->next = NULL;while(!feof(fp)){if(*phead != NULL){fscanf(fp,"%s %s %s %s",name,phonenumber,kinds,birthday);p = (struct Node *)malloc(sizeof(struct Node));if(p == NULL){printf("malloc failed\n");exit(0);}memset(p,0,sizeof(struct Node));strcpy(p->name,name);strcpy(p->phonenumber,phonenumber);strcpy(p->kinds,kinds);strcpy(p->birthday,birthday);p->next = NULL;}if(*phead == NULL){*phead = p;}else{q->next = p;q = p;}}fclose(fp);}void printlist(struct Node **phead){struct Node *p = *phead;if(NULL == p)       {        printf("ヘィミナツシホェソユ\n");    }    else    {printf("ヘィミナツシミナマ「ホェ」コ \n");while(p != NULL)        {printf("ミユテ・」コ");printf("%s\n",p->name);printf("オ扈ー 」コ");printf("%s\n",p->phonenumber);printf("ラ魍・」コ");printf("%s\n",p->kinds);printf("ノ抦ユ 」コ");printf("%s\n",p->birthday);            p = p->next;printf("\n");        }        printf("\n");    }}void print(struct TreeNode *root){if(root == NULL)return;else{print(root->left);printf("ミユテ・」コ");printf("%s\n",root->list->name);printf("オ扈ー 」コ");printf("%s\n",root->list->phonenumber);printf("ラ魍・」コ");printf("%s\n",root->list->kinds);printf("ノ抦ユ 」コ");printf("%s\n",root->list->birthday);printf("\n");printf("********************************************\n");print(root->right);return;}}int main(){struct Node *head = NULL;struct TreeNode *root = NULL;initlist(&head);printlist(&head);//insertlist(&head);//printlist(&head);//inserttree(&root,&head,3);//searchList(&head);deletelist(&head);printlist(&head);//print(root);return 0;}


0 0
原创粉丝点击