Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "string.h"
  4. #include<conio.h>
  5. #define N 100
  6.  
  7. typedef struct Pile
  8. {
  9. int value;
  10. struct Pile *prcd;
  11. } Pile;
  12.  
  13. typedef struct
  14. {
  15. char name[100], traduction[100];
  16. int left;
  17. int right;
  18. } tree;
  19. //algo pile
  20. int pushPile(Pile ** p, int valeur)
  21. {
  22. Pile * nv_elem = NULL;
  23. nv_elem =(Pile * ) malloc(sizeof(Pile));
  24. if(nv_elem != NULL)
  25. {
  26. nv_elem->value = valeur;
  27. nv_elem->prcd = *p;
  28. *p = nv_elem;
  29. return 1;
  30. }
  31.  
  32. return -1;
  33.  
  34. }
  35.  
  36. int * popPile(Pile **p)
  37. {
  38. int value;
  39. if((isEmpty_pile(*p) == 0))
  40. {
  41. if(p != NULL )
  42. {
  43. Pile * v = (*p)->prcd;
  44. value = (*p)->value;
  45. free(*p);
  46. *p = NULL;
  47. *p = v;
  48.  
  49. }
  50. }
  51.  
  52. return value;
  53. }
  54.  
  55.  
  56. Pile first(Pile * p)
  57. {
  58. Pile *t = NULL;
  59. t = p;
  60. while(t->prcd != NULL)
  61. {
  62. t = t->prcd;
  63. }
  64. return *t;
  65. }
  66. int isEmpty_pile(Pile *p)
  67. {
  68. return (p == NULL) ? 1 : 0;
  69. }
  70. int insertTree(tree * array,int * p, char * values, char * traduction)
  71. {
  72. tree t;
  73. int i = 0;
  74. values = strlwr(values);
  75.  
  76. if(*p <= N)
  77. {
  78. if(searchTree(array,values,0,*p) == -1)
  79. {
  80. strcpy(t.name,values);
  81. strcpy(t.traduction,traduction);
  82. t.left = -1;
  83. t.right = -1;
  84. array[*p] = t;
  85. if(*p > 0)
  86. {
  87. while(array[i].left != *p && array[i].right != *p)
  88. {
  89. if(array[*p].name[0] >= array[i].name[0])
  90. {
  91. if(array[i].right == -1)
  92. {
  93. array[i].right = *p;
  94. }
  95. else
  96. {
  97. i = array[i].right;
  98. }
  99.  
  100. }
  101. else
  102. {
  103.  
  104. if(array[i].left != -1)
  105. {
  106. i = array[i].left;
  107. }
  108. else
  109. {
  110.  
  111. array[i].left = *p;
  112. }
  113. }
  114. }
  115. }
  116.  
  117. (*p)++;
  118. return 0;
  119. }
  120. else
  121. {
  122.  
  123. return -2;
  124. }
  125.  
  126. }
  127. else
  128. return -1;
  129.  
  130. }
  131.  
  132. int searchTree(tree * array, char * value, int i, int position )
  133. {
  134. if(position > 0)
  135. {
  136. if(strcmp(value,array[i].name) == 0)
  137. {
  138. return i;
  139. }
  140. else
  141. {
  142. i = (strcmp(value,array[i].name) > 0 ? array[i].right : array[i].left);
  143. if(i == -1)
  144. return -1;
  145. else
  146. searchTree(array,value,i,position);
  147. }
  148. }
  149. else
  150. {
  151. return -1;
  152. }
  153.  
  154. }
  155.  
  156. void displayTree(tree * array, int i )
  157. {
  158. if(i != -1)
  159. {
  160. displayTree(array,array[i].left);
  161. printf("%s\n",array[i].name);
  162. displayTree(array,array[i].right);
  163. }
  164. }
  165.  
  166. /*void displayTreeIterator(tree * array )
  167. {
  168. Pile * p = NULL;
  169. tree * t , * current = NULL;
  170. current = array;
  171.  
  172. while(isEmpty_pile(p) == 0 || current != NULL )
  173. {
  174.  
  175. if(current != NULL)
  176. {
  177. pushPile(&p,current);
  178.  
  179. if(current->left == -1)
  180. current = NULL;
  181. else
  182. current = (array + current->left);
  183.  
  184. }else
  185. {
  186.  
  187. t = popPile(&p);
  188.  
  189. printf("%s",t->name);
  190.  
  191. if(t->right == -1)
  192. current = NULL;
  193. else
  194. current = (array + t->right);
  195.  
  196. }
  197. }
  198. }*/
  199. void displayTreeIteratorWithoutPointer(tree * array )
  200. {
  201. Pile * p = NULL;
  202. int t, current;
  203. current = 0;
  204.  
  205. while(isEmpty_pile(p) == 0 || (current != -1) )
  206. {
  207.  
  208. if(current != -1)
  209. {
  210. pushPile(&p,current);
  211.  
  212. if(current == -1)
  213. current = -1;
  214. else
  215. current = array[current].left;
  216.  
  217. }
  218. else
  219. {
  220.  
  221. t = popPile(&p);
  222. printf("Mot : %s \n",array[t].name);
  223. printf("Traduction : %s \n",array[t].traduction);
  224. printf("-------------------------\n");
  225.  
  226. if(t == -1)
  227. current = -1;
  228. else
  229. {
  230. current = array[t].right;
  231. }
  232.  
  233.  
  234. }
  235. }
  236. }
  237.  
  238. int deleteTree(tree * array, int *p, int position)
  239. {
  240. int i = 0,r ;
  241. tree newArray[100];
  242. r = 0;
  243. for(i = 0; i<*p; i++)
  244. {
  245. if(i != position)
  246. insertTree(newArray,&r,array[i].name,array[i].traduction);
  247. }
  248. for(i = 0; i<r; i++)
  249. {
  250. strcpy(array[i].name,newArray[i].name);
  251. strcpy(array[i].traduction,newArray[i].traduction);
  252. array[i].right = newArray[i].right;
  253. array[i].left = newArray[i].left;
  254. }
  255. (*p)--;
  256. }
  257. void ajouter(tree * menu, int * position)
  258. {
  259. char mot[100],traduction[100];
  260. clrscr();
  261. printf("Entrer le mot : \n");
  262. scanf("%s",&mot);
  263. printf("Entrer le traduction : \n");
  264. scanf("%s",&traduction);
  265. if(insertTree(menu,position,mot,traduction) != 0)
  266. {
  267. printf("Le mot existe deja .");
  268. getch();
  269. }
  270.  
  271. clrscr();
  272. }
  273. void rechercher(tree * menu, int position)
  274. {
  275. char mot[100];
  276. int rech;
  277. clrscr();
  278. printf("Veuillez entrez le mot : \n");
  279. scanf("%s",&mot);
  280. rech = searchTree(menu,&mot,0,position);
  281. if( rech == -1)
  282. {
  283. printf("Le mot n'existe pas .");
  284. getch();
  285.  
  286. }
  287. else
  288. {
  289. clrscr();
  290. printf("Mot : %s\n",menu[rech].name);
  291. printf("Traduction : %s\n",menu[rech].traduction);
  292. getch();
  293. }
  294.  
  295. clrscr();
  296. }
  297.  
  298. void modifier(tree * menu, int position)
  299. {
  300. char mot[100], traduction[100];
  301. int rech;
  302. clrscr();
  303. printf("Veuillez entrez le mot : \n");
  304. scanf("%s",&mot);
  305. rech = searchTree(menu,&mot,0,position);
  306. if( rech == -1)
  307. {
  308. printf("Le mot n'existe pas .");
  309. getch();
  310.  
  311. }
  312. else
  313. {
  314. clrscr();
  315. printf("Nouvelle traduction : \n");
  316. scanf("%s",&traduction);
  317. strcpy(menu[rech].traduction,traduction);
  318. printf("Modification effectuée ! ");
  319. getch();
  320. }
  321.  
  322. clrscr();
  323.  
  324. }
  325. void supprimer(tree * menu, int * position)
  326. {
  327.  
  328. char mot[100] ;
  329. int rech;
  330. clrscr();
  331. printf("Veuillez entrez le mot : \n");
  332. scanf("%s",&mot);
  333. rech = searchTree(menu,&mot,0,*position);
  334. if( rech == -1)
  335. {
  336. printf("Le mot n'existe pas .");
  337. getch();
  338.  
  339. }
  340. else
  341. {
  342. clrscr();
  343. deleteTree(menu,position,rech);
  344. printf("Suppression effectuee ! ");
  345. getch();
  346. }
  347.  
  348. clrscr();
  349. }
  350.  
  351. void save(tree * tab, int position)
  352. {
  353.  
  354. char nom[100];
  355. int i =0;
  356. int rech;
  357. FILE *file;
  358. clrscr();
  359. printf("Veuillez entrez le nom du fichier : \n");
  360. scanf("%s",&nom);
  361. file = fopen(strcat(nom,".txt"), "wt");
  362. if(file)
  363. remove(strcat(nom,".txt"));
  364.  
  365. for( i; i<position; i++)
  366. {
  367. fprintf(file,"%s %s %d %d\n",tab[i].name,tab[i].traduction,tab[i].left,tab[i].right);
  368. }
  369. fclose(file);
  370. printf("Fichier sauvgarder !");
  371. getch();
  372. clrscr();
  373. }
  374.  
  375. void charger(tree * tab, int *position )
  376. {
  377. int i =0,left,right;
  378. char nom[100],traduction[100];
  379. FILE * file;
  380. clrscr();
  381. printf("Veuillez entrez le nom du fichier : \n");
  382. scanf("%s",&nom);
  383.  
  384.  
  385. file = fopen(strcat(nom,".txt"),"rt");
  386.  
  387. if(file == NULL)
  388. {
  389. printf("Le fichier n'existe pas !");
  390. getch();
  391. }
  392. else
  393. {
  394. while(fscanf(file,"%s %s %d %d",&nom,&traduction,&left,&right)!= EOF)
  395. {
  396. tab[i].left = left;
  397. tab[i].right = right;
  398. strcpy(tab[i].name,nom);
  399. strcpy(tab[i].traduction,traduction);
  400. i++;
  401. }
  402. *position = i;
  403. printf("Chargement effectuee!");
  404. getch();
  405.  
  406. }
  407.  
  408. fclose(file);
  409. clrscr();
  410. }
  411.  
  412. int main()
  413. {
  414. int menu = 0, position = 0,i;
  415. tree tab[100];
  416.  
  417.  
  418. do
  419. {
  420. printf("1 - Ajouter un mot\n");
  421. printf("2 - Rechercher un mot\n");
  422. printf("3 - Modifier un mot\n");
  423. printf("4 - Supprimer un mot\n");
  424. printf("5 - Afficher par ordre alphabetique\n");
  425. printf("6 - Afficher la structure interne\n");
  426. printf("7 - Sauvgarder un fichier\n");
  427. printf("8 - Charger un fichier\n");
  428. printf("9 - Sortir \n");
  429. printf("Choix : ");
  430. scanf("%d",&menu);
  431. if(menu == 1)
  432. {
  433. ajouter(tab,&position);
  434. }
  435. else if(menu == 2)
  436. {
  437. rechercher(tab,position);
  438. }
  439. else if(menu == 3)
  440. {
  441. modifier(tab,position);
  442. }
  443. else if(menu == 4)
  444. {
  445. supprimer(tab,&position);
  446. }
  447. else if(menu == 5)
  448. {
  449. clrscr();
  450. if(position > 0)
  451. displayTreeIteratorWithoutPointer(tab);
  452. else
  453. printf("Arbre vide !");
  454. getch();
  455. clrscr();
  456.  
  457. }
  458. else if(menu == 6)
  459. {
  460. clrscr();
  461. if(position > 0)
  462. {
  463.  
  464. for(i = 0; i<position; i++)
  465. {
  466. printf("Mot : %s\n",tab[i].name);
  467. printf("Traduction : %s\n",tab[i].traduction);
  468. printf("Droit : %d\n",tab[i].right);
  469. printf("Gauche : %d\n",tab[i].left);
  470. printf("--------------------------\n");
  471.  
  472. }
  473. }
  474. else
  475. printf("Arbre vide !");
  476. getch();
  477. clrscr();
  478.  
  479. }
  480. else if(menu == 7)
  481. {
  482. save(tab,position);
  483. }
  484. else if(menu == 8)
  485. {
  486. charger(tab,&position);
  487. }
  488.  
  489. }
  490. while( menu == 1 || menu == 2 || menu == 3 || menu == 4 || menu == 5 || menu == 6 || menu == 7 || menu == 8);
  491. return 0;
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement