From e377d9e94119364174717e0d551346346e92998a Mon Sep 17 00:00:00 2001 From: Supergoof1410 <161129122+Supergoof1410@users.noreply.github.com> Date: Mon, 19 May 2025 19:56:17 +0200 Subject: [PATCH 1/2] fixes --- README.md | 2 +- src/helping_tools/string_manipulation.rs | 43 ++++++++++++++++++++---- src/main.rs | 9 +++-- 3 files changed, 45 insertions(+), 9 deletions(-) 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..0fcb4d9 100644 --- a/src/helping_tools/string_manipulation.rs +++ b/src/helping_tools/string_manipulation.rs @@ -1,3 +1,5 @@ +use std::ops::Index; + use super::display_terminal::display_terminals; // Diese Funktion ist für andere sichtbar und macht die @@ -9,8 +11,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 +23,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; } @@ -61,6 +63,8 @@ fn validation_brackets_operators(brackets_ops: &mut String) { } } + + // Mit dieser Funktion werden nur Leerzeichen vor den Operatoren und die Klammern // gesetzt, um sie in der Funktion "split_terms" besser zu teilen. fn terms_replace_operators(splitted_equation: &mut String) { @@ -68,8 +72,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 +90,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 +98,33 @@ 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; + } + + // println!("Vektor davor: {:?}", set_operator); + // todo!("set_operator_mul"); +} + +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 From 1149e092e45b046acdf25ca152e72fb413b3910a Mon Sep 17 00:00:00 2001 From: supergoof1410 Date: Thu, 20 Nov 2025 18:44:19 +0100 Subject: [PATCH 2/2] nix --- src/helping_tools/string_manipulation.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/helping_tools/string_manipulation.rs b/src/helping_tools/string_manipulation.rs index 0fcb4d9..8062315 100644 --- a/src/helping_tools/string_manipulation.rs +++ b/src/helping_tools/string_manipulation.rs @@ -1,5 +1,3 @@ -use std::ops::Index; - use super::display_terminal::display_terminals; // Diese Funktion ist für andere sichtbar und macht die @@ -63,8 +61,6 @@ fn validation_brackets_operators(brackets_ops: &mut String) { } } - - // Mit dieser Funktion werden nur Leerzeichen vor den Operatoren und die Klammern // gesetzt, um sie in der Funktion "split_terms" besser zu teilen. fn terms_replace_operators(splitted_equation: &mut String) { @@ -116,9 +112,6 @@ fn set_operator_mul(set_operator: &mut Vec) { } index += 1; } - - // println!("Vektor davor: {:?}", set_operator); - // todo!("set_operator_mul"); } fn is_number(token: &str) -> bool {