From 7ec8ed6cdddc555ec9d824d58d17f06bf9aa3038 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Sat, 27 Apr 2019 22:22:20 +0530 Subject: refactor main.rs --- src/main.rs | 83 +++---------------------------------------------------------- 1 file changed, 3 insertions(+), 80 deletions(-) diff --git a/src/main.rs b/src/main.rs index 28af074..5846a87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,8 @@ mod parse; use crate::parse::*; mod error; use crate::error::{ CalcError, handler }; +mod format; +use crate::format::*; // extern crates use rustyline::error::ReadlineError; @@ -84,63 +86,6 @@ fn main() { } } -fn pprint(ans: f64) { - let ans_string = radix_fmt(ans, CONFIGURATION.base).unwrap(); - let ans_vector: Vec<&str> = ans_string.split(".").collect(); - match ans_vector.len() { - 1 => println!("{:>10}", thousand_sep(ans_vector[0])), - 2 => println!("{:>10}.{}", thousand_sep(ans_vector[0]),ans_vector[1]), - _ => () - } -} - -fn radix_fmt(number: f64, obase: usize) -> Result { - if obase > 36 { - return Err(CalcError::Syntax("Unknown base".into())); - } - - let table: Vec = "0123456789abcdefghijklmnopqrstuvwxyz".chars().collect(); - - // format integral part of float - let mut integral = number.trunc() as i64; - let mut obase_int = String::new(); - while integral >= obase as i64 { - obase_int.push(table[(integral % obase as i64) as usize]); - integral /= obase as i64; - } - obase_int.push(table[integral as usize]); - if number.is_sign_negative() { - obase_int.push('-'); - } - let obase_int = obase_int.chars().rev().collect::(); - - // format fractional part of float - let mut fract = number.abs().fract(); - let mut obase_fract = String::new(); - let mut i = 0; - loop { - fract *= obase as f64; - obase_fract.push(table[fract.trunc() as usize]); - i += 1; - if fract.fract() == 0. || i > 10 { - break; - } - fract = fract.fract(); - } - Ok(format!("{}.{}", obase_int, obase_fract)) -} - -fn thousand_sep(inp: &str) -> String { - let mut result_string = String::new(); - for (i,c) in inp.to_string().chars().rev().enumerate() { - if i % 3 == 0 && i != 0 && c.to_string() != "-" { - result_string.push(','); - } - result_string.push(c) - } - result_string.chars().rev().collect::() -} - fn parse_arguments() -> Configuration { let config = App::new(env!("CARGO_PKG_NAME")) .version(env!("CARGO_PKG_VERSION")) @@ -185,28 +130,6 @@ fn parse_arguments() -> Configuration { } } -fn autobalance_parens(input: &str) -> Result { - let mut balanced = String::from(input); - let mut left_parens = 0; - let mut right_parens = 0; - for letter in input.chars() { - if letter == '(' { - left_parens += 1; - } else if letter == ')' { - right_parens += 1; - } - } - - if left_parens > right_parens { - let extras = ")".repeat(left_parens - right_parens); - balanced.push_str(&extras[..]); - Ok(balanced) - } else if left_parens < right_parens { - return Err(CalcError::Syntax("Mismatched parentheses!".into())) - } else { - Ok(balanced) - } -} fn eval_math_expression(input: &str) -> Result { let input = input.trim(); @@ -214,7 +137,7 @@ fn eval_math_expression(input: &str) -> Result { if input.len() == 0 { return Ok(0.) } - let input = autobalance_parens(&input[..])?; + let input = format::autobalance_parens(&input[..])?; let lexed = lexer(&input[..])?; let postfixed = to_postfix(lexed)?; let evaled = eval_postfix(postfixed)?; -- cgit v1.2.3