-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathinfixtopostfix.rs
240 lines (214 loc) · 7.09 KB
/
infixtopostfix.rs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::io::{self, Read};
fn main() {
let input_str = read_user_input();
let original_tokens = tokenize(&input_str);
let result = infix_to_postfix(&original_tokens);
println!("{:?}", result);
}
pub fn read_user_input() -> String {
let mut buffer = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
handle.read_to_string(&mut buffer);
buffer
}
pub fn tokenize(input_str: &String) -> Vec<InfixToken> {
let mut tokens: Vec<InfixToken> = Vec::new();
for ele in input_str.split_whitespace() {
if ele == "+" {
tokens.push(InfixToken::Operator(Operator::Add));
}
else if ele == "-" {
tokens.push(InfixToken::Operator(Operator::Sub));
}
else if ele == "*" {
tokens.push(InfixToken::Operator(Operator::Mul));
}
else if ele == "/" {
tokens.push(InfixToken::Operator(Operator::Div));
}
else if ele == "(" {
tokens.push(InfixToken::LeftParen);
}
else if ele == ")" {
tokens.push(InfixToken::RightParen);
}
else if ele == "\n" {
continue;
}
else {
tokens.push(InfixToken::Operand(ele.parse::<isize>().unwrap()));
}
}
tokens
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Operator {
// `+`
Add,
// `-`
Sub,
// `*`
Mul,
// `/`
Div,
}
#[derive(Debug, PartialEq)]
pub enum InfixToken {
Operator(Operator),
Operand(isize),
LeftParen,
RightParen,
}
#[derive(Debug, PartialEq)]
pub enum PostfixToken {
Operator(Operator),
Operand(isize),
}
/// Transforms an infix expression to a postfix expression.
///
/// If the infix expression is valid, outputs `Some(_)`;
/// otherwise, outputs `None`.
pub fn infix_to_postfix(tokens: &[InfixToken]) -> Option<Vec<PostfixToken>> {
if check_valid(tokens) {
let mut stack = Vec::new();
let mut output_vec: Vec<PostfixToken> = Vec::new();
for token in tokens {
if let InfixToken::Operand(value) = token {
output_vec.push(PostfixToken::Operand(*value));
}
else if let InfixToken::LeftParen = token {
stack.push(token);
}
else if let InfixToken::RightParen = token {
while true {
let popout_token = stack.pop().unwrap();
if let InfixToken::LeftParen = popout_token {
break;
}
else {
if let InfixToken::Operator(operator) = popout_token {
output_vec.push(PostfixToken::Operator(*operator));
}
}
}
}
else if let InfixToken::Operator(operator) = token {
if stack.len() == 0 {
stack.push(token);
}
else {
while true {
if stack.len() >= 1 {
let top_token = stack[stack.len() - 1];
if let InfixToken::Operator(stack_operator) = top_token {
if *stack_operator == Operator::Add || *stack_operator == Operator::Sub {
if *operator == Operator::Sub || *operator == Operator::Add {
if let InfixToken::Operator(operator4out) = stack.pop().unwrap() {
output_vec.push(PostfixToken::Operator(*operator4out));
} else { break }
} else { break }
} else if *stack_operator == Operator::Mul || *stack_operator == Operator::Div {
if let InfixToken::Operator(operator4out) = stack.pop().unwrap() {
output_vec.push(PostfixToken::Operator(*operator4out));
} else { break }
}
}
else {
break;
}
}
else {
break;
}
}
stack.push(token);
}
}
}
for x in 0..stack.len() {
if let InfixToken::Operator(operator) = stack.pop().unwrap() {
output_vec.push(PostfixToken::Operator(*operator));
}
}
return Some(output_vec);
}
else {
return None;
}
}
pub fn check_valid(tokens: &[InfixToken]) -> bool {
if tokens.len() >= 1 {
if let InfixToken::Operator(_) = tokens[tokens.len() - 1] {
return false;
}
if let InfixToken::LeftParen = tokens[tokens.len() - 1] {
return false;
}
if let InfixToken::RightParen = tokens[0] {
return false;
}
if let InfixToken::Operator(_) = tokens[0] {
return false;
}
let mut index = 0;
for eachEle in tokens {
if index == 0 {
index += 1;
continue;
}
else {
let previous = &tokens[index - 1];
if let InfixToken::Operand(_) = eachEle {
if let InfixToken::Operand(_) = previous {
return false;
}
if let InfixToken::RightParen = previous {
return false;
}
}
if let InfixToken::LeftParen = eachEle {
if let InfixToken::Operand(_) = previous {
return false;
}
if let InfixToken::RightParen = previous {
return false;
}
}
if let InfixToken::Operator(_) = eachEle {
if let InfixToken::LeftParen = previous {
return false;
}
if let InfixToken::Operator(_) = previous {
return false;
}
}
if let InfixToken::RightParen = eachEle {
if let InfixToken::LeftParen = previous {
return false;
}
if let InfixToken::Operator(_) = previous {
return false;
}
}
}
index += 1;
}
let mut num_paren = 0;
for eachEle in tokens {
if let InfixToken::LeftParen = eachEle {
num_paren += 1;
}
if let InfixToken::RightParen = eachEle {
num_paren -= 1;
}
}
if num_paren != 0 {
return false;
}
return true;
}
else {
return false;
}
}