Void-voiD

Untitled

Dec 29th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Elem {
  6. int v;
  7. char *k;
  8. struct Elem *parent, *left, *right;
  9. };
  10.  
  11. struct Elem *InitBinarySearchTree()
  12. {
  13. struct Elem *t = NULL;
  14. return t;
  15. }
  16.  
  17. struct Elem *Descend(struct Elem *t, char *k)
  18. {
  19. struct Elem *x = t;
  20. while (x != NULL && 0 != strcmp(k, x->k)) {
  21. if ((int)strlen(k) < (int)strlen(x->k)) x = x->left;
  22. else x = x->right;
  23. }
  24. return x;
  25. }
  26.  
  27. struct Elem *Insert(struct Elem *t, char *k, int v) {
  28. struct Elem *y = (struct Elem *)malloc(sizeof(struct Elem));
  29. y->parent = NULL;
  30. y->left = NULL;
  31. y->right = NULL;
  32. y->k = k;
  33. y->v = v;
  34. if (t == NULL) {
  35. t = y;
  36. return t;
  37. }
  38. else {
  39. struct Elem *x = t;
  40. for (;;) {
  41. if ((int)strlen(k) < (int)strlen(x->k)) {
  42. if (x->left == NULL) {
  43. x->left = y;
  44. x->parent = x;
  45. break;
  46. }
  47. x = x->left;
  48. }
  49. else {
  50. if (x->right == NULL) {
  51. x->right = y;
  52. x->parent = x;
  53. break;
  54. }
  55. x = x->right;
  56. }
  57. }
  58. return t;
  59. }
  60. }
  61.  
  62. void SuperFree(struct Elem *t)
  63. {
  64. if (t != NULL) {
  65. if (t->left != NULL) SuperFree(t->left);
  66. if (t->right != NULL) SuperFree(t->right);
  67. free(t->k);
  68. free(t);
  69. }
  70. }
  71.  
  72. int main()
  73. {
  74. char a[8];
  75. char cur[100];
  76. cur[0] = 0;
  77. scanf("%s\n", a);
  78. int pos = 0, num = -777, i, id = 0, n = atoi(a), len = 0, j;
  79. char *expr = (char*)malloc((n + 1) * sizeof(char));
  80. gets(expr);
  81. n = (int)strlen(expr);
  82. struct Elem *t = InitBinarySearchTree(), *now = NULL;
  83. for (i = 0; i < n; i++) {
  84. if (len == 0 && (expr[i] == '0' || expr[i] == '1' || expr[i] == '2' || expr[i] == '3' || expr[i] == '4' || expr[i] == '5' || expr[i] == '6' || expr[i] == '7' || expr[i] == '8' || expr[i] == '9')) {
  85. if (num == -777) {
  86. num = (int)expr[i] - 48;
  87. }
  88. else num = num * 10 + (int)expr[i] - 48;
  89. }
  90. else {
  91. if (len == 0 && num != -777) {
  92. printf("CONST %d\n", num);
  93. num = -777;
  94. }
  95. if (len != 0 && (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/' || expr[i] == '(' || expr[i] == ')' || expr[i] == ' ')) {
  96. pos = 0;
  97. now = NULL;
  98. if (t != NULL) now = Descend(t, cur);
  99. if (now == NULL) {
  100. char *curr = (char*)malloc((len + 1) * sizeof(char));
  101. for (j = 0; j < len; j++) curr[j] = cur[j];
  102. curr[j] = 0;
  103. t = Insert(t, curr, id);
  104. printf("IDENT %d\n", id);
  105. id++;
  106. } else printf("IDENT %d\n", now->v);
  107. len = 0;
  108. }
  109. if (expr[i] == '+') {
  110. printf("SPEC 0\n");
  111. continue;
  112. }
  113. if (expr[i] == '-') {
  114. printf("SPEC 1\n");
  115. continue;
  116. }
  117. if (expr[i] == '*') {
  118. printf("SPEC 2\n");
  119. continue;
  120. }
  121. if (expr[i] == '/') {
  122. printf("SPEC 3\n");
  123. continue;
  124. }
  125. if (expr[i] == '(') {
  126. printf("SPEC 4\n");
  127. continue;
  128. }
  129. if (expr[i] == ')') {
  130. printf("SPEC 5\n");
  131. continue;
  132. }
  133. if (expr[i] != ' ') {
  134. cur[pos] = expr[i];
  135. pos++;
  136. cur[pos] = 0;
  137. len++;
  138. }
  139. }
  140. }
  141. if (len != 0) {
  142. pos = 0;
  143. len = 0;
  144. if (t != NULL) now = Descend(t, cur);
  145. else now = NULL;
  146. if (now == NULL) {
  147. char *curr = (char*)malloc((len + 1) * sizeof(char));
  148. for (j = 0; j < len; j++) curr[j] = cur[j];
  149. curr[j] = 0;
  150. Insert(t, curr, id);
  151. printf("IDENT %d\n", id);
  152. id++;
  153. } else printf("IDENT %d\n", now->v);
  154. }
  155. free(expr);
  156. SuperFree(t);
  157. return 0;
  158. }
Add Comment
Please, Sign In to add comment