关闭

嵌入式 一个简单的个人通讯录(基于二叉排序树,带注释)

600人阅读 评论(0) 收藏 举报
分类:

TreeNode.h:

  1. #define N 50
  2. typedef structstudent
  3. {
  4. charname[N];
  5. charstudentID[N];
  6. charbrithday[N];
  7. chartele[N];
  8. }TreeData;
  9. typedef structtreenode
  10. {
  11. TreeData * stuinfo;
  12. struct treenode *leftchild;
  13. struct treenode *rightchild;
  14. }TREENODE;
#define N 50

typedef struct student
{
    char name[N];
    char studentID[N];
    char brithday[N];
    char tele[N];
}TreeData;

typedef struct treenode
{
    TreeData * stuinfo;
    struct treenode * leftchild;
    struct treenode * rightchild;
}TREENODE;

Addressbook.h:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include"Treenode.h"
  5. extern TREENODE *mycreatetree();
  6. extern voidmyinsertnode(TREENODE *,TreeData *);
  7. extern voidmycopy(TreeData *,char*,char *,char*,char *);
  8. extern voidmyeva(TreeData *);
  9. extern voidpreorder(TREENODE *);
  10. extern TREENODE*treeinit(TREENODE *);
  11. extern TREENODE *mysearch(TREENODE *,char*);
  12. extern intisfind(TREENODE *,char*);
  13. extern intmycorrect(TREENODE *,char*);
  14. extern TREENODE *findleaf(TREENODE *);
  15. extern TREENODE *findparent(TREENODE *,TREENODE *);
  16. extern voidmyremove(TREENODE *,char*);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Treenode.h"

extern TREENODE * mycreatetree();
extern void myinsertnode(TREENODE *,TreeData *);
extern void mycopy(TreeData *,char *,char *,char *,char *);
extern void myeva(TreeData *);
extern void preorder(TREENODE *);
extern TREENODE *treeinit(TREENODE *);
extern TREENODE * mysearch(TREENODE *,char *);
extern int isfind(TREENODE *,char *);
extern int mycorrect(TREENODE *,char *);
extern TREENODE * findleaf(TREENODE *);
extern TREENODE * findparent(TREENODE *,TREENODE *);
extern void myremove(TREENODE *,char *);

