From fc49831d5cdaa08c0d68cf0cddd24560b3cf3b29 Mon Sep 17 00:00:00 2001 From: JoseCanales99 Date: Fri, 30 Sep 2022 11:22:45 -0600 Subject: [PATCH] Deleting an extra folder --- calculadora/.gitignore | 1 + calculadora/Cargo.lock | 42 +++++++++++++++++++++++++++++++ calculadora/Cargo.toml | 9 +++++++ calculadora/src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++++ suma/.gitignore | 1 + suma/Cargo.lock | 7 ++++++ suma/Cargo.toml | 8 ++++++ suma/src/main.rs | 33 ++++++++++++++++++++++++ 8 files changed, 157 insertions(+) create mode 100644 calculadora/.gitignore create mode 100644 calculadora/Cargo.lock create mode 100644 calculadora/Cargo.toml create mode 100644 calculadora/src/main.rs create mode 100644 suma/.gitignore create mode 100644 suma/Cargo.lock create mode 100644 suma/Cargo.toml create mode 100644 suma/src/main.rs diff --git a/calculadora/.gitignore b/calculadora/.gitignore new file mode 100644 index 0000000..c41cc9e --- /dev/null +++ b/calculadora/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/calculadora/Cargo.lock b/calculadora/Cargo.lock new file mode 100644 index 0000000..853e866 --- /dev/null +++ b/calculadora/Cargo.lock @@ -0,0 +1,42 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e" +dependencies = [ + "memchr", +] + +[[package]] +name = "calculadora" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" diff --git a/calculadora/Cargo.toml b/calculadora/Cargo.toml new file mode 100644 index 0000000..dde3633 --- /dev/null +++ b/calculadora/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "calculadora" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1.5.5" diff --git a/calculadora/src/main.rs b/calculadora/src/main.rs new file mode 100644 index 0000000..5c2692a --- /dev/null +++ b/calculadora/src/main.rs @@ -0,0 +1,56 @@ +use regex::Regex; + +fn make_operation(reg: Regex, mut expresion: String, operation: &str) -> String { + if operation.is_empty() { + return "".to_string(); + } + loop { + //Aplicar operaciones + let caps = reg.captures(expresion.as_str()); + + if caps.is_none() { + break; + } + let caps = caps.unwrap(); + + let cap_expresion = caps.get(0).unwrap().as_str(); + let left_value: i32 = caps.get(1).unwrap().as_str().parse().unwrap(); + let right_value: i32 = caps.get(2).unwrap().as_str().parse().unwrap(); + + let result = match operation { + "+" => left_value + right_value, + "-" => left_value - right_value, + "*" => left_value * right_value, + "/" => left_value / right_value, + _ => 0, + }; + + expresion = expresion.replace(cap_expresion, &result.to_string()); + } + expresion +} + +fn main() { + //Regex + let re_add = Regex::new(r"(\d+)\s?\+\s?(\d+)").unwrap(); + let re_less = Regex::new(r"(\d+)\s?\-\s?(\d+)").unwrap(); + let re_mult = Regex::new(r"(\d+)\s?\*\s?(\d+)").unwrap(); + let re_div = Regex::new(r"(\d+)\s?/\s?(\d+)").unwrap(); + + //Traer datos del usuario + println!("Por favor introduce tu expresion"); + let mut expresion = String::new(); + std::io::stdin().read_line(&mut expresion).unwrap(); + + //Multiplicacion + expresion = make_operation(re_mult, expresion, "*"); + //Division + expresion = make_operation(re_div, expresion, "/"); + //Suma + expresion = make_operation(re_add, expresion, "+"); + //Resta + expresion = make_operation(re_less, expresion, "-"); + + //Mostrar resultados + println!("Resultados: {}", expresion); +} \ No newline at end of file diff --git a/suma/.gitignore b/suma/.gitignore new file mode 100644 index 0000000..c41cc9e --- /dev/null +++ b/suma/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/suma/Cargo.lock b/suma/Cargo.lock new file mode 100644 index 0000000..5acebab --- /dev/null +++ b/suma/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "suma" +version = "0.1.0" diff --git a/suma/Cargo.toml b/suma/Cargo.toml new file mode 100644 index 0000000..6d891af --- /dev/null +++ b/suma/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "suma" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/suma/src/main.rs b/suma/src/main.rs new file mode 100644 index 0000000..e77f45c --- /dev/null +++ b/suma/src/main.rs @@ -0,0 +1,33 @@ +fn main() { + + println!("Ingrese el primer numero: "); + let mut numero_1 = String::new(); + std::io::stdin().read_line(&mut numero_1).unwrap(); + let numero1_int: i32 = numero_1.trim().parse().unwrap(); + + println!("Ingrese el segundo numero: "); + let mut numero_2 = String::new(); + std::io::stdin().read_line(&mut numero_2).unwrap(); + let numero2_int: i32 = numero_2.trim().parse().unwrap(); + + let suma = numero1_int + numero2_int; + + loop { + //mostrar los dos numeros en consola + println!("El primer numero es: {}, el segundo numero es: {}, ¿Cuál es la suma?", numero_1, numero_2); + + //Obtener del usuario el numero que represnta la suma + let mut suma_usuario = String::new(); + std::io::stdin().read_line(&mut suma_usuario).unwrap(); + let suma_usuario_int: i32 = suma_usuario.trim().parse().unwrap(); + + + if suma_usuario_int == suma { + println!("La suma es correcta C:"); + break + } else { + println!("La suma es incorrecta :C. Respuesta correcta: {}", suma) + } + } +} +