Compare commits
4 Commits
fa33d861be
...
327917e29a
Author | SHA1 | Date |
---|---|---|
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,249 @@
|
|||
use std::{
|
||||
ops::{AddAssign, DivAssign, MulAssign, SubAssign},
|
||||
process,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Symbol {
|
||||
Minus,
|
||||
Plus,
|
||||
Multiply,
|
||||
Divide,
|
||||
|
||||
OpenBracket,
|
||||
CloseBracket,
|
||||
}
|
||||
impl Symbol {
|
||||
fn priority(&self) -> bool {
|
||||
match self {
|
||||
Symbol::Multiply => true,
|
||||
Symbol::Divide => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
fn bracket(&self) -> bool {
|
||||
match self {
|
||||
Symbol::OpenBracket => true,
|
||||
Symbol::CloseBracket => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
fn open_bracket(&self) -> bool {
|
||||
match self {
|
||||
Symbol::OpenBracket => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
fn close_bracket(&self) -> bool {
|
||||
match self {
|
||||
Symbol::CloseBracket => true,
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TmpNum {
|
||||
s: String,
|
||||
}
|
||||
impl TmpNum {
|
||||
pub fn new() -> Self {
|
||||
Self { s: String::new() }
|
||||
}
|
||||
fn push(&mut self, c: char) {
|
||||
self.s.push(c);
|
||||
}
|
||||
fn get(&mut self) -> f64 {
|
||||
let n = self.s.parse::<f64>().ok().expect("number is malformed");
|
||||
self.s = String::new();
|
||||
|
||||
n
|
||||
}
|
||||
fn len(&self) -> usize {
|
||||
self.s.len()
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(pattern: String) -> (Vec<Symbol>, Vec<f64>) {
|
||||
let mut symbols = Vec::new();
|
||||
let mut numbers = Vec::new();
|
||||
|
||||
let mut number = TmpNum::new();
|
||||
|
||||
let mut i = 0;
|
||||
while i < pattern.len() {
|
||||
let c = pattern.chars().nth(i).expect("some char");
|
||||
let s: Option<Symbol> = match c {
|
||||
'-' => Some(Symbol::Minus),
|
||||
'+' => Some(Symbol::Plus),
|
||||
'*' => Some(Symbol::Multiply),
|
||||
'/' => Some(Symbol::Divide),
|
||||
|
||||
'(' => Some(Symbol::OpenBracket),
|
||||
')' => Some(Symbol::CloseBracket),
|
||||
|
||||
_ => None,
|
||||
};
|
||||
|
||||
match s {
|
||||
Some(s) => {
|
||||
if s.bracket() {
|
||||
let mut nb_bracket = 1;
|
||||
let mut i2 = i + 1;
|
||||
while i2 < pattern.len() {
|
||||
let c2 = pattern.chars().nth(i2).expect("some char");
|
||||
let s2: Option<Symbol> = match c2 {
|
||||
'(' => Some(Symbol::OpenBracket),
|
||||
')' => Some(Symbol::CloseBracket),
|
||||
|
||||
_ => None,
|
||||
};
|
||||
match s2 {
|
||||
Some(s2) => {
|
||||
if s2.bracket() {
|
||||
if s2.open_bracket() {
|
||||
nb_bracket += 1;
|
||||
} else if s2.close_bracket() {
|
||||
nb_bracket -= 1;
|
||||
if nb_bracket == 0 {
|
||||
let new_pattern = pattern
|
||||
.get(i..i2)
|
||||
.expect("expect the sub string")
|
||||
.to_string();
|
||||
let (tmp_symbols, tmp_numbers) = parse(new_pattern);
|
||||
let tmp_result =
|
||||
calculation(&tmp_symbols, &tmp_numbers);
|
||||
numbers.push(tmp_result);
|
||||
i = i2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
i2 += 1;
|
||||
}
|
||||
} else {
|
||||
if number.s.len() == 0 {
|
||||
number.push(c);
|
||||
} else {
|
||||
symbols.push(s);
|
||||
numbers.push(number.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
number.push(c);
|
||||
}
|
||||
}
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if number.len() != 0 {
|
||||
numbers.push(number.get());
|
||||
}
|
||||
|
||||
(symbols, 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");
|
||||
while counter < symbols.len() {
|
||||
let s = symbols
|
||||
.get(counter)
|
||||
.expect("expect at least a symbol value");
|
||||
let n2 = *numbers
|
||||
.get(counter + 1)
|
||||
.expect("expect at least a second value");
|
||||
|
||||
if s.priority() {
|
||||
match s {
|
||||
Symbol::Multiply => {
|
||||
n1.mul_assign(n2);
|
||||
}
|
||||
Symbol::Divide => {
|
||||
n1.div_assign(n2);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
} else {
|
||||
let next_s = symbols.get(counter + 1);
|
||||
match next_s {
|
||||
Some(ss) => {
|
||||
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)
|
||||
.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);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
counter = counter_2;
|
||||
n1 = nn1;
|
||||
break;
|
||||
}
|
||||
|
||||
counter_2 += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
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");
|
||||
process::exit(2);
|
||||
}
|
||||
|
||||
let res = calculation(&symbols, &numbers);
|
||||
println!("{res}");
|
||||
}
|
Loading…
Reference in New Issue