Skip to content

Instantly share code, notes, and snippets.

@nothingnesses
Created July 18, 2017 20:35
Show Gist options
  • Save nothingnesses/6ad0e571d9ff52d4a27396b07eb35b27 to your computer and use it in GitHub Desktop.
Save nothingnesses/6ad0e571d9ff52d4a27396b07eb35b27 to your computer and use it in GitHub Desktop.
Simple RPN calculator in Rust
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