From 8d764d89ece5999a477b381ea1bc5d985b945b8b Mon Sep 17 00:00:00 2001 From: Supergoof1410 <161129122+Supergoof1410@users.noreply.github.com> Date: Sun, 18 May 2025 16:47:12 +0200 Subject: [PATCH] =?UTF-8?q?menue=20eingef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 304 ++++++++++++++++++ Cargo.toml | 3 +- .../basic_arithmetic_ops/brackets_solver.rs | 8 +- .../basic_arithmetic_ops/calculating.rs | 11 +- .../calculating_fractions.rs | 71 ++++ .../basic_arithmetic_ops/calculation_rules.rs | 42 ++- src/arithmetic/basic_arithmetic_ops/mod.rs | 3 +- src/helping_tools/display_terminal.rs | 12 + src/helping_tools/io_formula.rs | 11 + src/helping_tools/mod.rs | 3 +- src/helping_tools/string_manipulation.rs | 45 ++- src/main.rs | 17 +- src/menue.rs | 64 ++++ src/paths.rs | 1 + 14 files changed, 540 insertions(+), 55 deletions(-) create mode 100644 src/arithmetic/basic_arithmetic_ops/calculating_fractions.rs create mode 100644 src/helping_tools/io_formula.rs create mode 100644 src/menue.rs diff --git a/Cargo.lock b/Cargo.lock index 541c653..b8814e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ version = 4 name = "Mathetool" version = "0.1.0" dependencies = [ + "crossterm", "num-complex", ] @@ -15,6 +16,77 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "crossterm" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" +dependencies = [ + "bitflags", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + +[[package]] +name = "libc" +version = "0.2.172" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "mio" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys", +] + [[package]] name = "num-complex" version = "0.4.6" @@ -32,3 +104,235 @@ checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.6", +] + +[[package]] +name = "redox_syscall" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928fca9cf2aa042393a8325b9ead81d2f0df4cb12e1e24cef072922ccd99c5af" +dependencies = [ + "bitflags", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "signal-hook" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9203b8055f63a2a00e2f593bb0510367fe707d7ff1e5c872de2f537b339e5410" +dependencies = [ + "libc", +] + +[[package]] +name = "smallvec" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 3a06b75..3cbafdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] -num-complex = "0.4" \ No newline at end of file +num-complex = "0.4" +crossterm = "0.27" \ No newline at end of file diff --git a/src/arithmetic/basic_arithmetic_ops/brackets_solver.rs b/src/arithmetic/basic_arithmetic_ops/brackets_solver.rs index f58befc..61dd75a 100644 --- a/src/arithmetic/basic_arithmetic_ops/brackets_solver.rs +++ b/src/arithmetic/basic_arithmetic_ops/brackets_solver.rs @@ -17,8 +17,8 @@ pub fn calculate_resolve_brackets(numbers: Vec) -> Vec { let mut result_index_brackets: Vec<(usize, usize)> = find_index(&result_brackets_calc); // Hier hole ich den höchsten Index für die Klammerpaarungen - // und die Auflösungen nach dem Prinzip innen nach aussen, und - // Klammern die unabhängig von den verschachtelten bin. Mit + // für die Auflösungen nach dem Prinzip von innen nach aussen, und + // Klammernpaare die unabhängig von den verschachtelten sind. Mit // dieser Art wird die Reihenfolge eingehalten. for i in result_index_brackets.iter() { println!("Vektor Klammer: [{:?}, {:?}]", i.0, i.1); @@ -61,7 +61,7 @@ fn calculate_brackets(numbers: &mut Vec, brackets_index: (usize, usize)) println!("Vektor davor: {:?}", numbers); // Ich benutze hier drain, damit kann ich den Teil für die Berechnung - // heraus entfernen. Dadurch wird mir der Teil gegeben den ich berechnen kann. + // heraus entfernen kann. Dadurch wird mir der Teil gegeben den ich berechnen kann. let mut removed_slices: Vec = numbers.drain(brackets_index.0..=brackets_index.1).collect(); // Die Klammern müssen entfernt werden sonst würde es zu @@ -72,7 +72,7 @@ fn calculate_brackets(numbers: &mut Vec, brackets_index: (usize, usize)) println!("Herausgenommener Term: {:?}", removed_slices); - // Berechnen nach den Regeln/Reihenfolge der Mathemathik + // Berechnen nach den Regeln/Reihenfolge der Mathematik let bracket_result: String = rules_for_calculation(removed_slices); // Zuletzt wird hier das Ergebnis in den Verktor wieder eingefügt, diff --git a/src/arithmetic/basic_arithmetic_ops/calculating.rs b/src/arithmetic/basic_arithmetic_ops/calculating.rs index fa1c3eb..2ee5e72 100644 --- a/src/arithmetic/basic_arithmetic_ops/calculating.rs +++ b/src/arithmetic/basic_arithmetic_ops/calculating.rs @@ -90,12 +90,12 @@ pub fn calculate_numbers_mult_diff(numbers: Vec) -> Vec { } // Division - else if result_mul_div_vector[index].contains("/") { + else if result_mul_div_vector[index].contains(":") || result_mul_div_vector[index].contains("/") { counter_div += 1; println!("Vektor davor: {:?}", result_mul_div_vector); - let result_mult: String = calculate_mult_diff(&result_mul_div_vector, counter_div, '/'); + let result_mult: String = calculate_mult_diff(&result_mul_div_vector, counter_div, result_mul_div_vector[index].parse::().unwrap()); removing_from_vector(&mut result_mul_div_vector, index, result_mult); println!("Vektor danach: {:?}", result_mul_div_vector); @@ -126,7 +126,12 @@ fn calculate_mult_diff(numbers: &Vec, counter: usize, which_operator: ch result = left_right[0] * left_right[1]; operation = "Multiplikation".to_string(); } - '/' => { + ':' | '/' => { + // Prüfen des rechten Operanden auf 0, wenn ja + // abbruch. + if left_right[1] == 0.0 { + panic!("Division durch 0 nicht möglich!"); + } result = left_right[0] / left_right[1]; operation = "Division".to_string(); } diff --git a/src/arithmetic/basic_arithmetic_ops/calculating_fractions.rs b/src/arithmetic/basic_arithmetic_ops/calculating_fractions.rs new file mode 100644 index 0000000..3d8ec1c --- /dev/null +++ b/src/arithmetic/basic_arithmetic_ops/calculating_fractions.rs @@ -0,0 +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); + + } + index += 1; + } +} + +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() { + + + index += 1; + } + + 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 225d1bc..a42e1c7 100644 --- a/src/arithmetic/basic_arithmetic_ops/calculation_rules.rs +++ b/src/arithmetic/basic_arithmetic_ops/calculation_rules.rs @@ -11,26 +11,33 @@ pub fn rules_for_calculation(formula: Vec) -> String { let mut changed_formula: Vec = formula; - if found_ops[0].active { + // if found_ops[0].active { + // println!("\nBrüche berechnen\n"); + // changed_formula = calculate_fraction(changed_formula); + // found_ops = find_operators(&changed_formula); + // println!("--------------------------\n"); + // } + + if found_ops[1].active { println!("Klammern auflösen/berechnen\n"); changed_formula = calculate_resolve_brackets(changed_formula); found_ops = find_operators(&changed_formula); println!("--------------------------\n"); } - if found_ops[1].active { + if found_ops[2].active { println!("Potenzen werden berechnet.\n"); changed_formula = calculate_numbers_powers(changed_formula); //found_ops[1].active = false; println!("--------------------------\n"); } - if found_ops[2].active || found_ops[3].active { + if found_ops[3].active || found_ops[4].active || found_ops[0].active { println!("Berechnung (Punkt vor Strich)\n"); changed_formula = calculate_numbers_mult_diff(changed_formula); println!("--------------------------\n"); } - if found_ops[4].active || found_ops[5].active { + if found_ops[5].active || found_ops[6].active { println!("Berechnung (Plus und Minus)\n"); changed_formula = calculate_numbers_addition(changed_formula); println!("--------------------------\n"); @@ -67,15 +74,16 @@ impl OperatorInfo { // ob eine Funktion zum berechnen aufgerufen werden muss // oder auch nicht. Und damit die Rechenregeln in der richtigen // Reihenfolge ablaufen. -fn find_operators(numbers: &Vec) -> Vec { +pub fn find_operators(numbers: &Vec) -> Vec { let mut operations:Vec = vec![ - OperatorInfo { index: 0, symbol: '(', active: false }, - OperatorInfo { index: 1, symbol: '^', active: false }, - OperatorInfo { index: 2, symbol: '*', active: false }, - OperatorInfo { index: 3, symbol: '/', active: false }, - OperatorInfo { index: 4, symbol: '+', active: false }, - OperatorInfo { index: 5, symbol: '-', active: false }, + OperatorInfo { index: 0, symbol: '/', active: false }, + OperatorInfo { index: 1, symbol: '(', active: false }, + OperatorInfo { index: 2, symbol: '^', active: false }, + OperatorInfo { index: 3, symbol: '*', active: false }, + OperatorInfo { index: 4, symbol: ':', active: false }, + OperatorInfo { index: 5, symbol: '+', active: false }, + OperatorInfo { index: 6, symbol: '-', active: false }, ]; for i in numbers.iter() { @@ -84,11 +92,11 @@ fn find_operators(numbers: &Vec) -> Vec { if operations[0].active == true { continue; } operations[0].active = true; } - - else if i.contains(operations[1].symbol) { + 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; @@ -99,11 +107,15 @@ fn find_operators(numbers: &Vec) -> Vec { } else if i.contains(operations[4].symbol) { if operations[4].active == true { continue; } - operations[4].active = true; + operations[4].active = true; } else if i.contains(operations[5].symbol) { if operations[5].active == true { continue; } - operations[5].active = true; + operations[5].active = true; + } + else if i.contains(operations[6].symbol) { + if operations[6].active == true { continue; } + operations[6].active = true; } } return operations diff --git a/src/arithmetic/basic_arithmetic_ops/mod.rs b/src/arithmetic/basic_arithmetic_ops/mod.rs index dd345e8..49dcba9 100644 --- a/src/arithmetic/basic_arithmetic_ops/mod.rs +++ b/src/arithmetic/basic_arithmetic_ops/mod.rs @@ -3,4 +3,5 @@ pub mod calculation_rules; pub mod index_operators; pub mod operators; pub mod operations; -pub mod brackets_solver; \ No newline at end of file +pub mod brackets_solver; +pub mod calculating_fractions; \ No newline at end of file diff --git a/src/helping_tools/display_terminal.rs b/src/helping_tools/display_terminal.rs index 989ca0b..9b75453 100644 --- a/src/helping_tools/display_terminal.rs +++ b/src/helping_tools/display_terminal.rs @@ -8,4 +8,16 @@ pub(crate) fn display_terminals_validate(output_string: String, left: &String, r let width: usize = 33; println!("{: String { + + io::stdout().flush().expect("Ungültige Eingabe"); + + let mut input_term: String = String::new(); + io::stdin().read_line(&mut input_term).expect("Ungültige Eingabe"); + + return input_term.trim_end().to_string(); +} \ No newline at end of file diff --git a/src/helping_tools/mod.rs b/src/helping_tools/mod.rs index 911095c..1e5c5ad 100644 --- a/src/helping_tools/mod.rs +++ b/src/helping_tools/mod.rs @@ -1,2 +1,3 @@ pub mod string_manipulation; -pub mod display_terminal; \ No newline at end of file +pub mod display_terminal; +pub mod io_formula; \ No newline at end of file diff --git a/src/helping_tools/string_manipulation.rs b/src/helping_tools/string_manipulation.rs index 8dd09e8..e44d57b 100644 --- a/src/helping_tools/string_manipulation.rs +++ b/src/helping_tools/string_manipulation.rs @@ -1,14 +1,15 @@ use super::display_terminal::display_terminals; // Diese Funktion ist für andere sichtbar und macht die -// Stringmanipulationen verfügbar. Hier wird auch für die -// Klammerberechnung noch ein bool hinzugefügt, was später -// abgefragt werden kann ob die Klammerberechnung nötig ist. +// Stringmanipulationen verfügbar. pub fn strings_refactor(crazy_string: String) -> Vec { - let result_string: String = remove_whitespaces(crazy_string); - let terms_replaced: String = terms_replace_operators(result_string); - let terms_splitted: Vec = split_terms(terms_replaced); + let mut crazy_string_refactor: String = crazy_string; + remove_whitespaces(&mut crazy_string_refactor); + validation_brackets_operators(&mut crazy_string_refactor); + terms_replace_operators(&mut crazy_string_refactor); + + let terms_splitted: Vec = split_terms(crazy_string_refactor); let negative_numbers: Vec = validate_negative_numbers(terms_splitted); return negative_numbers @@ -16,38 +17,35 @@ pub fn strings_refactor(crazy_string: String) -> Vec { // Um überflüssige Leerzeichen zu entfernen // Damit sind auch Strings in der Form " 3 + 5 * 3" möglich -fn remove_whitespaces(with_whitespaces: String) -> String { +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); - result_string + + *with_whitespaces = result_string; } // Extra Funktionen für den Anfang einer Formel, -// und auf vollständigkeit der Klammern. Zudem wird hier ein -// bool zurückgegeben für die notwendigkeit der Klammerberechnung. -// Da hier nur eine Validierung gemacht wird benutze ich Referenzen -// anstatt die Ownership zu übergeben. -fn validation_brackets_operators(brackets_ops: &String) -> bool { +// 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; - let mut if_brackets: bool = false; + //let brackets_ops_string: &String = brackets_ops; // Das allererste Zeichen wird gelesen. - let first_char: char = brackets_ops_string.chars().nth(0).unwrap(); + 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 sein + // anfängt. 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. if first_char == '-' || first_char == '+' || first_char.is_digit(10) || first_char == '(' { - for terms in brackets_ops_string.chars() { + for terms in brackets_ops.chars() { if terms == ')' && count_brackets == 0 { panic!("Darf nicht mit einer ')' Klammer beginnen") } if terms == '(' || terms == ')' { count_brackets += 1; - if_brackets = true; } } // prüfen ob die Klammern vollständig sind @@ -58,13 +56,11 @@ fn validation_brackets_operators(brackets_ops: &String) -> bool { else { panic!("Anfang der Formel muss mit +,-, ( oder einer Zahl beginnen"); } - return if_brackets - } // 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: String) -> String { +fn terms_replace_operators(splitted_equation: &mut String) { let mut terms_replaced: String = String::new(); for terms in splitted_equation.chars() { @@ -74,13 +70,14 @@ fn terms_replace_operators(splitted_equation: String) -> String { '+' => 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(" / "), _ => terms_replaced.push(terms), } } display_terminals("Leerzeichen vor den Operatoren".to_string(), &terms_replaced); - terms_replaced + *splitted_equation = terms_replaced; } // Hier werden die einzelnen Terme nochmals gesplitted, damit man besser diff --git a/src/main.rs b/src/main.rs index 11c1df2..f85568f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,23 +2,28 @@ use arithmetic::basic_arithmetic_ops::calculation_rules::rules_for_calculation; use helping_tools::display_terminal::display_terminals; +use menue::mathtool_menue_terminal; mod paths; mod helping_tools; mod algebra; mod arithmetic; +mod menue; fn main() { //let equation_string: String = "(100+2)^2-2*(5*(8/(9-12)+5)/2)*8+1*(2+5)*9".to_string(); // 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 = "3 + (-2) * (4 - 1)^2 - (-6 / 2 + 3)".to_string(); - let equation_string: String = "-(-5)-(-10)*6+5-(-(6-10))+5^(6+5)".to_string(); - println!(); - display_terminals("Original Formel".to_string(), &equation_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 splitted_terms: Vec = paths::str_manipulation::strings_refactor(equation_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)); + // println!("Ergebnis: {:?}", rules_for_calculation(splitted_terms)); } \ No newline at end of file diff --git a/src/menue.rs b/src/menue.rs new file mode 100644 index 0000000..95e7d8a --- /dev/null +++ b/src/menue.rs @@ -0,0 +1,64 @@ +use crate::helping_tools::io_formula::input_formula; +use crate::helping_tools::display_terminal::clearscreen; +use crate::arithmetic::basic_arithmetic_ops::calculation_rules::rules_for_calculation; +use crate::helping_tools::display_terminal::display_terminals; +use crate::paths; + + +pub fn mathtool_menue_terminal() { + + loop { + clearscreen(); + + main_print_menue(); + + let mut input: String = input_formula(); + + match input.as_str().trim() { + "y" | "Y" => { break; } + "1" => { + sub_menue_arithmetic(); + } + _ => { + print!("Ungültige Eingabe"); + } + } + input = input_formula(); + } +} + +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"); + print!("Ihre Eingabe: "); +} +fn sub_menue_arithmetic() { + let mut input: String = String::new(); + + loop { + clearscreen(); + + print!("Ihr Term: "); + input = input_formula(); + + if input.trim() == "" { continue; } + + display_terminals("Original Formel".to_string(), &input); + + let splitted_terms: Vec = paths::str_manipulation::strings_refactor(input); + + println!("Ergebnis: {:?}", rules_for_calculation(splitted_terms)); + + println!("Weiter (w) oder zurück (b)"); + print!("Ihre Eingabe: "); + + input = input_formula(); + + match input.as_str() { + "b" | "B" => { break; } + "w" | "W" => { continue; } + _ => { print!("Ungültige Eingabe") } + } + } +} \ No newline at end of file diff --git a/src/paths.rs b/src/paths.rs index 1fca589..e13ecaa 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -1,2 +1,3 @@ pub use crate::helping_tools::string_manipulation as str_manipulation; pub use crate::arithmetic::basic_arithmetic_ops::calculating as calc_nums; +