From 4a3bfea385b917271c56c885948a0515549d484b Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Wed, 1 Jan 2025 22:08:02 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 6 ++++ src/main.rs | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..48f3b86 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5633413 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "weirdcat_first_calculator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..82e16a8 --- /dev/null +++ b/src/main.rs @@ -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); + +}