Advertisement
Void-voiD

Untitled

Dec 26th, 2018
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 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 *expr = (char*)malloc(100000 * sizeof(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. gets(expr);
  80. struct Elem *t = InitBinarySearchTree(), *now = NULL;
  81. for (i = 0; i < n; i++) {
  82. 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')) {
  83. if (num == -777) {
  84. num = (int)expr[i] - 48;
  85. }
  86. else num = num * 10 + (int)expr[i] - 48;
  87. }
  88. else {
  89. if (len == 0 && num != -777) {
  90. printf("CONST %d\n", num);
  91. num = -777;
  92. }
  93. if (len != 0 && (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/' || expr[i] == '(' || expr[i] == ')' || expr[i] == ' ')) {
  94. pos = 0;
  95. now = NULL;
  96. if (t != NULL) now = Descend(t, cur);
  97. if (now == NULL) {
  98. char *curr = (char*)malloc((len + 1) * sizeof(char));
  99. for (j = 0; j < len; j++) curr[j] = cur[j];
  100. curr[j] = 0;
  101. t = Insert(t, curr, id);
  102. printf("IDENT %d\n", id);
  103. id++;
  104. } else printf("IDENT %d\n", now->v);
  105. len = 0;
  106. }
  107. if (expr[i] == '+') {
  108. printf("SPEC 0\n");
  109. continue;
  110. }
  111. if (expr[i] == '-') {
  112. printf("SPEC 1\n");
  113. continue;
  114. }
  115. if (expr[i] == '*') {
  116. printf("SPEC 2\n");
  117. continue;
  118. }
  119. if (expr[i] == '/') {
  120. printf("SPEC 3\n");
  121. continue;
  122. }
  123. if (expr[i] == '(') {
  124. printf("SPEC 4\n");
  125. continue;
  126. }
  127. if (expr[i] == ')') {
  128. printf("SPEC 5\n");
  129. continue;
  130. }
  131. if (expr[i] != ' ') {
  132. cur[pos] = expr[i];
  133. pos++;
  134. cur[pos] = 0;
  135. len++;
  136. }
  137. }
  138. }
  139. if (len != 0) {
  140. pos = 0;
  141. len = 0;
  142. if (t != NULL) now = Descend(t, cur);
  143. else now = NULL;
  144. if (now == NULL) {
  145. Insert(t, cur, id);
  146. printf("IDENT %d\n", id);
  147. id++;
  148. } else printf("IDENT %d\n", now->v);
  149. }
  150. free(expr);
  151. SuperFree(t);
  152. return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement