-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfix_to_postfix.cpp
More file actions
60 lines (60 loc) · 1.34 KB
/
Copy pathinfix_to_postfix.cpp
File metadata and controls
60 lines (60 loc) · 1.34 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
#include<iostream>
using namespace std;
void Push(char Stack[] ,int& top ,char x){
top++;
Stack[top]=x;
}
char Pop(char Stack[] ,int& top){
char x = Stack[top];
top--;
return x;
}
int priority(char t)
{
if (t == '*' || t == '/')
return 2;
else if (t == '+' || t == '-')
return 1;
else
return -1;
}
int main()
{
char Stack[50];
int top=-1;
string inFix;
cout<<"enter infix expression:\n";
cin>>inFix;
string postFix = "\0";
for(int i=0;i<inFix.size();i++){
if(inFix[i]=='('){
Push(Stack,top,inFix[i]);
}
else if(inFix[i]==')'){
char ch = Pop(Stack ,top);
while (ch!='(')
{
postFix += ch;
ch =Pop(Stack ,top);
}
}
else if(inFix[i]=='*'||inFix[i]=='/'||inFix[i]=='-'||inFix[i]=='+'){
while (top!=-1 && priority(inFix[i]) <= priority(Stack[top]))
{
char ch = Pop(Stack ,top);
postFix += ch;
}
Push(Stack,top,inFix[i]);
}
else{
postFix += inFix[i];
}
}
while (top!=-1)
{
char ch = Pop(Stack ,top);
postFix += ch;
}
cout<<postFix<<endl;
return 0;
}