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
pub fn priority(ch: &char) -> u8 {
if *ch == '^' {
return 3;
} else if *ch == '*' || *ch == '/' {
return 2;
} else if *ch == '+' || *ch == '-' {
return 1;
} else {
return 0;
}
}
pub fn convert(expr: &str) -> Result<String, &'static str> {
let mut is_bracket_closed:bool = true;
let mut stack: Vec<char> = Vec::new();
let mut postfix_expr = String::new();
let mut iter = expr.chars();
while let Some(mut ch) = iter.next() {
let mut c = ch;
while c.is_ascii_digit() || c == ' ' || c == '.' {
if !c.is_whitespace(){
postfix_expr.push(c);
}
c = match iter.next(){
Some(v) => v,
None => break
};
}
ch = c;
postfix_expr.push(' ');
if ch.is_ascii_whitespace() {
continue;
} else if ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^' {
if stack.is_empty() {
stack.push(ch);
} else if !stack.is_empty() && priority(&ch) > priority(&stack.last().unwrap()) {
stack.push(ch);
} else {
while !stack.is_empty() && priority(&ch) <= priority(&stack.last().unwrap()) {
postfix_expr.push(stack.pop().unwrap());
postfix_expr.push(' ');
}
stack.push(ch);
}
} else {
if ch == '(' {
stack.push(ch);
is_bracket_closed = false;
} else if ch == ')' {
while match stack.last(){
Some(v)=> v,
None => {
return Err("Syntax error : open bracket missing");
}
} != &'(' {
postfix_expr.push(stack.pop().unwrap());
postfix_expr.push(' ');
}
stack.pop();
is_bracket_closed = true;
}
else{
break;
}
}
}
while !stack.is_empty() {
postfix_expr.push(stack.pop().unwrap());
postfix_expr.push(' ');
}
postfix_expr = postfix_expr.trim().to_string();
if is_bracket_closed && !postfix_expr.is_empty(){
Ok(postfix_expr)
}else if !is_bracket_closed{
Err("Syntax error : close bracket missing")
}else{
Err("Syntax error : wrong input provided")
}
}
pub fn solve(postfix_expr: &str) -> Result<f64,&'static str> {
let mut stack: Vec<f64> = Vec::new();
for ch in postfix_expr.split_whitespace() {
if ch.chars().all(|x| x.is_ascii_digit() || x == '.') {
stack.push(match ch.trim().parse(){
Ok(v) => v,
Err(_) => {
return Err("Parse error : invalid number");
}
});
} else {
let num2 = match stack.pop(){
Some(v) => v,
None => {
return Err("Syntax error : wrong value entered");
}
};
let num1 = match stack.pop(){
Some(v) => v,
None => {
return Err("Syntax error : wrong value entered");
}
};
match ch.chars().next().unwrap() {
'+' => stack.push(num1 + num2),
'-' => stack.push(num1 - num2),
'*' => stack.push(num1 * num2),
'/' => {
if num2 == 0.0{
return Err("Divide by zero error");
}
stack.push(num1 / num2)
},
'^' => stack.push(f64::powf(num1, num2)),
_ => {}
}
}
}
Ok(stack.pop().unwrap())
}
#[cfg(test)]
mod tests {
use super::{priority,convert,solve};
#[test]
fn test_priority() {
assert_eq!(3,priority(&'^'));
assert_eq!(2,priority(&'*'));
assert_eq!(2,priority(&'/'));
assert_eq!(1,priority(&'-'));
assert_eq!(1,priority(&'+'));
assert_eq!(0,priority(&'!'));
}
#[test]
fn test_convert(){
assert_eq!("1 2 +",convert("1 + 2").unwrap());
assert_eq!("2 3 4 * +",convert("2 + 3 * 4").unwrap());
assert_eq!("0.156 2 1 2 / ^ * 14 3 5 * * 2 / +",convert("0.156 * 2 ^ (1 / 2) + (14 * ( 3* 5))/2").unwrap());
}
#[test]
fn test_solve(){
assert_eq!(3.00,solve("1 2 +").unwrap());
assert_eq!(14.00,solve("2 3 4 * +").unwrap());
assert_eq!(105.22061731573021,solve("0.156 2 1 2 / ^ * 14 3 5 * * 2 / +").unwrap());
}
}