-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack expression-II.c
More file actions
224 lines (215 loc) · 4.15 KB
/
Stack expression-II.c
File metadata and controls
224 lines (215 loc) · 4.15 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
224
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct stack{
int data;
int size;
struct stack*next;
};
char oper(int o){
if(o==1){
return '(';
}
else if(o==-1){
return '#';
}
else if(o==0){
return '$';
}
else if(o==2){
return '+' ;
}
else if(o==3){
return '-';
}
else if(o==6){
return '*';
}
else if(o==7){
return '/';
}
else if(o==10){
return '^';
}
}
int operate(int n1,int n2,int o){
if(o==2){
return n1+n2;
}
else if(o==3){
return n1-n2;
}
else if(o==6){
return n1*n2;
}
else if(o==7){
return n1/n2;
}
else if(o==10){
return n1^n2;
}
}
int inprior( char c){
if(c=='$'){
return 0;
}
else if(c=='-'){
return 3;
}
else if(c=='+'){
return 2;
}
else if(c=='*'){
return 6;
}
else if(c=='/'){
return 7;
}
else if(c=='^'){
return 10;
}
else if(c=='('){
return 1;
}
else if(c=='#'){
return -1;
}
else if(c==')'){
return 100;
}
else{
return -2;
}
}
int outprior( char c){
if(c=='$'){
return 0;
}
else if(c=='-'){
return 4;
}
else if(c=='+'){
return 2;
}
else if(c=='*'){
return 6;
}
else if(c=='/'){
return 8;
}
else if(c=='^'){
return 12;
}
else if(c=='('){
return 16;
}
else if(c=='#'){
return -1;
}
else{
return -2;
}
}
struct stack* newnode(int data){
struct stack* node=(struct stack*)(malloc(sizeof(struct stack)));
node->data=data;
node->next=NULL;
return node;
}
int isempty(struct stack**head){
if((*head)==NULL){
return 1;
}
else {
return 0;
}
}
void push(struct stack**head,int val){
struct stack*node=newnode(val);
if((*head)==NULL){
(*head)=node;
(*head)->size=1;
return;
}
node->size=(*head)->size;
node->next=(*head);
(*head)=node;
(*head)->size=node->size+1;
return;
}
void pop(struct stack**head){
if(isempty(head)){
return;
}
if((*head)->next==NULL){
(*head)=NULL;
return;
}
struct stack*temp=(*head);
(*head)=(*head)->next;
(*head)->size=temp->size-1;
free(temp);
}
int top(struct stack**head){
if(isempty(head)){
return -1;
}
return (*head)->data;
}
int size(struct stack**head){
if((*head)==NULL){
return 0;
}
return (*head)->size;
}
int main(){
struct stack*O=NULL;
struct stack*N=NULL;
push(&O,inprior('$'));
while(oper(top(&O))!='#'){
char c;
scanf("%c",&c);
int num=0;
if(inprior(c)==-2){
while(inprior(c)==-2){
num=num*10+(int)c-48;
scanf("%c",&c);
}
push(&N,num);
}
// printf("%c\n",oper(top(&O)));
if(c==')'){
while(oper(top(&O))!='('){
int o=top(&O);
pop(&O);
int n2=top(&N);
pop(&N);
int n1=top(&N);
pop(&N);
int ans=operate(n1,n2,o);
push(&N,ans);
// printf("*%d\n",top(&N));
}
pop(&O);
}
else {
while(top(&O)/2>=outprior(c)/2 && oper(top(&O))!='$' ){
int o=top(&O);
pop(&O);
int n2=top(&N);
pop(&N);
int n1=top(&N);
pop(&N);
int ans=operate(n1,n2,o);
push(&N,ans);
if(oper(c)=='#' && size(&O)<=1){
break;
}
}
push(&O,inprior(c));
}
}
printf("%d",top(&N));
return 0;
}