use std::io;
+use std::io::Write;
use regex::Regex;
enum Operator {
// get a line from input and evaluate it
let mut expr = String::new ();
// regular expression to match a number
- let match_num = Regex::new (r"^\d+?.*?\d*?$").unwrap ();
+ let match_num = Regex::new (r"^\d+?\.*?\d*?$").unwrap ();
// loop until a blank line is received
loop {
expr.clear ();
- println!("Enter an expression (postfix) (blank to quit): ");
+ print!("evpf>");
+ io::stdout().flush ().expect ("Error flushing!");
// read a line of text
io::stdin().read_line (&mut expr).expect ("Error reading line!");
// trim the text
let expr = expr.trim ();
- // quit if the expression is blank
- if expr == "" {
+
+ if expr == "q" || expr == "Q" {
+ // quit if the expression is q or Qs
break;
+ } else if expr == "?" || expr == "h" || expr == "H" {
+ // display help text
+ println! ("Type an expression in postfix style to evaluate.");
+ println! ("Example: 4 5 + 12 -");
+ println! ("Supported operators: +, -, *, /");
+ println! ("Type q, Q to quit");
+ continue;
+ } else if expr == "" {
+ // continue without proceeding
+ continue;
}
let res = evaluate (&expr, &match_num);
if res.is_ok () {
- println! ("Result is : {}", res.unwrap());
+ println! ("Result: {}", res.unwrap());
}
else {
eprintln! ("Error: {}", res.unwrap_err());
+ eprintln! ("Type ? or h or H for help");
}
}
}