Created
February 17, 2023 13:15
-
-
Save SwannHERRERA/c16506fec4bc850c0d4a4c3af671fbad to your computer and use it in GitHub Desktop.
This program takes a file name as argument and an operation (+, - or *) it parses the file in this file, each line of the file should hopefully have a valid number it should take each number and print the operation, along with the intermediary result it should print at the end the total result of the defined operation applied to all numbers fou…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
pub enum Operator { | |
Add, | |
Subtract, | |
Multiply, | |
Divide, | |
} | |
// TODO create an object that store state | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
let (content, operator) = io::parse_argument(args); | |
let numbers = convert_file_content_to_list_of_number(content); | |
let summary = build_summary(numbers, &operator); | |
io::display_summary(&summary, &operator); | |
} | |
fn convert_file_content_to_list_of_number(file_content: String) -> Vec<usize> { | |
file_content | |
.lines() | |
.map(|line| line.parse().expect("line should contain strings")) | |
.collect() | |
} | |
fn build_summary(numbers: Vec<usize>, operator: &Operator) -> Vec<usize> { | |
let mut sum: usize = 0; | |
numbers.iter().map(|num| { | |
match operator { | |
Operator::Add => sum += num, | |
Operator::Subtract => sum -= num, | |
Operator::Multiply => sum *= num, | |
Operator::Divide => sum /= num, | |
} | |
sum | |
}).collect() | |
} | |
mod io { | |
use std::{fs, str::FromStr}; | |
use super::Operator; | |
pub fn parse_argument(args: Vec<String>) -> (String, Operator) { | |
if args.len() != 2 { | |
panic!("program should have 2 parameters : 1 is filename and 2 is operator"); | |
} | |
let file_content = fs::read_to_string(args[0].clone()).expect("file not found"); | |
let operator = Operator::from_str(&args[1]).expect("Unknow Operator"); | |
(file_content, operator) | |
} | |
impl FromStr for Operator { | |
type Err = String; | |
fn from_str(s: &str) -> Result<Self, Self::Err> { | |
match s { | |
"+" => Ok(Operator::Add), | |
"-" => Ok(Operator::Subtract), | |
"*" => Ok(Operator::Multiply), | |
"/" => Ok(Operator::Divide), | |
_ => unreachable!(), | |
} | |
} | |
} | |
// not finish | |
pub fn display_summary(summary: &[usize], operator: &Operator) { | |
if summary.len() == 0 { | |
println!("total = 0"); | |
} | |
summary.iter().for_each(|number| println!("{number}")); | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
const FILE_CONTENT_EXAMPLE: &str = "1 | |
2 | |
3"; | |
#[test] | |
fn test_result_with_basic_file() { | |
const EXPECTED_RESULT: usize = 6; | |
let numbers = convert_file_content_to_list_of_number(FILE_CONTENT_EXAMPLE.to_string()); | |
let sum: usize = numbers.iter().sum(); | |
assert_eq!(sum, EXPECTED_RESULT); | |
} | |
#[test] | |
fn test_with_basic_file() { | |
const EXPECTED_RESULT: &str = "1 | |
+2 (=3) | |
+3 (=6) | |
------ | |
total = 6 (addition"; | |
let numbers = convert_file_content_to_list_of_number(FILE_CONTENT_EXAMPLE.to_string()); | |
// let sum; | |
// assert_eq!(sum, EXPECTED_RESULT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment