-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionTreeRE4.c
128 lines (126 loc) · 3.89 KB
/
ExpressionTreeRE4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/******************************************************************************************
* Visit https://github.com/the10minoverview/ExpressionTreeRE.c for all files related to *
* project. *
* *
* *
* *
* *
* *
* Created by B I *
******************************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
typedef struct node{
bool x;
int value;
char data;
struct node *lp;
struct node *rp;
}* Node;
int ip(char x);
Node Tree(char pf[]);
int answer(Node root);
int Valid(Node root);
int main(void) {
char in[20];
Node ExpTree = NULL;
int ans;
printf("Enter the valid expression:\n");
scanf("%s",in);
ExpTree = Tree(in);
ans = answer(ExpTree);
printf("The Answer is %d\n",ans);
return 0;
}
int ip(char x){
if(x == '(' || x == '#' )
return 0;
else if (x == '+' || x == '-')
return 1;
else if (x == '*' || x == '/')
return 2;
else if (x =='$' || x == '^' )
return 3;
else
printf(" %c was to be tested\n",x);
exit(0);
}
Node Tree(char in[]){
Node DigStk[10],OpStk[10];
int j=0,topD=-1,topO= 0;
OpStk[0] = (Node)malloc(sizeof(struct node));
OpStk[0]->data = '#';
while(in[j]!='\0'){
Node temp = NULL;
temp = (Node)malloc(sizeof(struct node));
temp->data = in[j++];
temp->lp = NULL;
temp->rp = NULL;
if(temp->data =='-' && ( j<2 || (!isdigit(in[j-2]) && in[j-2] != ')'))){
temp->x = 1;
temp->value = 0;
while(isdigit(in[j]))
temp->value = temp->value*10 - (int)in[j++] + 48;
DigStk[++topD] = temp;
}
else if(isdigit(temp->data)){
temp->x = 1;
temp->value = (int)temp->data - 48;
while(isdigit(in[j]))
temp->value = temp->value*10 + (int)in[j++]-48;
DigStk[++topD] = temp;
}
else{
temp->x = 0;
if (temp->data=='(')
OpStk[++topO]=temp;
else if (temp->data==')'){
while (OpStk[topO]->data!='(') {
OpStk[topO]->rp = DigStk[topD--];
OpStk[topO]->lp = DigStk[topD];
DigStk[topD] = OpStk[topO--];
}
free(temp);
free(OpStk[topO--]);
}
else{
if(ip(OpStk[topO]->data) < ip(temp->data))
OpStk[++topO] = temp;
else{
while (ip(OpStk[topO]->data) >= ip(temp->data)) {
OpStk[topO]->rp = DigStk[topD--];
OpStk[topO]->lp = DigStk[topD];
DigStk[topD] = OpStk[topO--];
}
OpStk[++topO] = temp;
}
}
}
}
while (OpStk[topO]->data!='#')
{
OpStk[topO]->rp = DigStk[topD--];
OpStk[topO]->lp = DigStk[topD];
DigStk[topD] = OpStk[topO--];
}
return DigStk[topD];
}
int answer(Node root){
if(root->x)
return root->value;
else
switch(root->data)
{
case '+': return (answer(root->lp)+answer(root->rp));
case '-': return (answer(root->lp)-answer(root->rp));
case '*': return (answer(root->lp)*answer(root->rp));
case '/': return (answer(root->lp)/answer(root->rp));
case '^':
case '$': return ((int)pow((double)answer(root->lp),(double)answer(root->rp)));
default: return 0;
}
}