diff --git a/README.md b/README.md index d78e92a..22838cf 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Zielgruppe sind alle, die Mathematik besser verstehen wollen – von Schülern b - Klammern - Negative Zahlen (auch trickreich umgesetzt durch Voranstellen von 0) - Brüche (optional, aktuell in Entwicklung) - - Rechenoperatoren `+`, `-`, `*`, `:`, `/` + - Rechenoperatoren `+`, `-`, `*`, `:`, `/`, `^` --- diff --git a/src/helping_tools/string_manipulation.rs b/src/helping_tools/string_manipulation.rs index ae15782..8062315 100644 --- a/src/helping_tools/string_manipulation.rs +++ b/src/helping_tools/string_manipulation.rs @@ -9,8 +9,10 @@ pub fn strings_refactor(crazy_string: String) -> Vec { validation_brackets_operators(&mut crazy_string_refactor); terms_replace_operators(&mut crazy_string_refactor); - let terms_splitted: Vec = split_terms(crazy_string_refactor); + let mut terms_splitted: Vec = split_terms(crazy_string_refactor); + set_operator_mul(&mut terms_splitted); let negative_numbers: Vec = validate_negative_numbers(terms_splitted); + return negative_numbers } @@ -19,9 +21,7 @@ pub fn strings_refactor(crazy_string: String) -> Vec { // Damit sind auch Strings in der Form " 3 + 5 * 3" möglich fn remove_whitespaces(with_whitespaces: &mut String) { let result_string: String = with_whitespaces.chars().filter(|c| !c.is_whitespace()).collect(); - display_terminals("Leerzeichen entfernt".to_string(), &result_string); - *with_whitespaces = result_string; } @@ -68,8 +68,8 @@ fn terms_replace_operators(splitted_equation: &mut String) { for terms in splitted_equation.chars() { match terms { - '(' => terms_replaced.push_str("( "), - ')' => terms_replaced.push_str(" )"), + '(' => terms_replaced.push_str(" ( "), + ')' => terms_replaced.push_str(" ) "), '+' => terms_replaced.push_str( " + "), '-' => terms_replaced.push_str(" - "), '*' => terms_replaced.push_str(" * "), @@ -86,7 +86,7 @@ fn terms_replace_operators(splitted_equation: &mut String) { // Hier werden die einzelnen Terme nochmals gesplitted, damit man besser // mit Ihnen rechnen kann. fn split_terms(splitting_terms: String) -> Vec { - let mut splitted_terms: Vec = splitting_terms.split(' ').map(str::to_string).collect(); + let mut splitted_terms: Vec = splitting_terms.split_whitespace().map(str::to_string).collect(); if splitted_terms[0] == "" { splitted_terms.remove(0); } @@ -94,6 +94,30 @@ fn split_terms(splitting_terms: String) -> Vec { splitted_terms } +fn set_operator_mul(set_operator: &mut Vec) { + let mut index: usize = 1; + + while index < set_operator.len() - 1 { + if index > 0 && set_operator[index] == "(" && is_number(&set_operator[index - 1].to_string()) { + set_operator.insert(index, "*".to_string()); + index += 1; + } + else if index + 1 < set_operator.len() && set_operator[index] == ")" && is_number(&set_operator[index + 1].to_string()) { + set_operator.insert(index + 1, "*".to_string()); + index += 1; + } + else if index > 0 && set_operator[index] == "(" && set_operator[index - 1] == ")"{ + set_operator.insert(index, "*".to_string()); + index += 1; + } + index += 1; + } +} + +fn is_number(token: &str) -> bool { + token.parse::().is_ok() +} + // Wenn die Formel mit einem Minus anfängt, wird hier vorne eine 0 rangehängt, // damit die Berechnung sauber durchgeführt wird. Da die Berechnungen bei allen auf // dem Prinzip "Links-Operator-Rechts" ausgeführt wird. Auch wird hier geparst wenn diff --git a/src/main.rs b/src/main.rs index 4977546..62d7d91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![allow(dead_code)] use menue::mathtool_menue_terminal; +use crate::arithmetic::basic_arithmetic_ops::calculation_rules::rules_for_calculation; mod paths; @@ -14,7 +15,11 @@ fn main() { // let equation_string: String = "100+2*2-2^5*8/9-12+5/2*8+12".to_string(); //let equation_string: String = "(3+2)^2 * (4 ^ (-3)) - 5 * (10 / (2 + 3)) + 8 ^ 2".to_string(); //let equation_string: String = "-(5-5)".to_string(); - //let equation_string: String = "-4 + 5/6 * (3/2 + 6 / 4) + 6 : (3/9 * 3/4 + 1)".to_string(); + let equation_string: String = "4(6+5*10(5(2^2))123+6(5-4))".to_string(); - mathtool_menue_terminal(); + //mathtool_menue_terminal(); + + let splitted_terms: Vec = paths::str_manipulation::strings_refactor(equation_string); + + println!("Ergebnis: {:?}", rules_for_calculation(splitted_terms)); } \ No newline at end of file