Initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mine"
|
||||||
|
version = "0.1.0"
|
||||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "weirdcat_first_calculator"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
97
src/main.rs
Normal file
97
src/main.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
use std::io;
|
||||||
|
|
||||||
|
// The enum construct "Action" represents the mathematic actions possible
|
||||||
|
// to perform between two numbers in the calculator.
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
enum Action {
|
||||||
|
Addition,
|
||||||
|
Subtraction,
|
||||||
|
Multiplication,
|
||||||
|
Division
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adding a method to convert an instance of the enum to string, representing
|
||||||
|
// the mathematic sign, representing the operation.
|
||||||
|
impl Action {
|
||||||
|
fn to_char(&self) -> char {
|
||||||
|
match self {
|
||||||
|
Action::Addition => '+',
|
||||||
|
Action::Subtraction => '-',
|
||||||
|
Action::Multiplication => '*',
|
||||||
|
Action::Division => '/',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function for retrieving the operation the user wants to perform between
|
||||||
|
// to numbers.
|
||||||
|
fn input_action() -> Action {
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
|
||||||
|
println!("Choose action\n\
|
||||||
|
1. Addition\n\
|
||||||
|
2. Subtraction\n\
|
||||||
|
3. Multiplication\n\
|
||||||
|
4. Division");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut input)
|
||||||
|
.expect("Failed to read action");
|
||||||
|
|
||||||
|
let parsed_action_number: u8 = input.trim().parse().expect("Action is not valid, must be a num");
|
||||||
|
|
||||||
|
let parsed_action: Action;
|
||||||
|
match parsed_action_number {
|
||||||
|
1 => parsed_action = Action::Addition,
|
||||||
|
2 => parsed_action = Action::Subtraction,
|
||||||
|
3 => parsed_action = Action::Multiplication,
|
||||||
|
4 => parsed_action = Action::Division,
|
||||||
|
_ => panic!("Action is not valid, must be a num")
|
||||||
|
}
|
||||||
|
return parsed_action
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function for number input
|
||||||
|
fn input_num() -> i128 {
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
println!("Enter number: ");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut input)
|
||||||
|
.expect("Failed to read number");
|
||||||
|
|
||||||
|
let parsed_number: i128 = input.trim().parse().expect("Number is not valid");
|
||||||
|
return parsed_number
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function for calculating the result of an operation between to numbers.
|
||||||
|
fn calc(a: i128, b: i128, action: Action) -> i128 {
|
||||||
|
let result: i128;
|
||||||
|
|
||||||
|
match action {
|
||||||
|
Action::Addition => result = a + b,
|
||||||
|
Action::Subtraction => result = a - b,
|
||||||
|
Action::Multiplication => result = a * b,
|
||||||
|
Action::Division => result = a / b,
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let number_a: i128;
|
||||||
|
let number_b: i128;
|
||||||
|
let action: Action;
|
||||||
|
let calc_result: i128;
|
||||||
|
|
||||||
|
println!("Addition calculator");
|
||||||
|
|
||||||
|
number_a = input_num();
|
||||||
|
number_b = input_num();
|
||||||
|
action = input_action();
|
||||||
|
calc_result = calc(number_a, number_b, action);
|
||||||
|
|
||||||
|
println!("{} {} {} = {}", number_a, action.to_char(), number_b, calc_result);
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user