-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfds10.txt
89 lines (89 loc) · 1.19 KB
/
fds10.txt
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
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int getWeight(char ch)
{
switch (ch)
{
case '/':
case '*': return 2;
case '+':
case '-': return 1;
default : return 0;
}
}
void infix2postfix(char infix[], char postfix[], int size)
{
stack<char> s;
int weight;
int i = 0;
int k = 0;
char ch;
while (i < size)
{
ch = infix[i];
if (ch == '(')
{
s.push(ch);
i++;
continue;
}
if (ch == ')')
{
while (!s.empty() && s.top() != '(')
{
postfix[k++] = s.top();
s.pop();
}
if (!s.empty())
{
s.pop();
}
i++;
continue;
}
weight = getWeight(ch);
if (weight == 0)
{
postfix[k++] = ch;
}
else
{
if (s.empty())
{
s.push(ch);
}
else
{
while (!s.empty() && s.top() != '(' && weight <=
getWeight(s.top()))
{
postfix[k++] = s.top();
s.pop();
}
s.push(ch);
}
}
i++;
}
while (!s.empty())
{
postfix[k++] = s.top();
s.pop();
}
postfix[k] = 0;
}
int main()
{
char infix[100];
cout<<"\nENter Infix Operation:";
cin>>infix;
int size = strlen(infix);
char postfix[size];
infix2postfix(infix,postfix,size);
cout<<"\nInfix Expression :: "<<infix;
cout<<"\nPostfix Expression :: "<<postfix;
cout<<endl;
return 0;
}