-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
|