-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfix_to_Prefix_and_Postfix.cpp
More file actions
163 lines (145 loc) · 3.51 KB
/
Copy pathInfix_to_Prefix_and_Postfix.cpp
File metadata and controls
163 lines (145 loc) · 3.51 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
// Description:
// Try to transform an infix expression into its prefix and postfix equivalent with C++.
// Assume that the operators in the expression only include (, ), *, /, +, and -. Design a class called Stack and implement the necessary member functions, such as push() and pop() to accomplish the infix transformation.
// Input: An infix expression such as A*B/C.
#include <iostream>
using namespace std;
// implement stack
class Stack{
public:
int capacity;
int top=-1;
char *array;
void setStack(int n){
capacity=n;
array=new char[n];
}
bool isEmpty(){
if(top<0){
return true;
}else{
return false;
}
}
void push(char token){
if(top>=capacity-1){
throw "The stack is full.";
}else{
top++;
array[top]=token;
}
}
void pop(){
if(isEmpty()){
throw "The stack is empty.";
}else{
top--;
}
}
char Top(){
return array[top];
}
};
int isp(char token){
if(token=='+'||token=='-'){
return 2;
}else if(token=='#'||token=='('||token==')'){
return 3;
}else{
return 1;
}
}
// find the incoming priority
int icp(char token){
if(token=='+'||token=='-'){
return 2;
}else if(token=='#'){
return 0;
}else if(token=='('||token==')'){
return -1;
}else{
return 1;
}
}
// check if it is an operand
bool isOperand(char token){
if(token == '-'||token=='+'||token =='*'||token=='/'||token==')'||token=='('){
return false;
}else{
return true;
}
}
// convert to postfix
void Postfix(string e){
Stack stack;
stack.setStack((int)e.length());
stack.push('#');
cout<<"Postfix: ";
for(int i=0; i<(int)e.length();i++){
if(isOperand(e[i])){
cout<<e[i];
}else if(e[i]==')'){
for(;stack.Top()!='(';stack.pop())
cout<<stack.Top();
stack.pop();
}else if(e[i]=='('){
stack.push(e[i]);
}else{
for(;isp(stack.Top())<=icp(e[i]);stack.pop())
cout<<stack.Top();
stack.push(e[i]);
}
}
for(;!stack.isEmpty();stack.pop()){
if(stack.Top()=='#')
break;
cout<<stack.Top();
}
cout<<"\n";
}
// convert to postfix
void Prefix(string e){
char p[(int)e.length()];
for(int i =0; i<(int)e.length();i++){
p[i]=e[(int)e.length()-1-i];
}
Stack stack;
stack.setStack((int)e.length());
stack.push('#');
char d [(int)e.length()];
int num=0;
for(int i=0; i<stack.capacity;i++){
if(isOperand(p[i])){
d[num++]=p[i];
}else if(p[i]=='('){
for(;stack.Top()!=')';stack.pop())
d[num++]=stack.Top();
stack.pop();
}else if(p[i]==')'){
stack.push(p[i]);
}
else{
for(;isp(stack.Top())<icp(p[i]);stack.pop())
d[num++]=stack.Top();
stack.push(p[i]);
}
}
for(;!stack.isEmpty();stack.pop()){
if(stack.Top()=='#')
break;
d[num++]=stack.Top();
}
cout<<"Prefix: ";
for(int i =num-1; i>=0;i--){
cout<<d[i];
}
cout<<"\n"<<endl;
}
int main() {
string infixexpression;
while(cin>>infixexpression){
Postfix(infixexpression);
Prefix(infixexpression);
}
cout<<"\n";
}