forked from KISHOREMUTHU/Data-Structures-And-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfix_to_postfix.cpp
More file actions
104 lines (80 loc) · 1.64 KB
/
Copy pathinfix_to_postfix.cpp
File metadata and controls
104 lines (80 loc) · 1.64 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Data-Structures-And-Algorithms
Here I will post my regular DSA problems
// Program to convert infix expression to postfix expression
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
char s[20];
int top=-1;
// Function to assign value based on priority
int priority(char c)
{
if( c == '(' )
return(0);
else if( ( c == '+' ) || ( c == '-' ) )
return(1);
else if( ( c == '*' ) || ( c == '/' ) )
return(2);
return 0;
}
// Pooping the element from the stack
void pop()
{
if( top == -1 )
cout << "underflow";
else
{
if( top >= 0 )
{
cout << s[top];
top--;
}
}
}
// Pushing the element into the stack based on priority
void push(char d)
{
if( top == 20 )
cout << "overflow";
else
{
if( top == -1 )
s[++top] = d;
else if( d == '(' )
s[++top] = d;
else
{
while( priority( s[top] ) >= priority( d ) )
pop();
s[++top] = d;
}
}
}
int main()
{
int i,n;
char a[30];
cout << "Enter the expression";
cin >> a;
n=strlen(a);
for(i=0;i<n;i++)
{
// Checking whether the input is a number or an alphabet
if( isalnum( a[i] ) )
cout << a[i];
else if( a[i] == '(' )
push( a[i] );
else if( a[i] == ')' )
{
while( s[top] != '(' )
pop();
top--;
}
else
push(a[i]);
}
while( top >= 0 )
pop();
return 0;
}