fn in2rpn(variables: &mut HashMap<String, Numeric>, tokens: &Vec<Token>) -> Vec<Token> {
    let mut result: Vec<Token> = Vec::new();
    let mut funcs: Vec<Token> = Vec::new();
    let mut token_counter = 0;
    for item in tokens {
        token_counter += 1;
        match item.id {
            // push numeric value to result vector
            TokenType::IsNumeric => {
                result.push(item.clone());
            }
            // get value from hashmap and push to result
            TokenType::IsVariable => {
                if token_counter == 1 {
                    continue;
                }
                let token = match variables.get(&item.param) {
                    Some(value) => format!("{}", value.clone()),
                    None => {
                        println!("> error: can't found {} variable!", item.param);
                        break;
                    }
                };
                result.push(Token::new(token.clone(), TokenType::IsNumeric));
            }
            TokenType::IsFunction => {
                continue;
            }
            TokenType::IsOperation => {
                if item.param == ")" {
                    // push all acumulated functions in brackets
                    while funcs.len() > 0 && funcs.last().unwrap().param != "(" {
                        let function = match funcs.pop() {
                            Some(value) => value,
                            None => {
                                println!("> error: function stack is empty");
                                break;
                            }
                        };
                        result.push(function);
                    }
                    funcs.pop();
                } else if item.param == "=" {
                    continue;
                } else {
                    // push other functions
                    while can_pop(item, &funcs) {
                        let function = match funcs.pop() {
                            Some(value) => value,
                            None => {
                                println!("> error: function stack is empty");
                                break;
                            }
                        };
                        result.push(function);
                    }
                    funcs.push(item.clone());
                }
            }
        }
    }
    // push rest functions
    if funcs.len() > 0 {
        result.push(funcs.pop().unwrap());
    }
    result
}
// convert infix notation to reverse polish
convert infix notation to reverse polish
convert infix notation to reverse polish
fn in2rpn(variables: &mut HashMap<String, Numeric>, tokens: &Vec<Token>) -> Vec<Token> {
    let mut result: Vec<Token> = Vec::new();
    let mut funcs: Vec<Token> = Vec::new();
    let mut token_counter = 0;
    for item in tokens {
        token_counter += 1;
        match item.id {
            // push numeric value to result vector
            TokenType::IsNumeric => {
                result.push(item.clone());
            }
            // get value from hashmap and push to result
            TokenType::IsVariable => {
                if token_counter == 1 {
                    continue;
                }
                let token = match variables.get(&item.param) {
                    Some(value) => format!("{}", value.clone()),
                    None => {
                        println!("> error: can't found {} variable!", item.param);
                        break;
                    }
                };
                result.push(Token::new(token.clone(), TokenType::IsNumeric));
            }
            TokenType::IsFunction => {
                continue;
            }
            TokenType::IsOperation => {
                if item.param == ")" {
                    // push all acumulated functions in brackets
                    while funcs.len() > 0 && funcs.last().unwrap().param != "(" {
                        let function = match funcs.pop() {
                            Some(value) => value,
                            None => {
                                println!("> error: function stack is empty");
                                break;
                            }
                        };
                        result.push(function);
                    }
                    funcs.pop();
                } else if item.param == "=" {
                    continue;
                } else {
                    // push other functions
                    while can_pop(item, &funcs) {
                        let function = match funcs.pop() {
                            Some(value) => value,
                            None => {
                                println!("> error: function stack is empty");
                                break;
                            }
                        };
                        result.push(function);
                    }
                    funcs.push(item.clone());
                }
            }
        }
    }
    // push rest functions
    if funcs.len() > 0 {
        result.push(funcs.pop().unwrap());
    }
    result
}
fn in2rpn ( variables : & mut HashMap < String , Numeric > , tokens : & Vec < Token > ) -> Vec < Token > { let mut result : Vec < Token > = Vec :: new ( ) ; let mut funcs : Vec < Token > = Vec :: new ( ) ; let mut token_counter = 0 ; for item in tokens { token_counter += 1 ; match item . id { // push numeric value to result vector TokenType :: IsNumeric => { result . push ( item . clone ( ) ) ; } // get value from hashmap and push to result TokenType :: IsVariable => { if token_counter == 1 { continue ; } let token = match variables . get ( & item . param ) { Some ( value ) => format ! ( " " value clone ( ) ) , None => { println ! ( " " item param ) ; break ; } } ; result . push ( Token :: new ( token . clone ( ) , TokenType :: IsNumeric ) ) ; } TokenType :: IsFunction => { continue ; } TokenType :: IsOperation => { if item . param == " " { // push all acumulated functions in brackets while funcs . len ( ) > 0 && funcs . last ( ) . unwrap ( ) . param != " " { let function = match funcs . pop ( ) { Some ( value ) => value , None => { println ! ( " " ) ; break ; } } ; result . push ( function ) ; } funcs . pop ( ) ; } else if item . param == " " { continue ; } else { // push other functions while can_pop ( item , & funcs ) { let function = match funcs . pop ( ) { Some ( value ) => value , None => { println ! ( " " ) ; break ; } } ; result . push ( function ) ; } funcs . push ( item . clone ( ) ) ; } } } } // push rest functions if funcs . len ( ) > 0 { result . push ( funcs . pop ( ) . unwrap ( ) ) ; } result }
convert infix notation to reverse polish
convert infix notation to reverse polish
variables
&mut HashMap<String, Numeric>
tokens
&Vec<Token>
variables
&mut HashMap<String, Numeric>
tokens
&Vec<Token>