bracket - support for parentheses calculation #3
|
@ -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::<f32>().ok().expect("number is malformed");
|
||||
fn get(&mut self) -> f64 {
|
||||
println!("{}", self.s);
|
||||
let n = self.s.parse::<f64>().ok().expect("number is malformed");
|
||||
self.s = String::new();
|
||||
|
||||
n
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(pattern: String) -> (Vec<Symbol>, Vec<f32>) {
|
||||
fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
|
||||
let mut symbols = Vec::new();
|
||||
let mut numbers = Vec::new();
|
||||
|
||||
|
@ -52,13 +67,20 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f32>) {
|
|||
'*' => 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<Symbol>, Vec<f32>) {
|
|||
(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 n1 = *numbers.get(0).expect("expect at least the first value");
|
||||
while counter < symbols.len() {
|
||||
|
|
Loading…
Reference in New Issue