-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfix_Infix.cpp
More file actions
183 lines (176 loc) · 2.87 KB
/
Postfix_Infix.cpp
File metadata and controls
183 lines (176 loc) · 2.87 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
/*
struct node
{
char data ;
node *next ;
};
class stack
{
private :
node *top;
public :
stack()
{
top = NULL;
}
void push(char a)
{
node * temp = new node();
temp ->data = a ;
temp -> next = top ;
top = temp ;
}
char pop()
{
node *temp = top ;
top = temp->next ;
char a = temp->data ;
delete temp;
return a;
}
char see_top()
{
if(is_empty())
return '0' ;
node * temp = top ;
return (temp->data);
}
int is_empty()
{
if(top == NULL)
return 1;
else
return 0 ;
}
}S;
*/
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)
{ if(is_empty())
return 0;
ch=Stack[top];
return ch;
}
else
{
return '\0';
}
}
/*char gettop()
{
if(is_empty())
return '0' ;
node * temp = head ;
return (temp->data);
}*/
int is_empty()
{
if(top!=-1)
return 1;
else
return 0;
}
}S ;
int is_allnum(char an)
{
if(isalnum(an))
return 1;
else
return 0;
}
int is_operand(char ch)
{
switch(ch)
{
case '*' :return 2;
case '/' :return 2;
case '+' : return 1;
case '-' : return 1;
default : return 0;
}
}
void reverse(string c, int l)
{
int i=0;
for(int j=0;j<l;j++)
{
S.Push(c[j]);
}
while(!S.is_empty())
c[i++]=S.Pop();
return ;
}
string PostfixToInfix(string s)
{
string infix;
int size= s.length();
reverse(s,size);
for(int i=0;i<size;i++)
{
if(is_allnum(s[i]))
{
infix += s[i];
if(i==size-1)
break ;
infix += S.Pop() ;
}
else
S.Push(s[i]);
}
while(!S.is_empty())
{
infix +=S.Pop();
}
reverse(infix,size);
return infix;
}
int main()
{ int choice; string line;
cout<<" Postfix to Infix\n";
ifstream fPi; //file opened in read mode
fPi.open("Postfix.txt", ios::in);
if(!fPi) //if statement to check is file is null or doesn't exist
{
cout<<"File can\'t be opened \n";
}
else
{
while(!fPi.eof()) //while loop untill end of file
{
getline(fPi,line); //line take the infix expression from file
cout<< PostfixToInfix(line) <<endl;
}
}
fPi.close(); // read mode file closed
return 0;
}