Compare commits

...

2 Commits

Author SHA1 Message Date
Alexandre Stein a033d3b647 Add support for automatic bracket simbol in progress 2022-11-14 16:42:12 +01:00
Alexandre Stein fbe8e2c1c1 Add comment 2022-11-14 16:24:12 +01:00
1 changed files with 91 additions and 16 deletions

View File

@ -3,6 +3,7 @@ use std::{
process,
};
// Provide a conformable way to work with symbols
#[derive(Clone, Debug)]
enum Symbol {
Minus,
@ -14,6 +15,7 @@ enum Symbol {
CloseBracket,
}
impl Symbol {
// Checks if the given symbol has priority or not
fn priority(&self) -> bool {
match self {
Symbol::Multiply => true,
@ -22,6 +24,7 @@ impl Symbol {
_ => false,
}
}
// Check if it brackets (open or close)
fn bracket(&self) -> bool {
match self {
Symbol::OpenBracket => true,
@ -30,6 +33,7 @@ impl Symbol {
_ => false,
}
}
// Check if it's an open bracket
fn open_bracket(&self) -> bool {
match self {
Symbol::OpenBracket => true,
@ -37,6 +41,7 @@ impl Symbol {
_ => false,
}
}
// Check if it's an close bracket
fn close_bracket(&self) -> bool {
match self {
Symbol::CloseBracket => true,
@ -46,40 +51,50 @@ impl Symbol {
}
}
// Provides a object to hold the number being parsed from the string
struct TmpNum {
s: String,
}
impl TmpNum {
// Simple constructor
pub fn new() -> Self {
Self { s: String::new() }
}
// Add a new char to the list of chars used to describe the number in a base 10 fashion
fn push(&mut self, c: char) {
self.s.push(c);
}
// Returns the float number from the parsed input
fn get(&mut self) -> f64 {
println!("XXX {}", self.s);
let n = self.s.parse::<f64>().ok().expect("number is malformed");
self.s = String::new();
n
}
// Returns the length of the current save number as chars.
fn len(&self) -> usize {
self.s.len()
}
}
// Takes as argument a string representing the wanted calculation and return a list of symbols and values.
fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
let mut symbols = Vec::new();
let mut numbers = Vec::new();
let mut number = TmpNum::new();
// Iteration of the chars
let mut i = 0;
while i < pattern.len() {
let c = pattern.chars().nth(i).expect("some char");
// Check if the given char is a recognized symbol
let s: Option<Symbol> = match c {
'-' => Some(Symbol::Minus),
'+' => Some(Symbol::Plus),
'*' => Some(Symbol::Multiply),
'x' => Some(Symbol::Multiply), // This is the second way to express multiplication
'/' => Some(Symbol::Divide),
'(' => Some(Symbol::OpenBracket),
@ -89,12 +104,18 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
};
match s {
// If the char is a recognized symbol
Some(s) => {
// If the char is a bracket
if s.bracket() {
// Keep track of the number of brackets
let mut nb_bracket = 1;
// Keep track of the new position into the list
let mut i2 = i + 1;
// Loop the rest of the input
while i2 < pattern.len() {
let c2 = pattern.chars().nth(i2).expect("some char");
// Recognize the brackets from the rest of the input
let s2: Option<Symbol> = match c2 {
'(' => Some(Symbol::OpenBracket),
')' => Some(Symbol::CloseBracket),
@ -102,33 +123,68 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
_ => None,
};
match s2 {
// If brackets
Some(s2) => {
if s2.bracket() {
if s2.open_bracket() {
nb_bracket += 1;
} else if s2.close_bracket() {
nb_bracket -= 1;
if nb_bracket == 0 {
let new_pattern = pattern
.get(i..i2)
.expect("expect the sub string")
.to_string();
let (tmp_symbols, tmp_numbers) = parse(new_pattern);
let tmp_result =
calculation(&tmp_symbols, &tmp_numbers);
numbers.push(tmp_result);
i = i2;
// If open add the increment
if s2.open_bracket() {
nb_bracket += 1;
// If close decrease the counter and check if it gets to zero
} else if s2.close_bracket() {
nb_bracket -= 1;
if nb_bracket == 0 {
// Save the content of the brackets
let new_pattern = pattern
.get(i..i2)
.expect("expect the sub string")
.to_string();
// Send it to the parse function
let (tmp_symbols, tmp_numbers) = parse(new_pattern);
// Do the calculation
let tmp_result = calculation(&tmp_symbols, &tmp_numbers);
// Add the calculated value to the list of numbers
numbers.push(tmp_result);
// Change the position in the list
i = i2;
// Check the symbol after the closing bracket
let c3 = pattern.chars().nth(i2 + 1).expect("some char");
if !c3.is_numeric() {
let s2 = match c3 {
'-' => Some(Symbol::Minus),
'+' => Some(Symbol::Plus),
'*' => Some(Symbol::Multiply),
'/' => Some(Symbol::Divide),
_ => None,
};
match s2 {
Some(s2) => {
symbols.push(s2);
i += 1;
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// This will be used in case of bracket directly followed by number.
None => {
symbols.push(Symbol::Multiply);
}
}
}
}
}
}
// Not a bracket, do nothing
None => {}
}
// Add counter at the end of the loop
i2 += 1;
}
// If not a bracket but symbol
} else {
// If the length of number is zero, add to the current char to number
// It's in the case of signed numbers
if number.s.len() == 0 {
number.push(c);
// Otherwise add the number and the symbol
} else {
symbols.push(s);
numbers.push(number.get());
@ -136,23 +192,30 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
}
}
None => {
// It's not a recognized symbol so it must be part of a number
number.push(c);
}
}
// Increment the list position
i += 1;
}
// At the end the last number need to be added too.
if number.len() != 0 {
numbers.push(number.get());
}
// Return the values
(symbols, numbers)
}
// Run the calculation based on the 2 different lists of symbols and numbers
fn calculation(symbols: &Vec<Symbol>, numbers: &Vec<f64>) -> f64 {
let mut counter = 0;
let mut n1 = *numbers.get(0).expect("expect at least the first value");
// Iterate the list of symbols
while counter < symbols.len() {
let s = symbols
.get(counter)
@ -161,6 +224,7 @@ fn calculation(symbols: &Vec<Symbol>, numbers: &Vec<f64>) -> f64 {
.get(counter + 1)
.expect("expect at least a second value");
// If the symbol has priority then simply do the calculation and update n1
if s.priority() {
match s {
Symbol::Multiply => {
@ -172,9 +236,11 @@ fn calculation(symbols: &Vec<Symbol>, numbers: &Vec<f64>) -> f64 {
_ => {}
};
} else {
// Check the next symbol
let next_s = symbols.get(counter + 1);
match next_s {
Some(ss) => {
// If the next has priority start to check until where the priority calculation goes
if ss.priority() {
let mut counter_2 = counter + 1;
let mut nn1 = *numbers
@ -195,12 +261,15 @@ fn calculation(symbols: &Vec<Symbol>, numbers: &Vec<f64>) -> f64 {
}
_ => {}
}
// End of the priority.
// Change the position in the list, save the new value as n1 and stop the priority loop
} else {
counter = counter_2;
n1 = nn1;
break;
}
// Increment the position in the list
counter_2 += 1;
}
}
@ -208,6 +277,7 @@ fn calculation(symbols: &Vec<Symbol>, numbers: &Vec<f64>) -> f64 {
None => {}
}
// Run the non prioritized calculation
match s {
Symbol::Minus => {
n1.sub_assign(n2);
@ -240,7 +310,12 @@ fn main() {
// println!("{:?}", numbers);
if symbols.len() < 1 || symbols.len() + 1 != numbers.len() {
println!("The input is malformed");
println!(
"The input is malformed {} {} {}",
symbols.len() < 1,
symbols.len() + 1 != numbers.len(),
symbols.len()
);
process::exit(2);
}