Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
Alexandre Stein | 86f08875a8 | |
Alexandre Stein | 26601e548e | |
Alexandre Stein | 908726e3c6 | |
Alexandre Stein | 3781029019 | |
Alexandre Stein | a033d3b647 | |
Alexandre Stein | fbe8e2c1c1 | |
Alexandre Stein | 327917e29a | |
Alexandre Stein | fc31bfc38f | |
Alexandre Stein | 8d36c71340 | |
Alexandre Stein | a1075277be |
|
@ -0,0 +1 @@
|
|||
rust-calc/target
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "rust-calc"
|
||||
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "rust-calc"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,380 @@
|
|||
use std::{
|
||||
ops::{AddAssign, DivAssign, MulAssign, SubAssign},
|
||||
process,
|
||||
};
|
||||
|
||||
// Provide a conformable way to work with symbols
|
||||
#[derive(Clone, Debug)]
|
||||
enum Symbol {
|
||||
Minus,
|
||||
Plus,
|
||||
Multiply,
|
||||
Divide,
|
||||
|
||||
OpenBracket,
|
||||
CloseBracket,
|
||||
}
|
||||
impl Symbol {
|
||||
// Checks if the given symbol has priority or not
|
||||
fn priority(&self) -> bool {
|
||||
match self {
|
||||
Symbol::Multiply => true,
|
||||
Symbol::Divide => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
// Check if it brackets (open or close)
|
||||
fn bracket(&self) -> bool {
|
||||
match self {
|
||||
Symbol::OpenBracket => true,
|
||||
Symbol::CloseBracket => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
// Check if it's an open bracket
|
||||
fn open_bracket(&self) -> bool {
|
||||
match self {
|
||||
Symbol::OpenBracket => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
// Check if it's an close bracket
|
||||
fn close_bracket(&self) -> bool {
|
||||
match self {
|
||||
Symbol::CloseBracket => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Provides a object to hold the number being parsed from the string
|
||||
struct TmpNum {
|
||||
s: String,
|
||||
}
|
||||
impl TmpNum {
|
||||
// Simple constructor
|
||||
pub fn new() -> Self {
|
||||
Self { s: String::new() }
|
||||
}
|
||||
// Add a new char to the list of chars used to describe the number in a base 10 fashion
|
||||
fn push(&mut self, c: char) {
|
||||
self.s.push(c);
|
||||
}
|
||||
// Returns the float number from the parsed input
|
||||
fn get(&mut self) -> f64 {
|
||||
let n = self.s.parse::<f64>().ok().expect("number is malformed");
|
||||
self.s = String::new();
|
||||
|
||||
n
|
||||
}
|
||||
// Returns the length of the current save number as chars.
|
||||
fn len(&self) -> usize {
|
||||
self.s.len()
|
||||
}
|
||||
}
|
||||
|
||||
// Takes as argument a string representing the wanted calculation and return a list of symbols and values.
|
||||
fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
|
||||
let mut symbols = Vec::new();
|
||||
let mut numbers = Vec::new();
|
||||
|
||||
let mut number = TmpNum::new();
|
||||
|
||||
// Iteration of the chars
|
||||
let mut i = 0;
|
||||
while i < pattern.len() {
|
||||
let c = pattern.chars().nth(i).expect("some char");
|
||||
// Check if the given char is a recognized symbol
|
||||
let s: Option<Symbol> = match c {
|
||||
'-' => Some(Symbol::Minus),
|
||||
'+' => Some(Symbol::Plus),
|
||||
'*' => Some(Symbol::Multiply),
|
||||
'x' => Some(Symbol::Multiply), // This is the second way to express multiplication
|
||||
'/' => Some(Symbol::Divide),
|
||||
|
||||
'(' => Some(Symbol::OpenBracket),
|
||||
')' => Some(Symbol::CloseBracket),
|
||||
|
||||
_ => None,
|
||||
};
|
||||
|
||||
match s {
|
||||
// If the char is a recognized symbol
|
||||
Some(s) => {
|
||||
// If the char is a bracket
|
||||
if s.bracket() {
|
||||
// Keep track of the number of brackets
|
||||
let mut nb_bracket = 1;
|
||||
// Keep track of the new position into the list
|
||||
let mut i2 = i + 1;
|
||||
// Loop the rest of the input
|
||||
while i2 < pattern.len() {
|
||||
let c2 = pattern.chars().nth(i2).expect("some char");
|
||||
// Recognize the brackets from the rest of the input
|
||||
let s2: Option<Symbol> = match c2 {
|
||||
'(' => Some(Symbol::OpenBracket),
|
||||
')' => Some(Symbol::CloseBracket),
|
||||
|
||||
_ => None,
|
||||
};
|
||||
match s2 {
|
||||
// If brackets
|
||||
Some(s2) => {
|
||||
// If open add the increment
|
||||
if s2.open_bracket() {
|
||||
nb_bracket += 1;
|
||||
// If close decrease the counter and check if it gets to zero
|
||||
} else if s2.close_bracket() {
|
||||
nb_bracket -= 1;
|
||||
if nb_bracket == 0 {
|
||||
// Save the content of the brackets
|
||||
let new_pattern = pattern
|
||||
.get(i..i2)
|
||||
.expect("expect the sub string")
|
||||
.to_string();
|
||||
// Send it to the parse function
|
||||
let (tmp_symbols, tmp_numbers) = parse(new_pattern);
|
||||
// Do the calculation
|
||||
let tmp_result = calculation(tmp_symbols, tmp_numbers);
|
||||
// Add the calculated value to the list of numbers
|
||||
numbers.push(tmp_result);
|
||||
// Change the position in the list
|
||||
i = i2;
|
||||
|
||||
// Check the symbol after the closing bracket
|
||||
let c3 = match pattern.chars().nth(i2 + 1){
|
||||
Some(c) => c,
|
||||
None => break,
|
||||
};
|
||||
if !c3.is_numeric() {
|
||||
let s2 = match c3 {
|
||||
'-' => Some(Symbol::Minus),
|
||||
'+' => Some(Symbol::Plus),
|
||||
'*' => Some(Symbol::Multiply),
|
||||
'/' => Some(Symbol::Divide),
|
||||
|
||||
_ => None,
|
||||
};
|
||||
match s2 {
|
||||
Some(s2) => {
|
||||
symbols.push(s2);
|
||||
i += 1;
|
||||
}
|
||||
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
// This will be used in case of bracket directly followed by number.
|
||||
None => {
|
||||
symbols.push(Symbol::Multiply);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Not a bracket, do nothing
|
||||
None => {}
|
||||
}
|
||||
// Add counter at the end of the loop
|
||||
i2 += 1;
|
||||
}
|
||||
// If not a bracket but symbol
|
||||
} else {
|
||||
// If the length of number is zero, add to the current char to number
|
||||
// It's in the case of signed numbers
|
||||
if number.s.len() == 0 {
|
||||
number.push(c);
|
||||
// Otherwise add the number and the symbol
|
||||
} else {
|
||||
symbols.push(s);
|
||||
numbers.push(number.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// It's not a recognized symbol so it must be part of a number
|
||||
number.push(c);
|
||||
}
|
||||
}
|
||||
|
||||
// Increment the list position
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// At the end the last number need to be added too.
|
||||
if number.len() != 0 {
|
||||
numbers.push(number.get());
|
||||
}
|
||||
|
||||
// Return the values
|
||||
(symbols, numbers)
|
||||
}
|
||||
|
||||
// Run the calculation based on the 2 different lists of symbols and numbers
|
||||
fn calculation(symbols: Vec<Symbol>, numbers: Vec<f64>) -> f64 {
|
||||
let mut counter = 0;
|
||||
let mut n1 = *numbers.get(0).expect("expect at least the first value");
|
||||
|
||||
// Iterate the list of symbols
|
||||
|
||||
while counter < symbols.len() {
|
||||
let s = symbols
|
||||
.get(counter)
|
||||
.expect("expect at least a symbol value");
|
||||
let mut n2 = *numbers
|
||||
.get(counter + 1)
|
||||
.expect("expect at least a second value");
|
||||
|
||||
// If the symbol has priority then simply do the calculation and update n1
|
||||
if s.priority() {
|
||||
match s {
|
||||
Symbol::Multiply => {
|
||||
n1.mul_assign(n2);
|
||||
}
|
||||
Symbol::Divide => {
|
||||
n1.div_assign(n2);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
} else {
|
||||
// Check the next symbol
|
||||
let next_s = symbols.get(counter + 1);
|
||||
match next_s {
|
||||
Some(ss) => {
|
||||
// If the next has priority start to check until where the priority calculation goes
|
||||
if ss.priority() {
|
||||
let mut counter_2 = counter + 1;
|
||||
let mut nn1 = *numbers
|
||||
.get(counter_2)
|
||||
.expect("during the priority, we expect at least the first value");
|
||||
while counter_2 < symbols.len() {
|
||||
let nn2 = *numbers
|
||||
.get(counter_2 + 1)
|
||||
.expect("during the priority, we expect at least the second value");
|
||||
|
||||
if ss.priority() {
|
||||
match ss {
|
||||
Symbol::Multiply => {
|
||||
nn1.mul_assign(nn2);
|
||||
}
|
||||
Symbol::Divide => {
|
||||
nn1.div_assign(nn2);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// End of the priority.
|
||||
// Change the position in the list, save the new value as n1 and stop the priority loop
|
||||
} else {
|
||||
counter = counter_2;
|
||||
n2 = nn1;
|
||||
break;
|
||||
}
|
||||
n2 = nn1;
|
||||
|
||||
// Increment the position in the list
|
||||
counter_2 += 1;
|
||||
counter = counter_2;
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
// Run the non prioritized calculation
|
||||
match s {
|
||||
Symbol::Minus => {
|
||||
n1.sub_assign(n2);
|
||||
}
|
||||
Symbol::Plus => {
|
||||
n1.add_assign(n2);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
counter += 1;
|
||||
}
|
||||
|
||||
n1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let pattern = match std::env::args().nth(1) {
|
||||
None => {
|
||||
println!("Input must be provided");
|
||||
process::exit(1)
|
||||
}
|
||||
Some(s) => s,
|
||||
};
|
||||
|
||||
let (symbols, numbers) = parse(pattern);
|
||||
|
||||
// println!("{:?}", symbols);
|
||||
// println!("{:?}", numbers);
|
||||
|
||||
if symbols.len() < 1 || symbols.len() + 1 != numbers.len() {
|
||||
println!(
|
||||
"The input is malformed {} {} {}",
|
||||
symbols.len() < 1,
|
||||
symbols.len() + 1 != numbers.len(),
|
||||
symbols.len()
|
||||
);
|
||||
process::exit(2);
|
||||
}
|
||||
|
||||
let res = calculation(symbols, numbers);
|
||||
println!("{res}");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{calculation, parse};
|
||||
|
||||
#[test]
|
||||
fn basics() {
|
||||
let (s, n) = parse("2+2".to_string());
|
||||
|
||||
assert_eq!(calculation(s, n), 4.0);
|
||||
let (s, n) = parse("4-9".to_string());
|
||||
assert_eq!(calculation(s, n), -5.0);
|
||||
let (s, n) = parse("7*3".to_string());
|
||||
assert_eq!(calculation(s, n), 21.0);
|
||||
let (s, n) = parse("9/3".to_string());
|
||||
assert_eq!(calculation(s, n), 3.0);
|
||||
|
||||
let (s, n) = parse("15*20".to_string());
|
||||
assert_ne!(calculation(s, n), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signs_2() {
|
||||
let (s, n) = parse("2*4+4".to_string());
|
||||
assert_eq!(calculation(s, n), 12.0);
|
||||
let (s, n) = parse("2+2*4".to_string());
|
||||
assert_eq!(calculation(s, n), 10.0);
|
||||
let (s, n) = parse("30/4-9".to_string());
|
||||
assert_eq!(calculation(s, n), -1.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_number() {
|
||||
let (s, n) = parse("-2*4+4".to_string());
|
||||
assert_eq!(calculation(s, n), -4.0);
|
||||
let (s, n) = parse("-2+2*4".to_string());
|
||||
assert_eq!(calculation(s, n), 6.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_1_bracket_set() {
|
||||
let (s, n) = parse("2*(4+4)*2".to_string());
|
||||
assert_eq!(calculation(s, n), 32.0);
|
||||
let (s, n) = parse("2*(4+4)".to_string());
|
||||
assert_eq!(calculation(s, n), 16.0);
|
||||
let (s, n) = parse("-2*(4+4)".to_string());
|
||||
assert_eq!(calculation(s, n), -16.0);
|
||||
let (s, n) = parse("(4+4)*2".to_string());
|
||||
assert_eq!(calculation(s, n), 16.0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue