-
Notifications
You must be signed in to change notification settings - Fork 0
/
Infix_postfix.cpp
152 lines (145 loc) · 3.48 KB
/
Infix_postfix.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class Stack
{ private:
int top= -1, size= 100;
char Stack[100];
public:
char Push(char ch)
{ if (top!= size-1)
{
top++;
Stack[top]=ch;
}
}
char Pop()
{ char temp;
if (top!= -1)
{
temp=Stack[top];
top--;
return temp;
}
else
{
return '\0';
}
}
char Topele()
{ char ch;
if(top!= -1)
{ ch=Stack[top];
return ch;
}
else
{
return '\0';
}
}
}S;
int precedence(char ch) //function to check precendence of the operators
{switch(ch)
{ case '^':return 7;
case '/':return 6;
case '*':return 6;
case '+':return 5;
case '-':return 5;
case '<':return 4;
case '>':return 4;
case '&':return 3;
case '|':return 2;
default: return 0;
}
}
bool check_braces(string s) //function to check equal number of braces(in out) present or not
{ int leftbr=0, rightbr=0;
for (int i=0; s[i]; i++)
{ if (s[i]=='(')
{
leftbr++;
}
else if (s[i]==')')
{
rightbr++;
}
}
if(leftbr==rightbr)
{
return true;
}
else
{
return false;
}
}
string infixToPostfix( string s ) // The function to calculate
{ bool chk=check_braces( s );
char ch;
string postfix; //another string to get the postfix expression
if (chk==false )
{
cout<<"\nUnbalanced no. of braces\n Extra bracket";
}
for (int i=0; s[i]!='\0'; i++)
{ cout<< s[i] <<endl;
if (isalnum( s[i] ) || s[i]=='.') //alphabet and numeric value encountered
{
postfix += s[i]; //paste it to postfix expression
}
else if( s[i]=='(' ) // first braces encountered
{
S.Push( s[i]); //push it to stack
}
else if(s[i]==')') //'close first braces encountered
{
while( ((ch=S.Topele())!='(') && (S.Topele()!='\0') ) //check no null or no first braces encountered
{ S.Pop(); //pop value and movie it to
postfix +=ch;
}
if(S.Topele()=='(')
{
S.Pop();
}
}
else
{
while((S.Topele()!='\0')&&(precedence(S.Topele())>=precedence(s[i]))&&(s[i]!='&')&&(s[i]!='|'))
{
ch=S.Topele();
S.Pop();
postfix +=ch;
}
S.Push(s[i]);
}
}
while(S.Topele()!='\0')
{ ch=S.Topele();
S.Pop();
postfix +=ch;
}
return postfix;
}
int main()
{ int choice; string line;
cout<<"Infix to Postfix\n";
ifstream fiP; //file opened in read mode
fiP.open("Infix.txt", ios::in);
if(!fiP) //if statement to check is file is null or doesn't exist
{
cout<<"File can\'t be opened \n";
}
else
{
while(!fiP.eof()) //while loop untill end of file
{
getline(fiP,line); //line take the infix expression from file
cout<<"Infix Expression="<<line<<endl;
cout<<"Postfix Expression="<<infixToPostfix(line)<<"\n\n";
}
}
fiP.close(); // read mode file closed
return 0;
}