diff --git a/src/arithmetic/basic_arithmetic_ops/calculating_fractions.rs b/src/arithmetic/basic_arithmetic_ops/calculating_fractions.rs index 3d8ec1c..c389740 100644 --- a/src/arithmetic/basic_arithmetic_ops/calculating_fractions.rs +++ b/src/arithmetic/basic_arithmetic_ops/calculating_fractions.rs @@ -1,71 +1,71 @@ -use crate::arithmetic::basic_arithmetic_ops::index_operators::find_index; - - -// Diese Funktionen sind erstmal auf Eis gesetzt, weil es in meiner -// derzeitigen Entwicklung keinen richtigen Sinn macht, wenn ich zur -// Formelumstellung oder Formeln komme werde ich diese Funktionen als -// eigenes Modul für die Bruchrechnung verwenden und weiter ausbauen. - -pub fn calculate_fraction(fractions: Vec) -> Vec { - let mut result_fraction: Vec = fractions; - let mut index_brackets: Vec<(usize, usize)> = Vec::new(); - let mut fractions_term: Vec = vec![]; - - println!("Vektor davor: {:?}", result_fraction); - parse_fractions(&mut result_fraction, &mut fractions_term); - println!("Vektor danach: {:?}", result_fraction); - - index_brackets = find_index(&result_fraction); - println!("Index der Klammern: {:?}", index_brackets); - - calculate_rules_fraction(&mut result_fraction, &mut fractions_term, &mut index_brackets); - - //todo!("Noch im Bau! (calculate_fraction)"); - return Vec::new() -} - -struct Bruch { - numerator: i32, - denominator: i32, -} - -// hier werden alle Brüche in einem Vektor gespeichert -fn parse_fractions(fractions: &mut Vec, fractions_term: &mut Vec) { - let mut index: usize = 0; - - while index < fractions.len() { - if fractions[index].contains("/") { - if fractions[index + 1] == "0" { - panic!("Division by Zero!") - } - - let vec_fractions = Bruch { - numerator: fractions[index - 1].parse::().expect("invalid numerator"), - denominator: fractions[index + 1].parse::().expect("invalid numerator"), - }; - - fractions_term.push(vec_fractions); - - // Hier werden die Brüche durch einen Marker ersetzt. - let index_bruch: String = "bruch_".to_string() + &((fractions_term.len()) - 1).to_string(); - fractions.remove(index); - fractions.insert(index, index_bruch); - fractions.remove(index + 1); - fractions.remove(index - 1); +// use crate::arithmetic::basic_arithmetic_ops::index_operators::find_index; + + +// // Diese Funktionen sind erstmal auf Eis gesetzt, weil es in meiner +// // derzeitigen Entwicklung keinen richtigen Sinn macht, wenn ich zur +// // Formelumstellung oder Formeln komme werde ich diese Funktionen als +// // eigenes Modul für die Bruchrechnung verwenden und weiter ausbauen. +// #[warn(unused_assignments)] +// pub fn calculate_fraction(fractions: Vec) -> Vec { +// let mut result_fraction: Vec = fractions; +// let mut index_brackets: Vec<(usize, usize)> = Vec::new(); +// let mut fractions_term: Vec = vec![]; + +// println!("Vektor davor: {:?}", result_fraction); +// parse_fractions(&mut result_fraction, &mut fractions_term); +// println!("Vektor danach: {:?}", result_fraction); + +// index_brackets = find_index(&result_fraction); +// println!("Index der Klammern: {:?}", index_brackets); + +// calculate_rules_fraction(&mut result_fraction, &mut fractions_term, &mut index_brackets); + +// //todo!("Noch im Bau! (calculate_fraction)"); +// return Vec::new() +// } + +// struct Bruch { +// numerator: i32, +// denominator: i32, +// } + +// // hier werden alle Brüche in einem Vektor gespeichert +// fn parse_fractions(fractions: &mut Vec, fractions_term: &mut Vec) { +// let mut index: usize = 0; + +// while index < fractions.len() { +// if fractions[index].contains("/") { +// if fractions[index + 1] == "0" { +// panic!("Division by Zero!") +// } + +// let vec_fractions = Bruch { +// numerator: fractions[index - 1].parse::().expect("invalid numerator"), +// denominator: fractions[index + 1].parse::().expect("invalid numerator"), +// }; + +// fractions_term.push(vec_fractions); + +// // Hier werden die Brüche durch einen Marker ersetzt. +// let index_bruch: String = "bruch_".to_string() + &((fractions_term.len()) - 1).to_string(); +// fractions.remove(index); +// fractions.insert(index, index_bruch); +// fractions.remove(index + 1); +// fractions.remove(index - 1); - } - index += 1; - } -} +// } +// index += 1; +// } +// } -fn calculate_rules_fraction(fractions: &mut Vec, fractions_term: &mut Vec, index_brackets: &mut Vec<(usize, usize)>) { - let mut index: usize = 0; +// fn calculate_rules_fraction(fractions: &mut Vec, fractions_term: &mut Vec, index_brackets: &mut Vec<(usize, usize)>) { +// let mut index: usize = 0; - while index < fractions.len() { +// while index < fractions.len() { - index += 1; - } +// index += 1; +// } - todo!("Noch im Bau (calculates_rules_fraction)!") -} \ No newline at end of file +// todo!("Noch im Bau (calculates_rules_fraction)!") +// } \ No newline at end of file diff --git a/src/arithmetic/basic_arithmetic_ops/calculation_rules.rs b/src/arithmetic/basic_arithmetic_ops/calculation_rules.rs index a42e1c7..00bd26d 100644 --- a/src/arithmetic/basic_arithmetic_ops/calculation_rules.rs +++ b/src/arithmetic/basic_arithmetic_ops/calculation_rules.rs @@ -87,35 +87,17 @@ pub fn find_operators(numbers: &Vec) -> Vec { ]; for i in numbers.iter() { - - if i.contains(operations[0].symbol) { - if operations[0].active == true { continue; } - operations[0].active = true; - } - if i.contains(operations[1].symbol) { - if operations[1].active == true { continue; } - operations[1].active = true; - } - - else if i.contains(operations[2].symbol) { - if operations[2].active == true { continue; } - operations[2].active = true; - } - else if i.contains(operations[3].symbol) { - if operations[3].active == true { continue; } - operations[3].active = true; - } - else if i.contains(operations[4].symbol) { - if operations[4].active == true { continue; } - operations[4].active = true; - } - else if i.contains(operations[5].symbol) { - if operations[5].active == true { continue; } - operations[5].active = true; - } - else if i.contains(operations[6].symbol) { - if operations[6].active == true { continue; } - operations[6].active = true; + for c in i.chars() { + match c { + '/' => { operations[0].active = true; }, + '(' => { operations[1].active = true; }, + '^' => { operations[2].active = true; }, + '*' => { operations[3].active = true; }, + ':' => { operations[4].active = true; }, + '+' => { operations[5].active = true; }, + '-' => { operations[6].active = true; }, + _ => {} + } } } return operations diff --git a/src/helping_tools/string_manipulation.rs b/src/helping_tools/string_manipulation.rs index e44d57b..ae15782 100644 --- a/src/helping_tools/string_manipulation.rs +++ b/src/helping_tools/string_manipulation.rs @@ -29,23 +29,26 @@ fn remove_whitespaces(with_whitespaces: &mut String) { // und auf Vollständigkeit der Klammern. fn validation_brackets_operators(brackets_ops: &mut String) { let mut count_brackets: usize = 0; - //let brackets_ops_string: &String = brackets_ops; // Das allererste Zeichen wird gelesen. let first_char: char = brackets_ops.chars().nth(0).unwrap(); - // Prüfen ob das erste Zeichen des Strings korrekt - // anfängt. Bei der Berechnung dürfen nur +, -, ( oder eine Zahl + // Bei der Berechnung dürfen nur +, -, ( oder eine Zahl // den Anfang machen. Hier wird jeweils das Programm abgebrochen // um sicher zu gehen das es nicht in einen inkonsistenten - // Zusatnd kommt. + // Zustand kommt. if first_char == '-' || first_char == '+' || first_char.is_digit(10) || first_char == '(' { for terms in brackets_ops.chars() { if terms == ')' && count_brackets == 0 { panic!("Darf nicht mit einer ')' Klammer beginnen") } - if terms == '(' || terms == ')' { + if terms == '(' { count_brackets += 1; + } else if terms == ')' { + if count_brackets == 0 { + panic!("Zu viele schließende Klammern"); + } + count_brackets -= 1; } } // prüfen ob die Klammern vollständig sind diff --git a/src/main.rs b/src/main.rs index f85568f..4977546 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,5 @@ #![allow(dead_code)] -use arithmetic::basic_arithmetic_ops::calculation_rules::rules_for_calculation; -use helping_tools::display_terminal::display_terminals; use menue::mathtool_menue_terminal; mod paths; @@ -16,14 +14,7 @@ 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 + 5/6 * (3/2 + 6 / 4) + 6 : (3/9 * 3/4 + 1)".to_string(); mathtool_menue_terminal(); - - // println!(); - // display_terminals("Original Formel".to_string(), &equation_string); - - // let splitted_terms: Vec = paths::str_manipulation::strings_refactor(equation_string); - - // println!("Ergebnis: {:?}", rules_for_calculation(splitted_terms)); } \ No newline at end of file diff --git a/src/menue.rs b/src/menue.rs index 95e7d8a..c2196b2 100644 --- a/src/menue.rs +++ b/src/menue.rs @@ -15,7 +15,7 @@ pub fn mathtool_menue_terminal() { let mut input: String = input_formula(); match input.as_str().trim() { - "y" | "Y" => { break; } + "q" | "Q" => { break; } "1" => { sub_menue_arithmetic(); } @@ -30,15 +30,18 @@ pub fn mathtool_menue_terminal() { fn main_print_menue() { println!("Mathetool by Super(d/g)oof\n"); println!("(1). Arithmetik\n"); - println!("Eingabe als Nummer, zum beenden (y/Y) eingeben.\n"); + println!("Eingabe als Nummer, zum Beenden (q/Q) eingeben.\n"); print!("Ihre Eingabe: "); } + fn sub_menue_arithmetic() { let mut input: String = String::new(); loop { clearscreen(); + display_term_rules(); + print!("Ihr Term: "); input = input_formula(); @@ -55,10 +58,15 @@ fn sub_menue_arithmetic() { input = input_formula(); - match input.as_str() { + match input.as_str().trim() { "b" | "B" => { break; } "w" | "W" => { continue; } _ => { print!("Ungültige Eingabe") } } } +} + +fn display_term_rules() { + println!("Bei der Eingabe bitte darauf achten nur gültige Zeichen zu verwenden!\n"); + println!("Gültige Eingaben sind: '+,-,*,/,:,^,(,)' und alle Zahlen!\n") } \ No newline at end of file