Skip to content

Instantly share code, notes, and snippets.

@nothingnesses
Created July 18, 2017 20:35
Show Gist options
  • Select an option

  • Save nothingnesses/6ad0e571d9ff52d4a27396b07eb35b27 to your computer and use it in GitHub Desktop.

Select an option

Save nothingnesses/6ad0e571d9ff52d4a27396b07eb35b27 to your computer and use it in GitHub Desktop.

Revisions

  1. VoidNoire created this gist Jul 18, 2017.
    43 changes: 43 additions & 0 deletions rpn_calc.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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());
    },
    }
    }
    }