Question & Answer: a [postfix](https://en.wikipedia.org/wiki/Reverse_Polish_nota…..

Write a program using the language Rust.

## Description
Write a [postfix](https://en.wikipedia.org/wiki/Reverse_Polish_notation) expression evaluator. An expression consists of operands and operators. An operand is a signed integer (`isize`). An operator is `+`, `-`, or `*` with their common semantics. An expression is valid if it can be evaluated to a signed integer.

For example, the following are valid expressions:

```
-100
1 2 +
1 2 3 + *
```

The following expressions are invalid:

```
// empty expression
-1 -2
1 2 + *
```

## Public API

Your program must provide the following public API.

```
pub enum Operator {
    // `+`
    Add, 
    // `-`
    Sub,
    // `*`
    Mul,
}

pub enum Token {
    Operator(Operator),
    Operand(isize),
}

/// Evaluates the postix expression.
///
/// Input: a postfix expression, where each element contains an operator or operand.
/// Returns: if the postfix expression is valid, returns `Some(value)`;
///     otherwise, returns `None`.
pub fn eval(tokens: &[Token]) -> Option<isize> {
    // TODO
    unimplemented!();
}

Expert Answer

 

Question & Answer: a [postfix](https://en.wikipedia.org/wiki/Reverse_Polish_nota..... 1Question & Answer: a [postfix](https://en.wikipedia.org/wiki/Reverse_Polish_nota..... 2Question & Answer: a [postfix](https://en.wikipedia.org/wiki/Reverse_Polish_nota..... 3

Editable code:

fn main() {

 

let data1 = Token::Operand(3);

let data2 = Token::Operand(2);

let sign1 = Operator::Add;

let data3 = Token::Operator(sign1);

eval(&[data1, data2, data3]);

}

pub enum Operator {

 

Add,

 

Sub,

 

Mul,

}

pub enum Token {

Operator(Operator),

Operand(isize),

}

pub fn eval(tokens11: &[Token]) -> Option<isize> {

let mut v1: Vec<isize> = Vec::new();

for i1 in tokens11 {

match i1 {

&Token::Operator(ref op1) => {

if v1.len()>1 {

let x1 = v1.pop().unwrap();

let y1 = v1.pop().unwrap();

match op1 {

&Operator::Add => v1.push(x1+y1),

&Operator::Sub => v1.push(y1-x1),

&Operator::Mul => v1.push(x1*y1),

};

} else {

return None;

}

},

&Token::Operand(num) => v1.push(num),

};

}

if v1.len()==1 {

return v1.pop()

} else {

return None

}

}

Still stressed from student homework?
Get quality assistance from academic writers!