-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfixToPostfux.cpp
More file actions
70 lines (68 loc) · 1.62 KB
/
Copy pathinfixToPostfux.cpp
File metadata and controls
70 lines (68 loc) · 1.62 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
#include<iostream>
#include"stackFormulaBased1.cpp"
using namespace std;
int getprecedence(char ch)
{
switch(ch)
{
case'*':
case'/':
case'%':return 3;
case'+':
case'-':return 2;
case'(':return 1;
case'$':return 0;
}
}
void infixtopostfix(char *infix,char *postfix)
{
stack<char> stk(10);
stk.push('$');
int i=0,j=0;
char ch, chr;
while(infix[i]!='\0')
{
ch=infix[i];
if(isalpha(ch)||isdigit(ch))
postfix[j++]=ch;
else
{
switch(ch)
{
case'(':stk.push(ch);
break;
case')':while(chr=stk.gettop()!='(')
postfix[j++]=stk.pop();
break;
case'+':
case'-':
case'*':
case'/':
int prec1=getprecedence(ch);
int prec2=getprecedence(stk.gettop());
while(prec2>prec1)
{
postfix[j++]=stk.pop();
prec2=getprecedence(stk.gettop());
}
stk.push(ch);
break;
}
}
i++;
}
while((chr=stk.pop())!='$')
postfix[j++]=chr;
postfix[j++]='\0';
}
int main()
{
{
char infix[20],postfix[20];
cout<<"/n enter infix expression";
cin>>infix;
infixtopostfix(infix,postfix);
cout<<"\n equivalent expression is:"<<postfix;
return 0;
}
}