Created
July 18, 2017 20:35
-
-
Save nothingnesses/6ad0e571d9ff52d4a27396b07eb35b27 to your computer and use it in GitHub Desktop.
Simple RPN calculator in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
fn main() { | |
let mut v: Vec<f64> = Vec::new(); | |
loop { | |
let mut input = String::new(); | |
io::stdin().read_line(&mut input) | |
.expect("Failed to read line"); | |
match input.trim() { | |
"+" => { | |
let (y,x) = (v.pop().unwrap(),v.pop().unwrap()); | |
v.push(x+y); | |
println!("{}", x+y); | |
}, | |
"-" => { | |
let (y,x) = (v.pop().unwrap(),v.pop().unwrap()); | |
v.push(x*y); | |
println!("{}", x*y); | |
}, | |
"*" => { | |
let (y,x) = (v.pop().unwrap(),v.pop().unwrap()); | |
v.push(x*y); | |
println!("{}", x*y); | |
}, | |
"/" => { | |
let (y,x) = (v.pop().unwrap(),v.pop().unwrap()); | |
v.push(x/y); | |
println!("{}", x/y); | |
}, | |
"^" => { | |
let (y,x) = (v.pop().unwrap(),v.pop().unwrap()); | |
v.push(x.powf(y)); | |
println!("{}", x.powf(y)); | |
}, | |
&_ => { | |
v.push(input.trim().parse().unwrap()); | |
}, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment