| 1 | use std::io; |
| 2 | |
| 3 | fn main() { |
| 4 | println!("Welcome to the Calculator!"); |
| 5 | |
| 6 | loop { |
| 7 | println!("Please enter an operator (+, -, *, /) or 'q' to quit:"); |
| 8 | let operator = read_input(); |
| 9 | |
| 10 | if operator == "q" { |
| 11 | break; |
| 12 | } |
| 13 | |
| 14 | println!("Please enter the first number:"); |
| 15 | let num1 = read_input().parse::<f64>().unwrap(); |
| 16 | |
| 17 | println!("Please enter the second number:"); |
| 18 | let num2 = read_input().parse::<f64>().unwrap(); |
| 19 | |
| 20 | let result = match operator.as_str() { |
| 21 | "+" => num1 + num2, |
| 22 | "-" => num1 - num2, |
| 23 | "*" => num1 * num2, |
| 24 | "/" => num1 / num2, |
| 25 | _ => { |
| 26 | println!("Invalid operator. Please try again."); |
| 27 | continue; |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | println!("Result: {}", result); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn read_input() -> String { |
| 36 | let mut input = String::new(); |
| 37 | io::stdin() |
| 38 | .read_line(&mut input) |
| 39 | .expect("Failed to read input"); |
| 40 | input.trim().to_string() |
| 41 | } |
| 42 | |
| 43 | --- |
| 44 | |
| 45 | use std::io; |
| 46 | |
| 47 | fn main() { |
| 48 | println!("Welcome to the Calculator!"); |
| 49 | |
| 50 | loop { |
| 51 | println!("Please enter an operator (+, -, *, /, ^) or 'q' to quit:"); |
| 52 | let operator = read_input(); |
| 53 | |
| 54 | if operator == "q" { |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | println!("Please enter the first number:"); |
| 59 | let num1 = read_input().parse::<f64>().unwrap(); |
| 60 | |
| 61 | println!("Please enter the second number:"); |
| 62 | let num2 = read_input().parse::<f64>().unwrap(); |
| 63 | |
| 64 | let result = match operator.as_str() { |
| 65 | "+" => num1 + num2, |
| 66 | "-" => num1 - num2, |
| 67 | "*" => num1 * num2, |
| 68 | "/" => num1 / num2, |
| 69 | "^" => num1.powf(num2), |
| 70 | _ => { |
| 71 | println!("Invalid operator. Please try again."); |
| 72 | continue; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | println!("Result: {}", result); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // ... existing code ... |
| 81 | |
| 82 | --- |
| 83 | |
| 84 | use std::io; |
| 85 | |
| 86 | fn main() { |
| 87 | println!("Welcome to the Calculator!"); |
| 88 | |
| 89 | loop { |
| 90 | - println!("Please enter an operator (+, -, *, /) or 'q' to quit:"); |
| 91 | + println!("Please enter an operator (+, -, *, /, ^) or 'q' to quit:"); |
| 92 | let operator = read_input(); |
| 93 | |
| 94 | if operator == "q" { |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | println!("Please enter the first number:"); |
| 99 | let num1 = read_input().parse::<f64>().unwrap(); |
| 100 | |
| 101 | println!("Please enter the second number:"); |
| 102 | let num2 = read_input().parse::<f64>().unwrap(); |
| 103 | |
| 104 | let result = match operator.as_str() { |
| 105 | "+" => num1 + num2, |
| 106 | "-" => num1 - num2, |
| 107 | "*" => num1 * num2, |
| 108 | "/" => num1 / num2, |
| 109 | + "^" => num1.powf(num2), |
| 110 | _ => { |
| 111 | println!("Invalid operator. Please try again."); |
| 112 | continue; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | println!("Result: {}", result); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn read_input() -> String { |
| 121 | let mut input = String::new(); |
| 122 | io::stdin() |
| 123 | .read_line(&mut input) |
| 124 | .expect("Failed to read input"); |
| 125 | input.trim().to_string() |
| 126 | } |