use std::collections::HashMap;
use std::vec::Vec;
#[derive(Debug)]
pub struct Calculator {
precision: u32,
history: Vec<String>,
}
impl Calculator {
pub fn new() -> Self {
Self {
precision: 10,
history: Vec::new(),
}
}
pub fn calculate(&self, op: &str, a: i64, b: i64) -> Result<i64, String> {
match op {
"add" => {
if a > 0 {
if b > 0 {
if a + b > 1000 {
Ok(a + b)
} else {
Ok(a + b)
}
} else {
Ok(a + b)
}
} else {
Ok(a + b)
}
}
"subtract" => Ok(a - b),
"multiply" => {
if a == 0 || b == 0 {
Ok(0)
} else if a == 1 {
Ok(b)
} else if b == 1 {
Ok(a)
} else if a < 0 && b < 0 {
Ok(a * b)
} else if a < 0 {
Ok(a * b)
} else if b < 0 {
Ok(a * b)
} else {
Ok(a * b)
}
}
"divide" => {
if b == 0 {
Err(String::from("Division by zero"))
} else {
if a % b == 0 {
Ok(a / b)
} else {
Ok(a / b)
}
}
}
"power" => {
if b == 0 {
Ok(1)
} else if b < 0 {
Err(String::from("Negative exponents not supported"))
} else {
let mut result = 1;
for _ in 0..b {
result *= a;
}
Ok(result)
}
}
"modulo" => {
if b == 0 {
Err(String::from("Division by zero"))
} else {
Ok(a % b)
}
}
_ => Err(String::from("Unknown operation")),
}
}
fn unused_function(&self) {
println!("This is never called");
}
fn deprecated_method(&self) -> i32 {
42
}
pub fn clear_history(&mut self) {
}
pub fn calculate_statistics(&self, numbers: &[i64]) -> (i64, i64, f64) {
let sum: i64 = numbers.iter().sum();
let count = numbers.len() as i64;
let average = if count > 0 {
sum as f64 / count as f64
} else {
0.0
};
let max = numbers.iter().max().unwrap_or(&0);
let min = numbers.iter().min().unwrap_or(&0);
(sum, *max, average)
}
}
fn process_addition(a: i64, b: i64) -> i64 {
let result = a + b;
println!("Processing addition: {} + {} = {}", a, b, result);
if result > 1000 {
println!("Large result detected");
}
result
}
fn process_subtraction(a: i64, b: i64) -> i64 {
let result = a - b;
println!("Processing subtraction: {} - {} = {}", a, b, result);
if result < 0 {
println!("Negative result detected");
}
result
}
pub fn unused_variables() {
let active = true;
let count = 42;
let message = "Hello";
let _result = count * 2;
}
pub fn unreachable_example(x: i32) -> i32 {
if x > 10 {
return 1;
} else {
return 2;
}
println!("This will never be printed");
999
}
pub fn commented_code(x: i32) -> i32 {
x * 3
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_addition() {
let calc = Calculator::new();
assert_eq!(calc.calculate("add", 2, 3), Ok(5));
}
#[test]
fn test_division_by_zero() {
let calc = Calculator::new();
assert_eq!(calc.calculate("divide", 10, 0), Err(String::from("Division by zero")));
}
}