Skip to content

Instantly share code, notes, and snippets.

@andoniabedul
Last active January 14, 2024 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andoniabedul/2d13870b5e41ac6c8742c7f7bd98407b to your computer and use it in GitHub Desktop.
Save andoniabedul/2d13870b5e41ac6c8742c7f7bd98407b to your computer and use it in GitHub Desktop.
Simple calculator in Rust
use regex::Regex;
const OPERATIONS: [&str; 4] = ["MULTIPLICATION", "DIVISION", "SUM", "SUBTRACTION"];
fn main(){
println!("Calculadora");
let regex_add: Regex = Regex::new(r"(\d+)\s?\+\s?(\d+)").unwrap();
let regex_multiply: Regex = Regex::new(r"(\d+)\s?\*\s?(\d+)").unwrap();
let regex_subtraction: Regex = Regex::new(r"(\d+)\s?\-\s?(\d+)").unwrap();
let regex_division: Regex = Regex::new(r"(\d+)\s?\/\s?(\d+)").unwrap();
println!("Introduce tu expresion");
let mut expression: String = get_operation_from_user();
loop {
let is_addition: bool = !regex_add.captures(expression.as_str()).is_none();
let is_multiply: bool = !regex_multiply.captures(expression.as_str()).is_none();
let is_subtraction: bool = !regex_subtraction.captures(expression.as_str()).is_none();
let is_division: bool = !regex_division.captures(expression.as_str()).is_none();
// Operations
if is_multiply {
expression = make_operation(regex_multiply.clone(), expression, OPERATIONS[0]);
} else if is_division {
expression = make_operation(regex_division.clone(), expression, OPERATIONS[1]);
} else if is_addition {
expression = make_operation(regex_add.clone(), expression, OPERATIONS[2]);
} else if is_subtraction {
expression = make_operation(regex_subtraction.clone(), expression, OPERATIONS[3])
} else {
break;
}
}
println!("El resultado final es {}", expression);
}
fn get_operation_from_user() -> String {
let mut expression: String = String::new();
std::io::stdin().read_line(&mut expression).unwrap();
return expression;
}
fn make_operation(regex: Regex, expression: String, type_of_operation: &str) -> String {
let operation: i32;
let caps = regex.captures(expression.as_str());
let caps = caps.unwrap();
let cap_expression = 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();
if type_of_operation == OPERATIONS[0] {
operation = left_value * right_value;
return expression.replace(cap_expression, &operation.to_string());
} else if type_of_operation == OPERATIONS[1] {
operation = left_value / right_value;
return expression.replace(cap_expression, &operation.to_string());
} else if type_of_operation == OPERATIONS[2] {
operation = left_value + right_value;
return expression.replace(cap_expression, &operation.to_string());
} else if type_of_operation == OPERATIONS[3] {
operation = left_value - right_value;
return expression.replace(cap_expression, &operation.to_string());
} else {
println!("No operation found");
return "".to_string();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment