From fc31bfc38f5729840d54fb29708ff7127bdbea5b Mon Sep 17 00:00:00 2001 From: Alexandre Stein Date: Sun, 13 Nov 2022 16:25:24 +0100 Subject: [PATCH] bracket WIP but fixed issue with signed value --- rust-calc/src/main.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/rust-calc/src/main.rs b/rust-calc/src/main.rs index fd0ab86..fa31432 100644 --- a/rust-calc/src/main.rs +++ b/rust-calc/src/main.rs @@ -9,6 +9,9 @@ enum Symbol { Plus, Multiply, Divide, + + OpenBracket, + CloseBracket, } impl Symbol { fn priority(&self) -> bool { @@ -16,6 +19,17 @@ impl Symbol { Symbol::Multiply => true, Symbol::Divide => true, + Symbol::OpenBracket => true, + Symbol::CloseBracket => true, + + _ => false, + } + } + fn bracket(&self) -> bool { + match self { + Symbol::OpenBracket => true, + Symbol::CloseBracket => true, + _ => false, } } @@ -31,15 +45,16 @@ impl TmpNum { fn push(&mut self, c: char) { self.s.push(c); } - fn get(&mut self) -> f32 { - let n = self.s.parse::().ok().expect("number is malformed"); + fn get(&mut self) -> f64 { + println!("{}", self.s); + let n = self.s.parse::().ok().expect("number is malformed"); self.s = String::new(); n } } -fn parse(pattern: String) -> (Vec, Vec) { +fn parse(pattern: String) -> (Vec, Vec) { let mut symbols = Vec::new(); let mut numbers = Vec::new(); @@ -52,13 +67,20 @@ fn parse(pattern: String) -> (Vec, Vec) { '*' => Some(Symbol::Multiply), '/' => Some(Symbol::Divide), + '(' => Some(Symbol::OpenBracket), + ')' => Some(Symbol::CloseBracket), + _ => None, }; match s { Some(s) => { - symbols.push(s); - numbers.push(number.get()); + if number.s.len() == 0 { + number.push(c); + } else { + symbols.push(s); + numbers.push(number.get()); + } } None => { number.push(c); @@ -71,7 +93,7 @@ fn parse(pattern: String) -> (Vec, Vec) { (symbols, numbers) } -fn calcul(symbols: Vec, numbers: Vec) -> f32 { +fn calcul(symbols: Vec, numbers: Vec) -> f64 { let mut counter = 0; let mut n1 = *numbers.get(0).expect("expect at least the first value"); while counter < symbols.len() {