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