-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP1935.rs
40 lines (39 loc) · 1.23 KB
/
P1935.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
use std::io::{stdin, BufRead};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let stdin = stdin();
let handle = stdin.lock();
let mut input = handle.lines().flatten().skip(1);
let expr = input.next().unwrap();
let numbers: Vec<u32> = input.flat_map(|line| line.parse()).collect();
let mut stack = Vec::<f64>::new();
for letter in expr.chars() {
match letter {
'+' => {
let a = stack.pop().unwrap();
let b = stack.pop().unwrap();
stack.push(b + a);
}
'-' => {
let a = stack.pop().unwrap();
let b = stack.pop().unwrap();
stack.push(b - a);
}
'*' => {
let a = stack.pop().unwrap();
let b = stack.pop().unwrap();
stack.push(b * a);
}
'/' => {
let a = stack.pop().unwrap();
let b = stack.pop().unwrap();
stack.push(b / a);
}
_ => {
let index = letter as u8 - b'A';
stack.push(numbers[index as usize] as f64);
}
}
}
println!("{:.2}", stack[0]);
Ok(())
}