-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINTOPRE.BAK
79 lines (75 loc) · 1.35 KB
/
INTOPRE.BAK
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
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 20
char stack[max];
int top=-1;
void push(char x)
{
if(top==(max-1))
printf("overflow of stack");
else
stack[++top]=x;
}
char pop()
{
if(top==-1)
{ printf("underflow of stack");
return -1 ;
}
else
{
return(stack[top--]);
}
}
int priority(char x)
{
if(x=='(') return 0;
else if(x=='+'|| x=='-') return 1;
else if(x=='*'|| x=='/') return 2;
else return -1;
}
void main()
{ int i,l,t1,t2,k=0;
char exp[20],rev[20];
clrscr();
printf("Enter the infix expression\n");
scanf("%s",exp);
l=strlen(exp);
for(i=l-1;i>=0;i--)
{ if(exp[i]=='(')
rev[k++]=')';
else if(exp[i]==')')
rev[k++]='(';
else
rev[k++]=exp[i];
}
rev[k++]='\0';
rev[k]=')';
push('(');
k=0;
for(i=0;i<=l;i++)
{
if( (rev[i] >=97 && rev[i]<122) ||(rev[i] >=65 && rev[i]<90))
{
exp[k++]=rev[i];
}
else if(rev[i]=='(')
push(rev[i]);
else if(rev[i]==')')
{ while(stack[top]!='(')
exp[k++]=pop();
pop();
}
else
{ t1=priority(stack[top]);
t2=priority(exp[i]);
while(t1>t2 && stack[top]!='(')
exp[k++]=pop();
push(rev[i]);
}
}
for(i=k-1;i>=0;i--)
printf("%c",exp[i]);
getch();
}