Repository navigation
Expand file tree
/
Copy pathExpressionTree.c
More file actions
223 lines (222 loc) · 4.26 KB
/
Copy pathExpressionTree.c
File metadata and controls
223 lines (222 loc) · 4.26 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include<stdio.h>
#include<stdlib.h>
#define MainStream 1
#define Maxsize 100
typedef struct tree
{
union {char op;int number;};
struct tree* lchild;
struct tree* rchild;
}TreeNode;
typedef struct node
{
int top;
TreeNode* Tstack[Maxsize];
}Stack;
Stack MainStack;
Stack SubStack;
TreeNode* root;
int BackExpression[100][2];
void init();
int getNum();
void SinglePush(int data);
TreeNode* popup();
void Combine(char op);
TreeNode* getNode();
void CreatTree();
void BackTransition();
void myprint(TreeNode p);
void printleaves();
int calculate(TreeNode* p);
int main()
{
init();
BackTransition();
CreatTree();
printleaves();
return 0;
}
void init()
{
MainStack.top=0;
SubStack.top=0;
root=(TreeNode*)malloc(sizeof(TreeNode));
}
void SinglePush(int data)
{
MainStack.Tstack[MainStack.top]=(TreeNode*)malloc(sizeof(TreeNode));
(MainStack.Tstack[MainStack.top])->lchild=NULL;
(MainStack.Tstack[MainStack.top])->rchild=NULL;
(MainStack.Tstack[MainStack.top])->number=data;
MainStack.top++;
}
void pushTree(TreeNode* third)
{
MainStack.Tstack[MainStack.top]=third;
MainStack.top++;
}
void Combine(char op)
{
TreeNode* First;
TreeNode* Second;
TreeNode* Third=(TreeNode*)malloc(sizeof(TreeNode));
Second=popup();
First=popup();
Third->op=op;
Third->lchild=First;
Third->rchild=Second;
pushTree(Third);
}
TreeNode* popup()
{
MainStack.top--;
return MainStack.Tstack[MainStack.top];
}
void CreatTree()
{
int i;
for(i=0;BackExpression[i][1]!=-1;i++)
{
if(BackExpression[i][1]==0)
{
SinglePush(BackExpression[i][0]);
continue;
}
if(BackExpression[i][1]==1)
{
Combine(BackExpression[i][0]);
continue;
}
}
}
void BackTransition()
{
char temp;
char stack[100]={0};
int top1=0,top2=-1,prior[150]={0};
prior['(']=0;
prior[')']=0;
prior['=']=0;
prior['+']=1;
prior['-']=1;
prior['*']=2;
prior['/']=2;
prior['%']=2;
while(~scanf("%c",&temp)&&stack[top2]!='=')
{
if(temp>='0'&&temp<='9')
{
int num=0;
while(temp>='0'&&temp<='9')
{
num=num*10+temp-'0';
scanf("%c",&temp);
}
BackExpression[top1++][0]=num;
}
if(temp==' ')
continue;
if(top2==-1||temp=='(')
{
stack[++top2]=temp;
continue;
}
else
{
int d=stack[top2];
int n=prior[temp]-prior[d];
if(n>0)
{
stack[++top2]=temp;
continue;
}
while(n<=0)
{
d=stack[top2--];
BackExpression[top1][1]=1;
BackExpression[top1++][0]=d;
if(top2==-1)
break;
d=stack[top2];
if(d=='('&&temp==')')
break;
n=prior[temp]-prior[d];
}
if(d=='('&&temp==')')
{
top2--;
continue;
}
stack[++top2]=temp;
continue;
}
}
BackExpression[top1][1]=-1;
return;
}
void printleaves()
{
root=MainStack.Tstack[0];
if(root!=NULL)
{
myprint(*root);
}
if(root->lchild!=NULL)
{
printf(" ");
myprint(*(root->lchild));
}
else
{
printf("\n");
}
if(root->rchild!=NULL)
{
printf(" ");
myprint(*(root->rchild));
printf("\n");
}
printf("%d\n",calculate(root));
return;
}
int calculate(TreeNode* p)
{
if(p->lchild==NULL&&p->rchild==NULL)
{
return p->number;
}
int a=calculate(p->lchild);
int b=calculate(p->rchild);
switch (p->op)
{
case '+':
{
return a+b;
}
case '-':
{
return a-b;
}
case '*':
{
return a*b;
}
case '/':
{
return a/b;
}
}
}
void myprint(TreeNode p)
{
if(p.lchild!=NULL)
{
printf("%c",p.op);
return;
}
else
{
printf("%d",p.number);
return;
}
}