嵌入式 一个简单的个人通讯录(基于二叉排序树,带注释)
来源:互联网 发布:在台湾可以用淘宝吗 编辑:程序博客网 时间:2024/05/20 03:45
TreeNode.h:
[csharp] viewplaincopyprint?
- #define N 50
- typedef structstudent
- {
- charname[N];
- charstudentID[N];
- charbrithday[N];
- chartele[N];
- }TreeData;
- typedef structtreenode
- {
- TreeData * stuinfo;
- structtreenode *leftchild;
- structtreenode *rightchild;
- }TREENODE;
Addressbook.h:
[csharp] viewplaincopyprint?
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include"Treenode.h"
- externTREENODE *mycreatetree();
- externvoidmyinsertnode(TREENODE *,TreeData *);
- externvoidmycopy(TreeData *,char*,char*,char*,char*);
- externvoidmyeva(TreeData *);
- externvoidpreorder(TREENODE *);
- externTREENODE*treeinit(TREENODE *);
- externTREENODE *mysearch(TREENODE *,char*);
- externintisfind(TREENODE *,char*);
- externintmycorrect(TREENODE *,char*);
- externTREENODE *findleaf(TREENODE *);
- externTREENODE *findparent(TREENODE *,TREENODE *);
- externvoidmyremove(TREENODE *,char*);
Addressbook.c:
[csharp] viewplaincopyprint?
- #include "Addressbook.h"
- void myeva(TreeData*dest)
- {
- printf("请输入你要添加的姓名:");
- scanf("%s",dest->name);
- while(1)
- {
- printf("请输入你要添加的学号(20209XXXX):");
- scanf("%s",dest->studentID);
- if((strlen(dest->studentID)== 9) &&(dest->studentID[0] =='2')&&(dest->studentID[1] =='0')&&(dest->studentID[2] =='2')&&(dest->studentID[3] =='0')&&(dest->studentID[4] =='9')&&((dest->studentID[5]<='9')&&(dest->studentID[5]>='0'))&&((dest->studentID[6]<='9')&&(dest->studentID[6]>='0'))&&((dest->studentID[7]<='9')&&(dest->studentID[7]>='0'))&&((dest->studentID[8]<='9')&&(dest->studentID[8]>='0')))
- {
- break;
- }
- else
- {
- printf("输入有误!\n");
- printf("您输入的数据必须为20209XXXX的格式(XXXX为可变数)\n");
- }
- }
- while(1)
- {
- printf("请输入你要添加的生日(XXXX-XX-XX):");
- scanf("%s",dest->brithday);
- if((strlen(dest->brithday)== 10) &&((dest->brithday[0] <='9')&&(dest->brithday[0]>='0'))&&((dest->brithday[1] <='9')&&(dest->brithday[1]>='0'))&&((dest->brithday[2] <='9')&&(dest->brithday[2]>='0'))&&((dest->brithday[3] <='9')&&(dest->brithday[3]>='0'))&&(dest->brithday[4] =='-')&&((dest->brithday[5]<='1')&&(dest->brithday[5]>='0'))&&((dest->brithday[6]<='9')&&(dest->brithday[6]>='0'))&&(dest->brithday[7]=='-')&&((dest->brithday[8]<='3')&&(dest->brithday[8]>='0'))&&((dest->brithday[9]<='9')&&(dest->brithday[9]>='0')))
- {
- if(dest->brithday[5]=='1')
- {
- if(dest->brithday[6]<='2')
- {
- ;
- }
- else
- {
- gotoloop;
- }
- }
- if(dest->brithday[8]=='3')
- {
- if(dest->brithday[9]<='1')
- {
- ;
- }
- else
- {
- gotoloop;
- }
- }
- break;
- }
- else
- {
- loop:
- printf("输入有误!\n");
- printf("您输入的数据必须为XXXX-XX-XX的格式\n");
- }
- }
- while(1)
- {
- printf("请输入你要添加的电话:");
- scanf("%s",dest->tele);
- if((strlen(dest->tele)==11)&&((dest->tele[0]<='9')&&(dest->tele[0]>='0'))&&((dest->tele[1]<='9')&&(dest->tele[1]>='0'))&&((dest->tele[2]<='9')&&(dest->tele[2]>='0'))&&((dest->tele[3]<='9')&&(dest->tele[3]>='0'))&&((dest->tele[4]<='9')&&(dest->tele[4]>='0'))&&((dest->tele[5]<='9')&&(dest->tele[5]>='0'))&&((dest->tele[6]<'9')&&(dest->tele[6]>='0'))&&((dest->tele[7]<='9')&&(dest->tele[7]>='0'))&&((dest->tele[8]<='9')&&(dest->tele[8]>='0'))&&((dest->tele[9]<='9')&&(dest->tele[9]>='0'))&&((dest->tele[10]<='9')&&(dest->tele[10]>='0')))
- {
- break;
- }
- else
- {
- printf("输入有误!\n");
- printf("您输入的数据必须为11位数字!\n");
- }
- }
- }
- void mycopy(TreeData*dest,char*name,char*studentID,char*brithday,char*tele)
- {
- strcpy(dest->name,name);
- strcpy(dest->studentID,studentID);
- strcpy(dest->brithday,brithday);
- strcpy(dest->tele,tele);
- }
- TREENODE * findleaf(TREENODE*root)
- {
- staticTREENODE * leaf =NULL;
- if(root != NULL)
- {
- if((root->leftchild== NULL) &&(root->rightchild == NULL))
- {
- leaf = root;
- returnleaf;
- }
- findleaf(root->leftchild);
- findleaf(root->rightchild);
- }
- returnleaf;
- }
- TREENODE *findparent(TREENODE *root,TREENODE*leaf)
- {
- staticTREENODE *parent =NULL;
- if(root->leftchild!= NULL)
- {
- if(root->leftchild== leaf)
- {
- parent = root;
- }
- else
- {
- findparent(root->leftchild,leaf);
- }
- }
- if(root->rightchild!= NULL)
- {
- if(root->rightchild== leaf)
- {
- parent = root;
- }
- else
- {
- findparent(root->rightchild,leaf);
- }
- }
- returnparent;
- }
- void myremove(TREENODE*root,char*info)
- {
- TREENODE *find = (TREENODE*)malloc(sizeof(TREENODE));
- TREENODE *leaf = (TREENODE *)malloc(sizeof(TREENODE));
- TREENODE *parent = (TREENODE*)malloc(sizeof(TREENODE));
- int ret =isfind(root,info);
- if(ret ==0)
- {
- printf("没有此人!\n");
- }
- else
- {
- find = mysearch(root,info);
- printf("确定要删除此学生:\n");
- printf("确定/取消?(Y/N):");
- char yn;
- setbuf(stdin,NULL);
- scanf("%c",&yn);
- if(yn =='Y')
- {
- leaf = findleaf(root);
- parent = findparent(root,leaf);
- if(leaf == find)
- {
- free(find);
- if(parent->leftchild== leaf)
- {
- parent->leftchild = NULL;
- }
- else
- {
- parent->rightchild = NULL;
- }
- find = NULL;
- }
- else
- {
- strcpy(find->stuinfo->name,leaf->stuinfo->name);
- strcpy(find->stuinfo->studentID,leaf->stuinfo->studentID);
- strcpy(find->stuinfo->brithday,leaf->stuinfo->brithday);
- strcpy(find->stuinfo->tele,leaf->stuinfo->tele);
- free(leaf);
- if(parent->leftchild== leaf)
- {
- parent->leftchild =NULL;
- }
- else
- {
- parent->rightchild =NULL;
- }
- leaf = NULL;
- }
- }
- }
- }
- TREENODE * mycreatetree()
- {
- TREENODE *root = NULL;
- returnroot;
- }
- void myinsertnode(TREENODE*root,TreeData *data)
- {
- if(strcmp(root->stuinfo->studentID,data->studentID)< 0)
- {
- if(root->leftchild== NULL)
- {
- TREENODE *new= (TREENODE*)malloc(sizeof(TREENODE));
- new->stuinfo= data;
- new->leftchild= NULL;
- new->rightchild= NULL;
- root->leftchild = new;
- }
- else
- {
- myinsertnode(root->leftchild,data);
- }
- }
- else
- {
- if(root->rightchild== NULL)
- {
- TREENODE *new= (TREENODE *)malloc(sizeof(TREENODE));
- new->stuinfo= data;
- new->leftchild= NULL;
- new->rightchild= NULL;
- root->rightchild =new;
- }
- else
- {
- myinsertnode(root->rightchild,data);
- }
- }
- }
- void preorder(TREENODE*root)
- {
- if(root !=NULL)
- {
- printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
- preorder(root->leftchild);
- preorder(root->rightchild);
- }
- }
- int isfind(TREENODE*root,char*info)
- {
- return((root != NULL)&&((strcmp(root->stuinfo->name,info) ==0) || (isfind(root->leftchild,info) ||isfind(root->rightchild,info))));
- }
- TREENODE * mysearch(TREENODE *root,char*info)
- {
- int ret =isfind(root,info);
- staticintflt = 0;
- staticTREENODE *find =NULL;
- if((ret == 0)&& (flt == 0))
- {
- printf("系统为找到此人!\n");
- return;
- }
- else
- {
- flt = 1;
- if(root !=NULL)
- {
- if(strcmp(root->stuinfo->name,info)== 0)
- {
- find = root;
- printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
- returnfind;
- }
- mysearch(root->leftchild,info);
- mysearch(root->rightchild,info);
- }
- }
- returnfind;
- }
- int mycorrect(TREENODE*root,char*info)
- {
- int ret =isfind(root,info);
- staticintflt = 0;
- if((ret == 0)&& (flt == 0))
- {
- printf("系统为找到此人!\n");
- printf("请按Enter键继续...");
- getchar();
- getchar();
- return;
- }
- else
- {
- flt = 1;
- if(root !=NULL)
- {
- if(strcmp(root->stuinfo->name,info)== 0)
- {
- printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- intchoose;
- char yn;
- char *buffer =(char*)malloc(sizeof(char));
- printf("请输入你想修改的内容或按5取消修改:");
- scanf("%d",&choose);
- while((choose> 5) || (choose <1))
- {
- printf("输入有误!\n");
- printf("请重新输入要修改的内容或按5取消:");
- scanf("%d",&choose);
- }
- switch(choose)
- {
- case 1:
- {
- printf("请输入要新的姓名:");
- scanf("%s",buffer);
- printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->name,buffer);
- setbuf(stdin,NULL);
- scanf("%c",&yn);
- if(yn =='Y')
- {
- strcpy(root->stuinfo->name,buffer);
- printf("修改成功\n");
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- getchar();
- }
- else
- {
- printf("数据没有修改!\n");
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- getchar();
- }
- break;
- }
- case 2:
- {
- printf("请输入要新的学号:");
- scanf("%s",buffer);
- printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->studentID,buffer);
- setbuf(stdin,NULL);
- scanf("%c",&yn);
- if(yn =='Y')
- {
- strcpy(root->stuinfo->studentID,buffer);
- printf("修改成功\n");
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- getchar();
- }
- else
- {
- printf("数据没有修改!\n");
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- getchar();
- }
- break;
- }
- case 3:
- {
- printf("请输入要新的生日:");
- scanf("%s",buffer);
- printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->brithday,buffer);
- setbuf(stdin,NULL);
- scanf("%c",&yn);
- if(yn =='Y')
- {
- strcpy(root->stuinfo->brithday,buffer);
- printf("修改成功\n");
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- getchar();
- }
- else
- {
- printf("数据没有修改!\n");
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- getchar();
- }
- break;
- }
- case 4:
- {
- printf("请输入要新的电话:");
- scanf("%s",buffer);
- printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->tele,buffer);
- setbuf(stdin,NULL);
- scanf("%c",&yn);
- if(yn =='Y')
- {
- strcpy(root->stuinfo->tele,buffer);
- printf("修改成功\n");
- printf("请按Enter键继续...");
- getchar();
- getchar();
- }
- else
- {
- printf("数据没有修改!\n");
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- getchar();
- }
- break;
- }
- case 5:
- {
- break;
- }
- }//switend
- goto end;
- } //if==end
- mycorrect(root->leftchild,info);
- mycorrect(root->rightchild,info);
- } // if==NULLend
- } //elseend
- end:
- return;
- }
- void display(TREENODE*root)
- {
- intchoose;
- system("clear");
- printf("\n\n\n\n\n\n\n\n\n\n\n");
- printf("\t\t\t欢迎使用通讯录管理系统!\n");
- printf("\n\n\n\n\n\n\n\n\n\n\n");
- sleep(2);
- system("clear");
- while(1)
- {
- system("clear");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("\n");
- printf("请输入您的选择(1-6):");
- scanf("%d",&choose);
- if((choose >6) || (choose < 1))
- {
- printf("输入有误,您的选择必须在1-5之间!\n");
- printf("请重新输入:");
- scanf("%d",&choose);
- }
- switch(choose)
- {
- case 1:
- {
- system("clear");
- preorder(root);
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- break;
- }
- case 2:
- {
- system("clear");
- char *info =(char*)malloc(sizeof(char));
- printf("请输入要修改的人的姓名:");
- scanf("%s",info);
- mycorrect(root,info);
- break;
- }
- case 3:
- {
- system("clear");
- TreeData *info = (TreeData*)malloc(sizeof(TreeData));
- myeva(info);
- printf("您确定要添加学生:\n");
- printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",info->name,info->studentID,info->brithday,info->tele);
- printf("确定/取消(Y/N)?");
- char yn;
- setbuf(stdin,NULL);
- scanf("%c",&yn);
- if(yn =='Y')
- {
- if(root ==NULL)
- {
- root = (TREENODE *)malloc(sizeof(TREENODE));
- root->stuinfo = info;
- root->leftchild =NULL;
- root->rightchild = NULL;
- }
- else
- {
- myinsertnode(root,info);
- }
- printf("添加成功!\n");
- }
- else
- {
- printf("取消添加!\n");
- }
- printf("请按Enter键继续...");
- getchar();
- getchar();
- break;
- }
- case 4:
- {
- system("clear");
- char *info =(char*)malloc(sizeof(char));
- printf("请输入要查找的人的姓名:");
- scanf("%s",info);
- mysearch(root,info);
- printf("请按Enter键继续...");
- setbuf(stdin,NULL);
- getchar();
- break;
- }
- case 5:
- {
- system("clear");
- char *info =(char*)malloc(sizeof(char));
- printf("请输入要删除的人的姓名:");
- scanf("%s",info);
- myremove(root,info);
- printf("请按Enter键继续...");
- getchar();
- getchar();
- break;
- }
- case 6:
- {
- system("clear");
- printf("\n\n\n\n\n\n\n\n\n\n\n");
- printf("\t\t\t\t欢迎再次使用!\n");
- printf("\n\n\n\n\n\n\n\n\n\n\n");
- sleep(2);
- system("clear");
- exit(0);
- }
- default:
- {
- ;
- }
- }
- }
- }
- TREENODE * treeinit(TREENODE *root)
- {
- TreeData * info[4];
- int i;
- for(i = 0; i< 4; i++)
- {
- if((info[i] = (TreeData*)malloc(sizeof(TreeData))) ==NULL)
- {
- printf("分配空间失败!\n");
- }
- }
- mycopy(info[0],"张一","202090116","1990-10-11","15298377000");
- mycopy(info[1],"张二","202090114","1990-10-12","15298377111");
- mycopy(info[2],"张三","202090115","1990-10-13","15298377222");
- mycopy(info[3],"张四","202090118","1990-10-14","15298377333");
- root = (TREENODE *)malloc(sizeof(TREENODE));
- root->stuinfo =info[0];
- root->leftchild = NULL;
- root->rightchild =NULL;
- myinsertnode(root,info[1]);
- myinsertnode(root,info[2]);
- myinsertnode(root,info[3]);
- returnroot;
- }
- intmain()
- {
- TREENODE * root = NULL;
- root = mycreatetree();
- root = treeinit(root);
- display(root);
- return0;
- }
- 嵌入式 一个简单的个人通讯录(基于二叉排序树,带注释)
- 一个简单的个人通讯录(基于二叉排序树)(加注释版~~~~)
- 基于二叉排序树写的通讯录
- 二叉排序树实现简单的通讯录
- 基于j2me的个人通讯录
- 二叉排序树的应用(基于二叉排序树的个人通信录)
- 二叉排序树的应用(基于二叉排序树的个人通信录)
- 一个简单的通讯录
- 一个简单的通讯录。。。
- 嵌入式 一个基于linux的聊天软件(并发服务器,sqlite3数据库,有注释)
- 二叉排序树的一个简单实现
- 一个简单的通讯录程序
- 用类写一个简单的通讯录
- 实现一个简单的通讯录
- 基于sqlite3实现的简单通讯录程序(控制台)
- 基于sqlite3实现的简单通讯录程序(控制台)
- 基于UITableView写的简单通讯录
- 【汇编语言】写一个简单的通讯录
- 嵌入式 makefile编译静态库和动态库(
- 嵌入式 一个基于linux的聊天软件(并发服务器,sqlite3数据库,有注释)
- HTTP协议详解
- 嵌入式 select用于实现I/O多路复用示例
- 嵌入式 线性表(顺序表,链表)示例
- 嵌入式 一个简单的个人通讯录(基于二叉排序树,带注释)
- 嵌入式 用链表实现直接选择排序和直接插入排序示例
- 嵌入式 C语言中.h和.c文件解析经典收藏
- 嵌入式 实现自己的printf函数小示例
- 嵌入式 关于scanf("%c",&ch)实现拦截时候出现直接跳过的问题
- 嵌入式 二叉树实现通讯录管理系统
- 嵌入式 一句语言实现查找某个结点是否在二叉树里
- 嵌入式 c语言exit和return在程序中表现的区别
- 嵌入式 VI常用命令整理