bracket WIP but fixed issue with signed value

This commit is contained in:
Alexandre Stein 2022-11-13 16:25:24 +01:00
parent 8d36c71340
commit fc31bfc38f
1 changed files with 28 additions and 6 deletions

View File

@ -9,6 +9,9 @@ enum Symbol {
Plus, Plus,
Multiply, Multiply,
Divide, Divide,
OpenBracket,
CloseBracket,
} }
impl Symbol { impl Symbol {
fn priority(&self) -> bool { fn priority(&self) -> bool {
@ -16,6 +19,17 @@ impl Symbol {
Symbol::Multiply => true, Symbol::Multiply => true,
Symbol::Divide => true, Symbol::Divide => true,
Symbol::OpenBracket => true,
Symbol::CloseBracket => true,
_ => false,
}
}
fn bracket(&self) -> bool {
match self {
Symbol::OpenBracket => true,
Symbol::CloseBracket => true,
_ => false, _ => false,
} }
} }
@ -31,15 +45,16 @@ impl TmpNum {
fn push(&mut self, c: char) { fn push(&mut self, c: char) {
self.s.push(c); self.s.push(c);
} }
fn get(&mut self) -> f32 { fn get(&mut self) -> f64 {
let n = self.s.parse::<f32>().ok().expect("number is malformed"); println!("{}", self.s);
let n = self.s.parse::<f64>().ok().expect("number is malformed");
self.s = String::new(); self.s = String::new();
n n
} }
} }
fn parse(pattern: String) -> (Vec<Symbol>, Vec<f32>) { fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
let mut symbols = Vec::new(); let mut symbols = Vec::new();
let mut numbers = Vec::new(); let mut numbers = Vec::new();
@ -52,13 +67,20 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f32>) {
'*' => Some(Symbol::Multiply), '*' => Some(Symbol::Multiply),
'/' => Some(Symbol::Divide), '/' => Some(Symbol::Divide),
'(' => Some(Symbol::OpenBracket),
')' => Some(Symbol::CloseBracket),
_ => None, _ => None,
}; };
match s { match s {
Some(s) => { Some(s) => {
symbols.push(s); if number.s.len() == 0 {
numbers.push(number.get()); number.push(c);
} else {
symbols.push(s);
numbers.push(number.get());
}
} }
None => { None => {
number.push(c); number.push(c);
@ -71,7 +93,7 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f32>) {
(symbols, numbers) (symbols, numbers)
} }
fn calcul(symbols: Vec<Symbol>, numbers: Vec<f32>) -> f32 { fn calcul(symbols: Vec<Symbol>, numbers: Vec<f64>) -> f64 {
let mut counter = 0; let mut counter = 0;
let mut n1 = *numbers.get(0).expect("expect at least the first value"); let mut n1 = *numbers.get(0).expect("expect at least the first value");
while counter < symbols.len() { while counter < symbols.len() {