codemaster1104 / IISC

0 stars 0 forks source link

Test #1

Open codemaster1104 opened 1 month ago

codemaster1104 commented 1 month ago

use std::io::{self, Write};

enum Operation { Add, Subtract, Multiply, Divide, }

impl Operation { fn fromstr(input: &str) -> Option { match input { "+" => Some(Operation::Add), "-" => Some(Operation::Subtract), "*" => Some(Operation::Multiply), "/" => Some(Operation::Divide), => None, } } }

fn main() { loop { println!("Enter first number:"); let mut num1 = String::new(); io::stdin().readline(&mut num1).expect("Failed to read line"); let num1: f64 = match num1.trim().parse() { Ok(n) => n, Err() => { println!("Please enter a valid number"); continue; }, };

    println!("Enter an operator (+, -, *, /):");
    let mut op = String::new();
    io::stdin().read_line(&mut op).expect("Failed to read line");
    let op = match Operation::from_str(op.trim()) {
        Some(o) => o,
        None => {
            println!("Please enter a valid operator");
            continue;
        },
    };

    println!("Enter second number:");
    let mut num2 = String::new();
    io::stdin().read_line(&mut num2).expect("Failed to read line");
    let num2: f64 = match num2.trim().parse() {
        Ok(n) => n,
        Err(_) => {
            println!("Please enter a valid number");
            continue;
        },
    };

    let result = match op {
        Operation::Add => num1 + num2,
        Operation::Subtract => num1 - num2,
        Operation::Multiply => num1 * num2,
        Operation::Divide => {
            if num2 == 0.0 {
                println!("Cannot divide by zero");
                continue;
            } else {
                num1 / num2
            }
        },
    };

    println!("Result: {}", result);

    println!("Do you want to perform another calculation? (yes/no):");
    let mut response = String::new();
    io::stdin().read_line(&mut response).expect("Failed to read line");
    if response.trim().eq_ignore_ascii_case("no") {
        break;
    }
}

println!("Goodbye!");

}

codemaster1104 commented 1 month ago

flows summarize

codemaster1104 commented 1 month ago

Hello, I am a PR summary bot on flows.network.

It could take a few minutes for me to analyze this PR. Relax, grab a cup of coffee and check back later. Thanks!

codemaster1104 commented 1 month ago

flows summarize

codemaster1104 commented 1 month ago

Hello, I am a PR summary bot on flows.network.

It could take a few minutes for me to analyze this PR. Relax, grab a cup of coffee and check back later. Thanks!