Advertisement
ahmad_zizo

MENU2

May 31st, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. int main()
  2. {
  3. node_id* root_id;
  4. node_id* root_name;
  5.  
  6.  
  7. root_name = NULL;
  8. root_id = NULL;
  9. printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
  10. printf("choose:\n");
  11. printf("1- insert data\n");
  12. printf("2- write file to tree\n3- print\n");
  13. printf("4- delete\n");
  14. printf("5- search\n");
  15. printf("6- read file to tree\n7- quit\n\n");
  16.  
  17.  
  18. int command;
  19. char co[20];
  20.  
  21. char name[40];
  22. char id_2[10];
  23. int id;
  24. char c;
  25. printf("Enter command: ");
  26. scanf(" %s", co);
  27. command = atoi(co);
  28.  
  29. do
  30. {
  31. if(command<1 || command>7)
  32. {
  33. system("cls");
  34. printf("enter valid number\n\n");
  35. }
  36. else
  37. switch (command)
  38. {
  39. case 7:
  40. {
  41. system("cls");
  42. printf("THANK YOU!\n\n");
  43. printf("Ahmad Omar 2715 & Ahmad Abdulaziz 2210\n\n");
  44. exit(0);
  45. break;
  46. }
  47. case 1:
  48. {
  49. system("cls");
  50. printf("Enter name: ");
  51. while((c= getchar()) != '\n' && c != EOF);
  52. //gets(name);
  53. int j,f;
  54.  
  55. scanf(" %[^\n]",name);
  56. printf("Enter id: ");
  57. scanf(" %[^\n]",id_2);
  58. int l=strlen(name);
  59. for(j=0; j<l; j++)
  60. if ( !(name[j]=='\n' || name[j] == 32 || (name[j] >= 65 && name[j] <= 90) || (name[j] >= 97 && name[j] <= 122) ))
  61. {
  62. printf("ERROR in name\n");
  63. f=1;
  64. break;
  65. }
  66. if(f==1)
  67. break;
  68. node_id *test = search_id(&root_id,atoi(id_2));
  69. if(test != NULL && atoi(id_2) == test->id)
  70. printf("ERROR, id already exist!\n");
  71. else if(atoi(name)==0 && atoi(id_2)> 0 && atoi(id_2) == atof(id_2))
  72. {
  73. id= atoi(id_2);
  74. insert(&root_id, &root_name, id, name);
  75. system("cls");
  76. printf("%d %s inserted succesfuly\n\n",id,name);
  77. }
  78. else
  79. {
  80. printf("\nERROR in name or id!\n");
  81. }
  82. break;
  83. }
  84.  
  85.  
  86. case 2:
  87. {
  88. system("cls");
  89. char file_name[50];
  90. printf("Enter file name: ");
  91. scanf("%s", file_name);
  92. FILE* file = fopen(file_name, "w");
  93. write_tree_to_file(root_id, file);
  94. fclose(file);
  95. break;
  96. }
  97.  
  98. case 3:
  99. {
  100. char z[15];
  101. system("cls");
  102. printf("choose type of print:\n");
  103. printf("1- print preorder by id\n");
  104. printf("2- print preorder by name\n3- print post order by id\n");
  105. printf("4- print post order by name\n5- inorder by id\n");
  106. printf("6- print inorder by name\n");
  107. printf("your choice: ");
  108. scanf(" %[^\n]s",z);
  109. int x=atoi(z);
  110. if(x==1)
  111. print_preorder(root_id,0);
  112. else if(x==2)
  113. print_preorder(root_name,0);
  114. else if(x==3)
  115. print_postorder(root_id,0);
  116. else if(x==4)
  117. print_postorder(root_name,0);
  118. else if(x==5)
  119. print_inorder(root_id,0);
  120. else if(x==6)
  121. print_inorder(root_name,0);
  122. else
  123. {
  124. system("cls");
  125. printf("enter a valid choice!\n");
  126. break;
  127. }
  128. break;
  129. }
  130. case 4:
  131. {
  132. char z2[15];
  133. system("cls");
  134. printf("1- by name\n2- by id\n");
  135. printf("your choice: ");
  136. scanf(" %[^\n]s",z2);
  137. int x2=atoi(z2);
  138. if(x2==1)
  139. {
  140. system("cls");
  141. printf("Enter name: ");
  142. while((c= getchar()) != '\n' && c != EOF);
  143. gets(name);
  144. node_id* temp = search_name(&root_name, name);
  145. if (temp != NULL)
  146. {
  147. id = temp->id;
  148. }
  149. if(temp == NULL)
  150. printf("NOT FOUND.\n\n");
  151. else
  152. {
  153. root_id = delete_id(root_id, id);
  154. root_name = delete_name(root_name, name);
  155. printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
  156. }
  157. }
  158.  
  159. else if(x2==2)
  160. {
  161. system("cls");
  162. printf("Enter id: ");
  163. scanf("%d", &id);
  164. node_id* temp = search_id(&root_id, id);
  165. if (temp != NULL)
  166. {
  167. strcpy(name, temp->name);
  168. }
  169. if(temp==NULL)
  170. printf("NOT FOUND.\n\n");
  171. else
  172. {
  173. root_id = delete_id(root_id, id);
  174. root_name = delete_name(root_name,name);
  175. printf("\n%d %s has been deleted succesfuly.\n\n",id,name);
  176. }
  177. }
  178. else
  179. {
  180. system("cls");
  181. printf("enter a valid choice.\n");
  182. break;
  183. }
  184. break;
  185. }
  186. case 5:
  187. {
  188. system("cls");
  189. char z3[15];
  190. printf("1- by id\n2- by name\n");
  191. printf("your choice: ");
  192. scanf("% [^\n]s",z3);
  193. int x3=atoi(z3);
  194. if(x3==2)
  195. {
  196. system("cls");
  197. printf("Enter name: ");
  198. while((c= getchar()) != '\n' && c != EOF);
  199. gets(name);
  200. search_name_2(&root_name, name);
  201. }
  202. if(x3==1)
  203. {
  204. system("cls");
  205. printf("Enter id: ");
  206. scanf("%d", &id);
  207. search_id_2(&root_id, id);
  208. }
  209. else
  210. {
  211. system("cls");
  212. printf("enter a valid choice!\n");
  213. break;
  214. }
  215. break;
  216. }
  217. case 6:
  218. {
  219. system("cls");
  220. char file_name[50];
  221. printf("Enter file name: ");
  222. scanf("%s", file_name);
  223. reed_file_to_tree(&root_id, &root_name, file_name);
  224.  
  225. break;
  226. }
  227. }
  228. printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
  229. printf("choose:\n");
  230. printf("1- insert data\n");
  231. printf("2- write file to tree\n3- print\n");
  232. printf("4- delete\n");
  233. printf("5- search\n");
  234. printf("6- read file to tree\n7- quit\n\n");
  235. printf("Enter command: ");
  236. scanf(" %s", co);
  237. command = atoi(co);
  238. }
  239. while (command != ~(1<<31));
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement