Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Elem {
- int v;
- char *k;
- struct Elem *parent, *left, *right;
- };
- struct Elem *InitBinarySearchTree()
- {
- struct Elem *t = NULL;
- return t;
- }
- struct Elem *Descend(struct Elem *t, char *k)
- {
- struct Elem *x = t;
- while (x != NULL && 0 != strcmp(k, x->k)) {
- if ((int)strlen(k) < (int)strlen(x->k)) x = x->left;
- else x = x->right;
- }
- return x;
- }
- struct Elem *Insert(struct Elem *t, char *k, int v) {
- struct Elem *y = (struct Elem *)malloc(sizeof(struct Elem));
- y->parent = NULL;
- y->left = NULL;
- y->right = NULL;
- y->k = k;
- y->v = v;
- if (t == NULL) {
- t = y;
- return t;
- }
- else {
- struct Elem *x = t;
- for (;;) {
- if ((int)strlen(k) < (int)strlen(x->k)) {
- if (x->left == NULL) {
- x->left = y;
- x->parent = x;
- break;
- }
- x = x->left;
- }
- else {
- if (x->right == NULL) {
- x->right = y;
- x->parent = x;
- break;
- }
- x = x->right;
- }
- }
- return t;
- }
- }
- void SuperFree(struct Elem *t)
- {
- if (t != NULL) {
- if (t->left != NULL) SuperFree(t->left);
- if (t->right != NULL) SuperFree(t->right);
- free(t->k);
- free(t);
- }
- }
- int main()
- {
- char a[8];
- char cur[100];
- cur[0] = 0;
- scanf("%s\n", a);
- int pos = 0, num = -777, i, id = 0, n = atoi(a), len = 0, j;
- char *expr = (char*)malloc((n + 1) * sizeof(char));
- gets(expr);
- n = (int)strlen(expr);
- struct Elem *t = InitBinarySearchTree(), *now = NULL;
- for (i = 0; i < n; i++) {
- 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')) {
- if (num == -777) {
- num = (int)expr[i] - 48;
- }
- else num = num * 10 + (int)expr[i] - 48;
- }
- else {
- if (len == 0 && num != -777) {
- printf("CONST %d\n", num);
- num = -777;
- }
- if (len != 0 && (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/' || expr[i] == '(' || expr[i] == ')' || expr[i] == ' ')) {
- pos = 0;
- now = NULL;
- if (t != NULL) now = Descend(t, cur);
- if (now == NULL) {
- char *curr = (char*)malloc((len + 1) * sizeof(char));
- for (j = 0; j < len; j++) curr[j] = cur[j];
- curr[j] = 0;
- t = Insert(t, curr, id);
- printf("IDENT %d\n", id);
- id++;
- } else printf("IDENT %d\n", now->v);
- len = 0;
- }
- if (expr[i] == '+') {
- printf("SPEC 0\n");
- continue;
- }
- if (expr[i] == '-') {
- printf("SPEC 1\n");
- continue;
- }
- if (expr[i] == '*') {
- printf("SPEC 2\n");
- continue;
- }
- if (expr[i] == '/') {
- printf("SPEC 3\n");
- continue;
- }
- if (expr[i] == '(') {
- printf("SPEC 4\n");
- continue;
- }
- if (expr[i] == ')') {
- printf("SPEC 5\n");
- continue;
- }
- if (expr[i] != ' ') {
- cur[pos] = expr[i];
- pos++;
- cur[pos] = 0;
- len++;
- }
- }
- }
- if (len != 0) {
- pos = 0;
- len = 0;
- if (t != NULL) now = Descend(t, cur);
- else now = NULL;
- if (now == NULL) {
- char *curr = (char*)malloc((len + 1) * sizeof(char));
- for (j = 0; j < len; j++) curr[j] = cur[j];
- curr[j] = 0;
- Insert(t, curr, id);
- printf("IDENT %d\n", id);
- id++;
- } else printf("IDENT %d\n", now->v);
- }
- free(expr);
- SuperFree(t);
- return 0;
- }
Add Comment
Please, Sign In to add comment