Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5.  
  6. typedef struct node {
  7. char value;
  8.  
  9. struct node *left_c;
  10. struct node *righ_c;
  11. } nodes;
  12.  
  13. nodes *init() {
  14. nodes *nouv = malloc(sizeof(nodes));
  15. nouv->value = 'E';
  16. nouv->left_c = NULL;
  17. nouv->righ_c = NULL;
  18. return nouv;
  19. }
  20.  
  21. void addchild(nodes *racine, int x, char value) {
  22. nodes *nouv = malloc(sizeof(nodes));
  23. nouv->value = value;
  24. nouv->left_c = NULL;
  25. nouv->righ_c =NULL;
  26. if (x==0) {
  27. racine->left_c=nouv;
  28. }
  29. else {
  30. racine->righ_c=nouv;
  31. }
  32. }
  33.  
  34. int calcno(nodes *racine, nodes *curr, int no) {
  35. int tmp;
  36.  
  37. if (racine == curr)
  38. return no;
  39. else {
  40. if (racine->left_c!=NULL) {
  41. if ((tmp = calcno(racine->left_c, curr, (no*2))) != -1) {
  42. return tmp;
  43. }
  44. }
  45. if (racine->righ_c!=NULL) {
  46. if ((tmp = calcno(racine->righ_c, curr, (no*2)+1)) != -1) {
  47. return tmp; }
  48. }
  49. return (-1);
  50. }
  51. }
  52.  
  53. nodes *findfromno(nodes *racine, nodes *curr, int no) {
  54. nodes *tmp;
  55.  
  56. printf("racine recue = %p\tno recu = %d\n", racine, no);
  57.  
  58. if (calcno(curr, racine, 1) == no)
  59. return racine;
  60. else {
  61. if (racine->left_c!=NULL) {
  62. if ((tmp = findfromno(racine->left_c, curr, no)) != NULL) {
  63. return tmp;
  64. }
  65. }
  66. if (racine->righ_c!=NULL) {
  67. if ((tmp = findfromno(racine->righ_c, curr, no)) != NULL) {
  68. return tmp;
  69. }
  70. }
  71. return (NULL);
  72. }
  73. }
  74.  
  75. void addnoeud(nodes *racine, int no) {
  76. int pere;
  77. nodes *tmp;
  78.  
  79. if (no%2==0)
  80. pere = no/2;
  81. else
  82. pere = (no-1)/2;
  83.  
  84.  
  85.  
  86. tmp = findfromno(racine, racine, pere);
  87.  
  88. // printf("pere calculé : %d\nnoeud trouvé : %p\n", pere, tmp);
  89.  
  90. if (no%2==0)
  91. addchild(tmp, 0, 'A');
  92. else
  93. addchild(tmp, 1, 'B');
  94. }
  95.  
  96. int mystrlen(char *str) {
  97. int k =0;
  98. while (str[k]!='\0')
  99. k++;
  100.  
  101. return k;
  102. }
  103.  
  104. char *suppr(char *str){
  105. char *s = malloc(sizeof(char) *(mystrlen(str)));
  106. s = strncpy(s, str, mystrlen(str));
  107. s[(mystrlen(str) -1)] = '\0';
  108. // printf("%s -> %s\n", str, s);
  109. return s;
  110. }
  111.  
  112. void printnode(nodes *racine, char *chemin, int nb, nodes *curr) {
  113. int hauteur= strlen(chemin) - 1;
  114. // printf("Chemin: %s\tHauteur: %d\n", chemin, hauteur);
  115. if (nb == 0) {
  116. printf("\t %c", racine->value);
  117. return;
  118. }
  119. char *nchemin = malloc(sizeof(char) *(strlen(chemin)+1));
  120. nchemin = strcpy(nchemin, chemin);
  121. if (hauteur == nb) {
  122. printf("\t[%c] %d %p", racine->value, calcno(curr, racine, 1), racine);
  123. }
  124. else {
  125. if (racine->left_c != NULL){
  126. nchemin = strcat(nchemin, "0");
  127. printnode(racine->left_c, nchemin, nb, curr);
  128. nchemin = suppr(nchemin);
  129. }
  130. if (racine->righ_c != NULL) {
  131. nchemin = strcat(nchemin, "1");
  132. printnode(racine->righ_c, nchemin, nb, curr);
  133. nchemin = suppr(nchemin);
  134. }
  135. }
  136. }
  137.  
  138. char last(char *str) {
  139. for (int k=0; k<strlen(str); k++);
  140. return str[k];
  141. }
  142.  
  143. int prio(char c1, char c2) {
  144. char *l ="*/+-"
  145. int k = 0;
  146. int k2 = 0;
  147. while (k<strlen(l) && l[k]!=c1)
  148. k++;
  149. while (k2<strlen(l) && l[k2]!=c2)
  150. k2++;
  151.  
  152. return k2-k;
  153.  
  154. }
  155.  
  156. char *pol(char *str) {
  157. int i=0;
  158. int k=0;
  159.  
  160.  
  161.  
  162. char *pol="";
  163. char *pile="";
  164. while (str[i]!='\0') {
  165.  
  166. if (str[i]>='0' && str[i]<='9')
  167. pol[k++]=str[i];
  168.  
  169. if (str[i]=='(')
  170. pile = strcat(pile, '(');
  171.  
  172. if (str[i]=='*' || str[i]=='/' || str[i]=='+' || str[i]=='-') {
  173. if (strcmp(pile, "")==0 || last(pile)=='(' || prio(str[i], last(pile))>0)
  174. pile = strcat(pile, str[i]);
  175.  
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182. i++;
  183. }
  184. return pol;
  185. }
  186.  
  187. int main()
  188. {
  189. nodes *racine;
  190. racine = init();
  191.  
  192. char *op = "(2*(5+3))"
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement