Compare commits
No commits in common. "a033d3b647db810564fb7b2addf2e5716c588085" and "327917e29a2003200550ff6795c3a3d74916753d" have entirely different histories.
a033d3b647
...
327917e29a
|
@ -3,7 +3,6 @@ use std::{
|
||||||
process,
|
process,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Provide a conformable way to work with symbols
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
enum Symbol {
|
enum Symbol {
|
||||||
Minus,
|
Minus,
|
||||||
|
@ -15,7 +14,6 @@ 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,
|
||||||
|
@ -24,7 +22,6 @@ 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,
|
||||||
|
@ -33,7 +30,6 @@ 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,
|
||||||
|
@ -41,7 +37,6 @@ 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,
|
||||||
|
@ -51,50 +46,40 @@ 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),
|
||||||
|
@ -104,18 +89,12 @@ 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),
|
||||||
|
@ -123,68 +102,33 @@ fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
match s2 {
|
match s2 {
|
||||||
// If brackets
|
|
||||||
Some(s2) => {
|
Some(s2) => {
|
||||||
// If open add the increment
|
if s2.bracket() {
|
||||||
if s2.open_bracket() {
|
if s2.open_bracket() {
|
||||||
nb_bracket += 1;
|
nb_bracket += 1;
|
||||||
// If close decrease the counter and check if it gets to zero
|
|
||||||
} else if s2.close_bracket() {
|
} else if s2.close_bracket() {
|
||||||
nb_bracket -= 1;
|
nb_bracket -= 1;
|
||||||
if nb_bracket == 0 {
|
if nb_bracket == 0 {
|
||||||
// Save the content of the brackets
|
|
||||||
let new_pattern = pattern
|
let new_pattern = pattern
|
||||||
.get(i..i2)
|
.get(i..i2)
|
||||||
.expect("expect the sub string")
|
.expect("expect the sub string")
|
||||||
.to_string();
|
.to_string();
|
||||||
// Send it to the parse function
|
|
||||||
let (tmp_symbols, tmp_numbers) = parse(new_pattern);
|
let (tmp_symbols, tmp_numbers) = parse(new_pattern);
|
||||||
// Do the calculation
|
let tmp_result =
|
||||||
let tmp_result = calculation(&tmp_symbols, &tmp_numbers);
|
calculation(&tmp_symbols, &tmp_numbers);
|
||||||
// Add the calculated value to the list of numbers
|
|
||||||
numbers.push(tmp_result);
|
numbers.push(tmp_result);
|
||||||
// Change the position in the list
|
|
||||||
i = i2;
|
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 => {}
|
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());
|
||||||
|
@ -192,30 +136,23 @@ 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)
|
||||||
|
@ -224,7 +161,6 @@ 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 => {
|
||||||
|
@ -236,11 +172,9 @@ 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
|
||||||
|
@ -261,15 +195,12 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,7 +208,6 @@ 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);
|
||||||
|
@ -310,12 +240,7 @@ fn main() {
|
||||||
// println!("{:?}", numbers);
|
// println!("{:?}", numbers);
|
||||||
|
|
||||||
if symbols.len() < 1 || symbols.len() + 1 != numbers.len() {
|
if symbols.len() < 1 || symbols.len() + 1 != numbers.len() {
|
||||||
println!(
|
println!("The input is malformed");
|
||||||
"The input is malformed {} {} {}",
|
|
||||||
symbols.len() < 1,
|
|
||||||
symbols.len() + 1 != numbers.len(),
|
|
||||||
symbols.len()
|
|
||||||
);
|
|
||||||
process::exit(2);
|
process::exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue