-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInToPost.c
76 lines (72 loc) · 1.37 KB
/
InToPost.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
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#define size 5
struct stack
{
char a[size];
int top;
}obj;
void push(char n)
{
if(obj.top==size-1)
printf("Stack full\n");
else
{
obj.top++;
obj.a[obj.top]=n;
printf("Pushed: %c\n",n);
}
}
char pop()
{
if(obj.top==-1){
printf("Stack is empty");
return 0;
}
else
{
char temp=obj.a[obj.top];
obj.top--;
printf("Popped: %c\n",temp);
return temp;
}
}
void init()
{
obj.top=-1;
}
int priority(char x)
{
if(x == '(')
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/' || x == '%')
return(2);
return(3);
}
int main(int argc, char **argv)
{
init();
char c;int t;
while((c=getchar())!='n') //until we get 'n'
{
if(isalnum(c))
printf("%c\n",c);
else if(!iscntrl(c))
{
if(c=='(')
push(c);
else if(c==')')
while((t=pop())!='(' && t!=0){}
else
{
while(priority(c)<=priority(obj.a[obj.top]) && obj.top>-1)
pop();
push(c);
}
}
}
return 0;
}