|
| 1 | +#[derive(Debug, Clone, Copy)] |
| 2 | +pub struct Expression { |
| 3 | + pub first_num: f64, |
| 4 | + pub operation: Operation, |
| 5 | + pub second_num: f64, |
| 6 | +} |
| 7 | + |
| 8 | +#[derive(Debug, Clone, Copy)] |
| 9 | +pub enum Operation { |
| 10 | + Addition, |
| 11 | + Subtraction, |
| 12 | + Multiplication, |
| 13 | + Division, |
| 14 | + Power, |
| 15 | +} |
| 16 | + |
| 17 | +impl Expression { |
| 18 | + pub fn eval(&self) -> f64 { |
| 19 | + match self.operation { |
| 20 | + Operation::Addition => self.first_num + self.second_num, |
| 21 | + Operation::Subtraction => self.first_num - self.second_num, |
| 22 | + Operation::Multiplication => self.first_num * self.second_num, |
| 23 | + Operation::Division => self.first_num / self.second_num, |
| 24 | + Operation::Power => self.first_num.powf(self.second_num), |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + pub fn from_str(s: &str) -> Option<Expression> { |
| 29 | + parse_expression(s) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +fn parse_expression(s: &str) -> Option<Expression> { |
| 34 | + let s = s.trim(); |
| 35 | + |
| 36 | + // 1. Parse first (possibly signed) number with manual scan |
| 37 | + let (first_str, rest) = parse_signed_number_prefix(s)?; |
| 38 | + |
| 39 | + // 2. Next non‑whitespace char must be the binary operator |
| 40 | + let rest = rest.trim_start(); |
| 41 | + let (op_char, rest) = rest.chars().next().map(|c| (c, &rest[c.len_utf8()..]))?; |
| 42 | + |
| 43 | + let operation = match op_char { |
| 44 | + '+' => Operation::Addition, |
| 45 | + '-' => Operation::Subtraction, |
| 46 | + '*' => Operation::Multiplication, |
| 47 | + '/' => Operation::Division, |
| 48 | + '^' => Operation::Power, |
| 49 | + _ => return None, |
| 50 | + }; |
| 51 | + |
| 52 | + // 3. The remainder should be the second (possibly signed) number |
| 53 | + let rest = rest.trim_start(); |
| 54 | + let (second_str, tail) = parse_signed_number_prefix(rest)?; |
| 55 | + // Optionally ensure nothing but whitespace after second number: |
| 56 | + if !tail.trim().is_empty() { |
| 57 | + return None; |
| 58 | + } |
| 59 | + |
| 60 | + let first_num: f64 = first_str.parse().ok()?; |
| 61 | + let second_num: f64 = second_str.parse().ok()?; |
| 62 | + |
| 63 | + Some(Expression { |
| 64 | + first_num, |
| 65 | + operation, |
| 66 | + second_num, |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +/// Returns (number_lexeme, remaining_slice) for a leading signed float. |
| 71 | +/// Very simple: `[+|-]?` + "anything until we hit whitespace or an operator". |
| 72 | +fn parse_signed_number_prefix(s: &str) -> Option<(&str, &str)> { |
| 73 | + let s = s.trim_start(); |
| 74 | + if s.is_empty() { |
| 75 | + return None; |
| 76 | + } |
| 77 | + |
| 78 | + let mut chars = s.char_indices().peekable(); |
| 79 | + |
| 80 | + // Optional leading sign |
| 81 | + if let Some((_, c)) = chars.peek() |
| 82 | + && (*c == '+' || *c == '-') |
| 83 | + { |
| 84 | + chars.next(); |
| 85 | + } |
| 86 | + |
| 87 | + // Now consume until we hit an operator or whitespace |
| 88 | + let mut end = 0; |
| 89 | + while let Some((idx, c)) = chars.peek().cloned() { |
| 90 | + if c.is_whitespace() || "+-*/^".contains(c) { |
| 91 | + break; |
| 92 | + } |
| 93 | + end = idx + c.len_utf8(); |
| 94 | + chars.next(); |
| 95 | + } |
| 96 | + |
| 97 | + if end == 0 { |
| 98 | + return None; // nothing that looks like a number |
| 99 | + } |
| 100 | + |
| 101 | + let (num, rest) = s.split_at(end); |
| 102 | + Some((num, rest)) |
| 103 | +} |
0 commit comments