Skip to content

Commit

Permalink
Deleting an extra folder
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeHank9 committed Sep 30, 2022
1 parent b126b6b commit fc49831
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions calculadora/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
42 changes: 42 additions & 0 deletions calculadora/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions calculadora/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
56 changes: 56 additions & 0 deletions calculadora/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions suma/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions suma/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions suma/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
33 changes: 33 additions & 0 deletions suma/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit fc49831

Please sign in to comment.