From 8c424fbdf4baf3e27f63b2526def89ddf4ac1b83 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Fri, 29 May 2020 14:56:25 +0530 Subject: [PATCH] Fixed the regular expression - also made the command line robust Made the command line more robust and fixed the regular expression to make literal dot using \. instead of . (inadvertent error) --- src/main.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index e25413b..c911ca2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::io; +use std::io::Write; use regex::Regex; enum Operator { @@ -117,26 +118,39 @@ fn main() { // 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"); } } } -- 2.20.1