첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ 역 폴란드 표기법(RPN ; Reverse Polish Notation)의 수식을 계산하는 방법을 보여준다.

 

▶ 예제 코드 (RS)

use std::io;
use std::str;

fn main()
{
    let mut vector : Vec<f64> = vec![];

    println!("역 폴란드 표기법으로 수식을 입력해 주시기 바랍니다 : ");

    let mut input_string : String = String::new();

    io::stdin().read_line(&mut input_string).expect("입력 문자열에서 에러가 발생했습니다.");

    let token_split_whitespace : str::SplitWhitespace = input_string.split_whitespace();

    for token in token_split_whitespace
    {
        let token_trimmed : &str = token.trim();

        match token_trimmed.parse::<f64>()
        {
            Ok(value) =>
            {
                vector.push(value);

                continue;
            },
            Err(_) => 0.0,
        };

        let value2 : f64 = vector.pop().unwrap();
        let value1 : f64 = vector.pop().unwrap();

        match token_trimmed
        {
            "+" => vector.push(value1 + value2),
            "-" => vector.push(value1 - value2),
            "*" => vector.push(value1 * value2),
            "/" => vector.push(value1 / value2),
            _   => panic!("계산 불가 연산자 : {}", token_trimmed)
        }
    }

    println!("{}", vector.pop().unwrap());
}
728x90
그리드형(광고전용)
Posted by icodebroker
,