You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.0 KiB
Rust

use::std::io;
// Enum of different mathematical operations
#[derive(Debug)]
enum Operators {
Add,
Subtract,
Multiply,
Divide,
Exponent,
}
// Enum of different separators
#[derive(Debug)]
enum Separators {
Open,
Close,
Decimal,
}
// Enum for types of one 'block" in an eqation
#[derive(Debug)]
enum Blocks {
Operator(Operators), // A mathematical operator
Separator(Separators), // A separator, eg. '(', ')'
Integer(i32), // An integer
Float(f64), // A float
Error,
}
// Determine what type a 'block' is
fn determine_type(block: &str) -> Blocks {
let block_type = match block {
// Check if it's an operator
"+" => Blocks::Operator(Operators::Add),
"-" => Blocks::Operator(Operators::Subtract),
"*" => Blocks::Operator(Operators::Multiply),
"/" => Blocks::Operator(Operators::Divide),
"^" => Blocks::Operator(Operators::Exponent),
// Check if it's a separator
"(" => Blocks::Separator(Separators::Open),
")" => Blocks::Separator(Separators::Close),
"." => Blocks::Separator(Separators::Decimal),
_ => {
match block.parse::<i32>() {
Ok(integer) => Blocks::Integer(integer),
Err(_e) => {
match block.parse::<f64>() {
Ok(float) => Blocks::Float(float),
Err(_e) => Blocks::Error,
}
}
}
}
};
return block_type;
}
// cut the input string into different pieces
fn separate(input: String) {
// Vector for the whole equation
let mut equation: Vec<Blocks> = Vec::new();
// Vector for collecting a single number
let mut number: Vec<char> = Vec::new();
// Loop through each character in the equation
for i in input.chars() {
match i.to_digit(10) {
Some(..) => {
number.push(i);
},
None => {
if i == '.' {
number.push(i);
} else {
let full_number: String = number.iter().collect::<String>();
println!("Full number found: {}", full_number);
number = Vec::new(); // Any found number has finished, so clear the vector
equation.push(determine_type(&full_number));
}
equation.push(determine_type(&i.to_string())); // Determine the type and append it to the equation vector
}
}
println!("{}", i);
}
let full_number: String = number.iter().collect::<String>();
equation.push(determine_type(&full_number));
println!("{:?}", equation);
// 5+6-2/45*(6-2)^5
}
fn main() {
println!("RUNK (Rust Universal Number Calculator)");
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read input");
input = input.split_whitespace().collect();
println!("Your input was {}", input);
separate(input);
}