Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `+`, `-`, `*`, `:`, `/`, `^`

---

Expand Down
36 changes: 30 additions & 6 deletions src/helping_tools/string_manipulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ pub fn strings_refactor(crazy_string: String) -> Vec<String> {
validation_brackets_operators(&mut crazy_string_refactor);
terms_replace_operators(&mut crazy_string_refactor);

let terms_splitted: Vec<String> = split_terms(crazy_string_refactor);
let mut terms_splitted: Vec<String> = split_terms(crazy_string_refactor);
set_operator_mul(&mut terms_splitted);
let negative_numbers: Vec<String> = validate_negative_numbers(terms_splitted);


return negative_numbers
}
Expand All @@ -19,9 +21,7 @@ pub fn strings_refactor(crazy_string: String) -> Vec<String> {
// 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;
}

Expand Down Expand Up @@ -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(" * "),
Expand All @@ -86,14 +86,38 @@ 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<String> {
let mut splitted_terms: Vec<String> = splitting_terms.split(' ').map(str::to_string).collect();
let mut splitted_terms: Vec<String> = splitting_terms.split_whitespace().map(str::to_string).collect();
if splitted_terms[0] == "" {
splitted_terms.remove(0);
}
display_terminals("Terme einzeln aufgeteilt".to_string(), &splitted_terms.join(" "));
splitted_terms
}

fn set_operator_mul(set_operator: &mut Vec<String>) {
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::<f64>().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
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<String> = paths::str_manipulation::strings_refactor(equation_string);

println!("Ergebnis: {:?}", rules_for_calculation(splitted_terms));
}