|
| 1 | +#include<stdio.h> |
| 2 | +#include<string.h> |
| 3 | +#include<stdlib.h> |
| 4 | +#include"BST.h" |
| 5 | +#include"Stack.h" |
| 6 | + |
| 7 | +Node* createEXTNode(char data, Node *left, Node *right) { |
| 8 | + Node *newNode = malloc(sizeof(Node)); |
| 9 | + newNode->data = data; |
| 10 | + newNode->left = left; |
| 11 | + newNode->right = right; |
| 12 | + return newNode; |
| 13 | +} |
| 14 | + |
| 15 | +int is_operator(char data) { |
| 16 | + switch(data) { |
| 17 | + case '+': |
| 18 | + return 1; |
| 19 | + case '-': |
| 20 | + return 1; |
| 21 | + case '*': |
| 22 | + return 1; |
| 23 | + case '/': |
| 24 | + return 1; |
| 25 | + } |
| 26 | + return 0; |
| 27 | +} |
| 28 | + |
| 29 | +int precedance(char ch) { |
| 30 | + if(ch == '+' || ch == '-') { |
| 31 | + return 1; |
| 32 | + } else if(ch == '*' || ch == '/') { |
| 33 | + return 2; |
| 34 | + } else if(ch == '^') { |
| 35 | + return 3; |
| 36 | + } else { |
| 37 | + return 0; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +int isAlNum(char ch) { |
| 42 | + //48-57 0-9 |
| 43 | + //65-90 A-Z |
| 44 | + //97-122 a -z |
| 45 | + |
| 46 | + if(((int)ch) >= 48 && ((int)ch) <= 57) { |
| 47 | + return 1; |
| 48 | + } |
| 49 | + if(((int)ch) >= 65 && ((int)ch) <= 90) { |
| 50 | + return 1; |
| 51 | + } |
| 52 | + if(((int)ch) >= 97 && ((int)ch) <= 122) { |
| 53 | + return 1; |
| 54 | + } |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +char* convertToPostFix(char* inputString, int size) { |
| 59 | + int index = 0; |
| 60 | + CharStackList stack; |
| 61 | + initCharStack(&stack); |
| 62 | + |
| 63 | + char* postfix = malloc(sizeof(char) * size); |
| 64 | + char *temp = postfix; |
| 65 | + while(*inputString != '\0') { |
| 66 | + if(isAlNum(*inputString)) { |
| 67 | + *postfix = *inputString; |
| 68 | + postfix++; |
| 69 | + } else if(*inputString == '(') { |
| 70 | + pushChar(&stack, *inputString); |
| 71 | + } else if(*inputString == '^') { |
| 72 | + pushChar(&stack, *inputString); |
| 73 | + } else if(*inputString == ')') { |
| 74 | + while(peekChar(stack) != '(') { |
| 75 | + *postfix = peekChar(stack); |
| 76 | + postfix++; |
| 77 | + popChar(&stack); |
| 78 | + } |
| 79 | + popChar(&stack); |
| 80 | + } |
| 81 | + else { |
| 82 | + if(precedance(*inputString) > precedance(peekChar(stack))) { |
| 83 | + pushChar(&stack, *inputString); |
| 84 | + } else { |
| 85 | + while(precedance(*inputString) <= precedance(peekChar(stack))) { |
| 86 | + *postfix = peekChar(stack); |
| 87 | + postfix++; |
| 88 | + popChar(&stack); |
| 89 | + } |
| 90 | + pushChar(&stack, *inputString); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + inputString++; |
| 95 | + } |
| 96 | + |
| 97 | + while(peekChar(stack) != '\0') { |
| 98 | + *postfix = peekChar(stack); |
| 99 | + postfix++; |
| 100 | + popChar(&stack); |
| 101 | + } |
| 102 | + *postfix = '\0'; |
| 103 | + postfix = temp; |
| 104 | + |
| 105 | + return postfix; |
| 106 | +} |
| 107 | + |
| 108 | +void disp(char* str) { |
| 109 | + while(*str != '\0') { |
| 110 | + printf("%c", *str); |
| 111 | + str++; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void initTree(EXT *tree, char* inputString, int size) { |
| 116 | + inputString = convertToPostFix(inputString, size); |
| 117 | + |
| 118 | + int index = 0; |
| 119 | + StackList stack; |
| 120 | + initStack(&stack); |
| 121 | + while(inputString[index] != '\0') { |
| 122 | + if(!is_operator(inputString[index])) { |
| 123 | + push(&stack, (void *) createEXTNode(inputString[index], NULL, NULL)); |
| 124 | + } else { |
| 125 | + Node* element2 = (Node *)peek(stack); |
| 126 | + pop(&stack); |
| 127 | + |
| 128 | + Node* element1 = (Node *)peek(stack); |
| 129 | + pop(&stack); |
| 130 | + |
| 131 | + push(&stack, (Node *)createEXTNode(inputString[index], element1, element2)); |
| 132 | + } |
| 133 | + index++; |
| 134 | + } |
| 135 | + |
| 136 | + Node* finalTree = (Node *) peek(stack); |
| 137 | + pop(&stack); |
| 138 | + |
| 139 | + *tree = finalTree; |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +void traverse(EXT tree) { |
| 144 | + if(tree != NULL) { |
| 145 | + traverse(tree->left); |
| 146 | + printf("%c \n", tree->data); |
| 147 | + traverse(tree->right); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +int compute(EXT tree) { |
| 152 | + if(tree == NULL) |
| 153 | + return 0; |
| 154 | + |
| 155 | + if(tree->left == NULL && tree->right == NULL) |
| 156 | + return ((int)tree->data) - 48; |
| 157 | + |
| 158 | + int left = compute(tree->left); |
| 159 | + int right = compute(tree->right); |
| 160 | + |
| 161 | + if(tree->data == '+') |
| 162 | + return left + right; |
| 163 | + if(tree->data == '-') |
| 164 | + return left - right; |
| 165 | + if(tree->data == '*') |
| 166 | + return left * right; |
| 167 | + if(tree->data == '/') |
| 168 | + return left / right; |
| 169 | + |
| 170 | + return -1; |
| 171 | +} |
| 172 | + |
| 173 | + |
0 commit comments