Addressbook.c:

  1. #include "Addressbook.h"
  2. void myeva(TreeData*dest)
  3. {
  4. printf("请输入你要添加的姓名:");
  5. scanf("%s",dest->name);
  6. while(1)
  7. {
  8. printf("请输入你要添加的学号(20209XXXX):");
  9. scanf("%s",dest->studentID);
  10. 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')))
  11. {
  12. break;
  13. }
  14. else
  15. {
  16. printf("输入有误!\n");
  17. printf("您输入的数据必须为20209XXXX的格式(XXXX为可变数)\n");
  18. }
  19. }
  20. while(1)
  21. {
  22. printf("请输入你要添加的生日(XXXX-XX-XX):");
  23. scanf("%s",dest->brithday);
  24. 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')))
  25. {
  26. if(dest->brithday[5]== '1')
  27. {
  28. if(dest->brithday[6]<= '2')
  29. {
  30. ;
  31. }
  32. else
  33. {
  34. gotoloop;
  35. }
  36. }
  37. if(dest->brithday[8]== '3')
  38. {
  39. if(dest->brithday[9]<= '1')
  40. {
  41. ;
  42. }
  43. else
  44. {
  45. gotoloop;
  46. }
  47. }
  48. break;
  49. }
  50. else
  51. {
  52. loop:
  53. printf("输入有误!\n");
  54. printf("您输入的数据必须为XXXX-XX-XX的格式\n");
  55. }
  56. }
  57. while(1)
  58. {
  59. printf("请输入你要添加的电话:");
  60. scanf("%s",dest->tele);
  61. 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')))
  62. {
  63. break;
  64. }
  65. else
  66. {
  67. printf("输入有误!\n");
  68. printf("您输入的数据必须为11位数字!\n");
  69. }
  70. }
  71. }
  72. void mycopy(TreeData*dest,char*name,char*studentID,char*brithday,char*tele)
  73. {
  74. strcpy(dest->name,name);
  75. strcpy(dest->studentID,studentID);
  76. strcpy(dest->brithday,brithday);
  77. strcpy(dest->tele,tele);
  78. }
  79. TREENODE * findleaf(TREENODE*root)
  80. {
  81. static TREENODE * leaf =NULL;
  82. if(root != NULL)
  83. {
  84. if((root->leftchild== NULL) &&(root->rightchild == NULL))
  85. {
  86. leaf = root;
  87. returnleaf;
  88. }
  89. findleaf(root->leftchild);
  90. findleaf(root->rightchild);
  91. }
  92. returnleaf;
  93. }
  94. TREENODE *findparent(TREENODE *root,TREENODE*leaf)
  95. {
  96. static TREENODE *parent =NULL;
  97. if(root->leftchild!= NULL)
  98. {
  99. if(root->leftchild== leaf)
  100. {
  101. parent = root;
  102. }
  103. else
  104. {
  105. findparent(root->leftchild,leaf);
  106. }
  107. }
  108. if(root->rightchild!= NULL)
  109. {
  110. if(root->rightchild== leaf)
  111. {
  112. parent = root;
  113. }
  114. else
  115. {
  116. findparent(root->rightchild,leaf);
  117. }
  118. }
  119. returnparent;
  120. }
  121. void myremove(TREENODE*root,char*info)
  122. {
  123. TREENODE *find = (TREENODE*)malloc(sizeof(TREENODE));
  124. TREENODE *leaf = (TREENODE *)malloc(sizeof(TREENODE));
  125. TREENODE *parent = (TREENODE*)malloc(sizeof(TREENODE));
  126. int ret =isfind(root,info);
  127. if(ret ==0)
  128. {
  129. printf("没有此人!\n");
  130. }
  131. else
  132. {
  133. find = mysearch(root,info);
  134. printf("确定要删除此学生:\n");
  135. printf("确定/取消?(Y/N):");
  136. char yn;
  137. setbuf(stdin,NULL);
  138. scanf("%c",&yn);
  139. if(yn == 'Y')
  140. {
  141. leaf = findleaf(root);
  142. parent = findparent(root,leaf);
  143. if(leaf == find)
  144. {
  145. free(find);
  146. if(parent->leftchild== leaf)
  147. {
  148. parent->leftchild = NULL;
  149. }
  150. else
  151. {
  152. parent->rightchild = NULL;
  153. }
  154. find = NULL;
  155. }
  156. else
  157. {
  158. strcpy(find->stuinfo->name,leaf->stuinfo->name);
  159. strcpy(find->stuinfo->studentID,leaf->stuinfo->studentID);
  160. strcpy(find->stuinfo->brithday,leaf->stuinfo->brithday);
  161. strcpy(find->stuinfo->tele,leaf->stuinfo->tele);
  162. free(leaf);
  163. if(parent->leftchild== leaf)
  164. {
  165. parent->leftchild =NULL;
  166. }
  167. else
  168. {
  169. parent->rightchild =NULL;
  170. }
  171. leaf = NULL;
  172. }
  173. }
  174. }
  175. }
  176. TREENODE * mycreatetree()
  177. {
  178. TREENODE *root = NULL;
  179. returnroot;
  180. }
  181. void myinsertnode(TREENODE*root,TreeData *data)
  182. {
  183. if(strcmp(root->stuinfo->studentID,data->studentID)< 0)
  184. {
  185. if(root->leftchild== NULL)
  186. {
  187. TREENODE *new = (TREENODE*)malloc(sizeof(TREENODE));
  188. new->stuinfo= data;
  189. new->leftchild= NULL;
  190. new->rightchild= NULL;
  191. root->leftchild = new;
  192. }
  193. else
  194. {
  195. myinsertnode(root->leftchild,data);
  196. }
  197. }
  198. else
  199. {
  200. if(root->rightchild== NULL)
  201. {
  202. TREENODE *new= (TREENODE *)malloc(sizeof(TREENODE));
  203. new->stuinfo= data;
  204. new->leftchild= NULL;
  205. new->rightchild= NULL;
  206. root->rightchild =new;
  207. }
  208. else
  209. {
  210. myinsertnode(root->rightchild,data);
  211. }
  212. }
  213. }
  214. void preorder(TREENODE*root)
  215. {
  216. if(root !=NULL)
  217. {
  218. printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
  219. preorder(root->leftchild);
  220. preorder(root->rightchild);
  221. }
  222. }
  223. int isfind(TREENODE*root,char*info)
  224. {
  225. return ((root != NULL)&&((strcmp(root->stuinfo->name,info) ==0) || (isfind(root->leftchild,info) ||isfind(root->rightchild,info))));
  226. }
  227. TREENODE * mysearch(TREENODE *root,char*info)
  228. {
  229. int ret =isfind(root,info);
  230. static intflt = 0;
  231. static TREENODE *find =NULL;
  232. if((ret == 0)&& (flt == 0))
  233. {
  234. printf("系统为找到此人!\n");
  235. return;
  236. }
  237. else
  238. {
  239. flt = 1;
  240. if(root !=NULL)
  241. {
  242. if(strcmp(root->stuinfo->name,info)== 0)
  243. {
  244. find = root;
  245. printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
  246. returnfind;
  247. }
  248. mysearch(root->leftchild,info);
  249. mysearch(root->rightchild,info);
  250. }
  251. }
  252. returnfind;
  253. }
  254. int mycorrect(TREENODE*root,char*info)
  255. {
  256. int ret =isfind(root,info);
  257. static intflt = 0;
  258. if((ret == 0)&& (flt == 0))
  259. {
  260. printf("系统为找到此人!\n");
  261. printf("请按Enter键继续...");
  262. getchar();
  263. getchar();
  264. return;
  265. }
  266. else
  267. {
  268. flt = 1;
  269. if(root !=NULL)
  270. {
  271. if(strcmp(root->stuinfo->name,info)== 0)
  272. {
  273. printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",root->stuinfo->name,root->stuinfo->studentID,root->stuinfo->brithday,root->stuinfo->tele);
  274. printf("\n");
  275. printf("\n");
  276. printf("\n");
  277. printf("\n");
  278. printf("\n");
  279. printf("\n");
  280. printf("\n");
  281. intchoose;
  282. char yn;
  283. char *buffer =(char*)malloc(sizeof(char));
  284. printf("请输入你想修改的内容或按5取消修改:");
  285. scanf("%d",&choose);
  286. while((choose> 5) || (choose <1))
  287. {
  288. printf("输入有误!\n");
  289. printf("请重新输入要修改的内容或按5取消:");
  290. scanf("%d",&choose);
  291. }
  292. switch(choose)
  293. {
  294. case 1:
  295. {
  296. printf("请输入要新的姓名:");
  297. scanf("%s",buffer);
  298. printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->name,buffer);
  299. setbuf(stdin,NULL);
  300. scanf("%c",&yn);
  301. if(yn == 'Y')
  302. {
  303. strcpy(root->stuinfo->name,buffer);
  304. printf("修改成功\n");
  305. printf("请按Enter键继续...");
  306. setbuf(stdin,NULL);
  307. getchar();
  308. getchar();
  309. }
  310. else
  311. {
  312. printf("数据没有修改!\n");
  313. printf("请按Enter键继续...");
  314. setbuf(stdin,NULL);
  315. getchar();
  316. getchar();
  317. }
  318. break;
  319. }
  320. case 2:
  321. {
  322. printf("请输入要新的学号:");
  323. scanf("%s",buffer);
  324. printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->studentID,buffer);
  325. setbuf(stdin,NULL);
  326. scanf("%c",&yn);
  327. if(yn == 'Y')
  328. {
  329. strcpy(root->stuinfo->studentID,buffer);
  330. printf("修改成功\n");
  331. printf("请按Enter键继续...");
  332. setbuf(stdin,NULL);
  333. getchar();
  334. getchar();
  335. }
  336. else
  337. {
  338. printf("数据没有修改!\n");
  339. printf("请按Enter键继续...");
  340. setbuf(stdin,NULL);
  341. getchar();
  342. getchar();
  343. }
  344. break;
  345. }
  346. case 3:
  347. {
  348. printf("请输入要新的生日:");
  349. scanf("%s",buffer);
  350. printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->brithday,buffer);
  351. setbuf(stdin,NULL);
  352. scanf("%c",&yn);
  353. if(yn == 'Y')
  354. {
  355. strcpy(root->stuinfo->brithday,buffer);
  356. printf("修改成功\n");
  357. printf("请按Enter键继续...");
  358. setbuf(stdin,NULL);
  359. getchar();
  360. getchar();
  361. }
  362. else
  363. {
  364. printf("数据没有修改!\n");
  365. printf("请按Enter键继续...");
  366. setbuf(stdin,NULL);
  367. getchar();
  368. getchar();
  369. }
  370. break;
  371. }
  372. case 4:
  373. {
  374. printf("请输入要新的电话:");
  375. scanf("%s",buffer);
  376. printf("您确定要将%s更改为%s吗?(Y/N):",root->stuinfo->tele,buffer);
  377. setbuf(stdin,NULL);
  378. scanf("%c",&yn);
  379. if(yn == 'Y')
  380. {
  381. strcpy(root->stuinfo->tele,buffer);
  382. printf("修改成功\n");
  383. printf("请按Enter键继续...");
  384. getchar();
  385. getchar();
  386. }
  387. else
  388. {
  389. printf("数据没有修改!\n");
  390. printf("请按Enter键继续...");
  391. setbuf(stdin,NULL);
  392. getchar();
  393. getchar();
  394. }
  395. break;
  396. }
  397. case 5:
  398. {
  399. break;
  400. }
  401. }//switend
  402. goto end;
  403. } //if==end
  404. mycorrect(root->leftchild,info);
  405. mycorrect(root->rightchild,info);
  406. } // if==NULLend
  407. } //elseend
  408. end:
  409. return;
  410. }
  411. void display(TREENODE*root)
  412. {
  413. intchoose;
  414. system("clear");
  415. printf("\n\n\n\n\n\n\n\n\n\n\n");
  416. printf("\t\t\t欢迎使用通讯录管理系统!\n");
  417. printf("\n\n\n\n\n\n\n\n\n\n\n");
  418. sleep(2);
  419. system("clear");
  420. while(1)
  421. {
  422. system("clear");
  423. printf("\n");
  424. printf("\n");
  425. printf("\n");
  426. printf("\n");
  427. printf("\n");
  428. printf("\n");
  429. printf("\n");
  430. printf("\n");
  431. printf("请输入您的选择(1-6):");
  432. scanf("%d",&choose);
  433. if((choose >6) || (choose < 1))
  434. {
  435. printf("输入有误,您的选择必须在1-5之间!\n");
  436. printf("请重新输入:");
  437. scanf("%d",&choose);
  438. }
  439. switch(choose)
  440. {
  441. case 1:
  442. {
  443. system("clear");
  444. preorder(root);
  445. printf("请按Enter键继续...");
  446. setbuf(stdin,NULL);
  447. getchar();
  448. break;
  449. }
  450. case 2:
  451. {
  452. system("clear");
  453. char *info =(char*)malloc(sizeof(char));
  454. printf("请输入要修改的人的姓名:");
  455. scanf("%s",info);
  456. mycorrect(root,info);
  457. break;
  458. }
  459. case 3:
  460. {
  461. system("clear");
  462. TreeData *info = (TreeData*)malloc(sizeof(TreeData));
  463. myeva(info);
  464. printf("您确定要添加学生:\n");
  465. printf("姓名:%s\t学号:%s\t生日:%s\t电话:%s\n",info->name,info->studentID,info->brithday,info->tele);
  466. printf("确定/取消(Y/N)?");
  467. char yn;
  468. setbuf(stdin,NULL);
  469. scanf("%c",&yn);
  470. if(yn == 'Y')
  471. {
  472. if(root ==NULL)
  473. {
  474. root = (TREENODE *)malloc(sizeof(TREENODE));
  475. root->stuinfo = info;
  476. root->leftchild =NULL;
  477. root->rightchild = NULL;
  478. }
  479. else
  480. {
  481. myinsertnode(root,info);
  482. }
  483. printf("添加成功!\n");
  484. }
  485. else
  486. {
  487. printf("取消添加!\n");
  488. }
  489. printf("请按Enter键继续...");
  490. getchar();
  491. getchar();
  492. break;
  493. }
  494. case 4:
  495. {
  496. system("clear");
  497. char *info =(char*)malloc(sizeof(char));
  498. printf("请输入要查找的人的姓名:");
  499. scanf("%s",info);
  500. mysearch(root,info);
  501. printf("请按Enter键继续...");
  502. setbuf(stdin,NULL);
  503. getchar();
  504. break;
  505. }
  506. case 5:
  507. {
  508. system("clear");
  509. char *info =(char*)malloc(sizeof(char));
  510. printf("请输入要删除的人的姓名:");
  511. scanf("%s",info);
  512. myremove(root,info);
  513. printf("请按Enter键继续...");
  514. getchar();
  515. getchar();
  516. break;
  517. }
  518. case 6:
  519. {
  520. system("clear");
  521. printf("\n\n\n\n\n\n\n\n\n\n\n");
  522. printf("\t\t\t\t欢迎再次使用!\n");
  523. printf("\n\n\n\n\n\n\n\n\n\n\n");
  524. sleep(2);
  525. system("clear");
  526. exit(0);
  527. }
  528. default:
  529. {
  530. ;
  531. }
  532. }
  533. }
  534. }
  535. TREENODE * treeinit(TREENODE *root)
  536. {
  537. TreeData * info[4];
  538. int i;
  539. for(i = 0; i< 4; i++)
  540. {
  541. if((info[i] = (TreeData*)malloc(sizeof(TreeData))) ==NULL)
  542. {
  543. printf("分配空间失败!\n");
  544. }
  545. }
  546. mycopy(info[0],"张一","202090116","1990-10-11","15298377000");
  547. mycopy(info[1],"张二","202090114","1990-10-12","15298377111");
  548. mycopy(info[2],"张三","202090115","1990-10-13","15298377222");
  549. mycopy(info[3],"张四","202090118","1990-10-14","15298377333");
  550. root = (TREENODE *)malloc(sizeof(TREENODE));
  551. root->stuinfo =info[0];
  552. root->leftchild = NULL;
  553. root->rightchild =NULL;
  554. myinsertnode(root,info[1]);
  555. myinsertnode(root,info[2]);
  556. myinsertnode(root,info[3]);
  557. returnroot;
  558. }
  559. intmain()
  560. {
  561. TREENODE * root = NULL;
  562. root = mycreatetree();
  563. root = treeinit(root);
  564. display(root);
  565. return 0;
  566. }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1046975次
    • 积分:18282
    • 等级:
    • 排名:第521名
    • 原创:741篇
    • 转载:523篇
    • 译文:0篇
    • 评论:105条
    最新评论