-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35-infix_postfix.cpp
More file actions
85 lines (82 loc) · 1.13 KB
/
35-infix_postfix.cpp
File metadata and controls
85 lines (82 loc) · 1.13 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
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>
using namespace std;
class stack
{
public:
char symbol,s[30],infix[30],postfix[30];
int top,i,j;
stack()
{
top=-1;
}
int g(char symbol)
{
switch(symbol)
{
case '+':
case '-': return 1;
case '*':
case '/': return 3;
case '^':
case '$': return 6;
case '(':return 9;
case ')': return 0;
default:return 7;
}
}
int f(char symbol)
{
switch(symbol)
{
case '+':
case '-': return 2;
case '*':
case '/': return 4;
case '^':
case '$': return 5;
case '(':return 0;
case '#': return -1;
default:return 8;
}
}
void infix_postfix()
{
s[++top]='#';
j=0;
for( i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while((f(s[top])>g(symbol)))
{
postfix[j++]=s[top--];
}
if(f(s[top])!=(g(symbol)))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
}
void display()
{
cout<<"enter infix expression \n";
cin>>infix;
infix_postfix();
cout<<"new code:\n";
cout<<postfix;
}
};
int main()
{
stack obj;
obj.display();
getch();
return 0;
}