bracket - support for parentheses calculation #3
|
@ -1,4 +1,7 @@
|
|||
use std::process;
|
||||
use std::{
|
||||
ops::{AddAssign, DivAssign, MulAssign, SubAssign},
|
||||
process,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Symbol {
|
||||
|
@ -7,6 +10,16 @@ enum Symbol {
|
|||
Multiply,
|
||||
Divide,
|
||||
}
|
||||
impl Symbol {
|
||||
fn priority(&self) -> bool {
|
||||
match self {
|
||||
Symbol::Multiply => true,
|
||||
Symbol::Divide => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TmpNum {
|
||||
s: String,
|
||||
|
@ -58,6 +71,81 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f32>) {
|
|||
(symbols, numbers)
|
||||
}
|
||||
|
||||
fn calcul(symbols: Vec<Symbol>, numbers: Vec<f32>) -> f32 {
|
||||
let mut counter = 0;
|
||||
let mut n1 = *numbers.get(0).expect("expect at least the first value");
|
||||
while counter < symbols.len() {
|
||||
let s = symbols
|
||||
.get(counter)
|
||||
.expect("expect at least a symbol value");
|
||||
let n2 = *numbers
|
||||
.get(counter + 1)
|
||||
.expect("expect at least a second value");
|
||||
|
||||
if s.priority() {
|
||||
match s {
|
||||
Symbol::Multiply => {
|
||||
n1.mul_assign(n2);
|
||||
}
|
||||
Symbol::Divide => {
|
||||
n1.div_assign(n2);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
} else {
|
||||
let next_s = symbols.get(counter + 1);
|
||||
match next_s {
|
||||
Some(ss) => {
|
||||
if ss.priority() {
|
||||
let mut counter_2 = counter + 1;
|
||||
let mut nn1 = *numbers
|
||||
.get(counter_2)
|
||||
.expect("during the priority, we expect at least the first value");
|
||||
while counter_2 < symbols.len() {
|
||||
let nn2 = *numbers
|
||||
.get(counter_2)
|
||||
.expect("during the priority, we expect at least the second value");
|
||||
|
||||
if ss.priority() {
|
||||
match ss {
|
||||
Symbol::Multiply => {
|
||||
nn1.mul_assign(nn2);
|
||||
}
|
||||
Symbol::Divide => {
|
||||
nn1.div_assign(nn2);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
counter = counter_2;
|
||||
n1 = nn1;
|
||||
break;
|
||||
}
|
||||
|
||||
counter_2 += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
match s {
|
||||
Symbol::Minus => {
|
||||
n1.sub_assign(n2);
|
||||
}
|
||||
Symbol::Plus => {
|
||||
n1.add_assign(n2);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
counter += 1;
|
||||
}
|
||||
|
||||
n1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let pattern = match std::env::args().nth(1) {
|
||||
None => {
|
||||
|
@ -69,13 +157,14 @@ fn main() {
|
|||
|
||||
let (symbols, numbers) = parse(pattern);
|
||||
|
||||
println!("{:?}", symbols);
|
||||
println!("{:?}", numbers);
|
||||
// println!("{:?}", symbols);
|
||||
// println!("{:?}", numbers);
|
||||
|
||||
let mut counter = 0;
|
||||
let mut n1 = numbers.get(0).expect("expect at least the first value");
|
||||
while counter < symbols.len() {
|
||||
let mut n2 = numbers.get(counter+1).expect("expect at least a second value");
|
||||
counter +=1;
|
||||
if symbols.len() < 1 || symbols.len() + 1 != numbers.len() {
|
||||
println!("The input is malformed");
|
||||
process::exit(2);
|
||||
}
|
||||
|
||||
let res = calcul(symbols, numbers);
|
||||
println!("{res}");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue