diff options
Diffstat (limited to 'src/format')
-rw-r--r-- | src/format/mod.rs | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/src/format/mod.rs b/src/format/mod.rs index 3e3ade2..45673d7 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs | |||
@@ -1,12 +1,8 @@ | |||
1 | extern crate num; | 1 | extern crate num; |
2 | use num::{BigInt, FromPrimitive, ToPrimitive}; | 2 | use num::{BigInt, FromPrimitive, ToPrimitive}; |
3 | 3 | ||
4 | use crate::error::{CalcError, Math}; | ||
4 | use crate::CONFIGURATION; | 5 | use crate::CONFIGURATION; |
5 | use crate::error::{ | ||
6 | CalcError, | ||
7 | Math | ||
8 | }; | ||
9 | |||
10 | 6 | ||
11 | pub fn autobalance_parens(input: &str) -> Result<String, CalcError> { | 7 | pub fn autobalance_parens(input: &str) -> Result<String, CalcError> { |
12 | let mut balanced = String::from(input); | 8 | let mut balanced = String::from(input); |
@@ -25,7 +21,7 @@ pub fn autobalance_parens(input: &str) -> Result<String, CalcError> { | |||
25 | balanced.push_str(&extras[..]); | 21 | balanced.push_str(&extras[..]); |
26 | Ok(balanced) | 22 | Ok(balanced) |
27 | } else if left_parens < right_parens { | 23 | } else if left_parens < right_parens { |
28 | return Err(CalcError::Syntax("Mismatched parentheses!".into())) | 24 | Err(CalcError::Syntax("Mismatched parentheses!".into())) |
29 | } else { | 25 | } else { |
30 | Ok(balanced) | 26 | Ok(balanced) |
31 | } | 27 | } |
@@ -39,10 +35,10 @@ fn radix_fmt(number: f64, obase: usize) -> Result<String, CalcError> { | |||
39 | match (number.is_infinite(), number.is_sign_positive()) { | 35 | match (number.is_infinite(), number.is_sign_positive()) { |
40 | (true, true) => return Ok("inf".to_string()), | 36 | (true, true) => return Ok("inf".to_string()), |
41 | (true, false) => return Ok("-inf".to_string()), | 37 | (true, false) => return Ok("-inf".to_string()), |
42 | _ => () | 38 | _ => (), |
43 | } | 39 | } |
44 | 40 | ||
45 | if number.is_nan(){ | 41 | if number.is_nan() { |
46 | return Ok("nan".to_string()); | 42 | return Ok("nan".to_string()); |
47 | } | 43 | } |
48 | 44 | ||
@@ -53,7 +49,7 @@ fn radix_fmt(number: f64, obase: usize) -> Result<String, CalcError> { | |||
53 | let mut obase_int = String::new(); | 49 | let mut obase_int = String::new(); |
54 | let obaseb = BigInt::from_usize(obase).unwrap(); | 50 | let obaseb = BigInt::from_usize(obase).unwrap(); |
55 | 51 | ||
56 | while &integral >= &obaseb { | 52 | while integral >= obaseb { |
57 | obase_int.push(table[(&integral % &obaseb).to_usize().unwrap()]); | 53 | obase_int.push(table[(&integral % &obaseb).to_usize().unwrap()]); |
58 | integral /= &obaseb; | 54 | integral /= &obaseb; |
59 | } | 55 | } |
@@ -81,7 +77,7 @@ fn radix_fmt(number: f64, obase: usize) -> Result<String, CalcError> { | |||
81 | 77 | ||
82 | fn thousand_sep(inp: &str) -> String { | 78 | fn thousand_sep(inp: &str) -> String { |
83 | let mut result_string = String::new(); | 79 | let mut result_string = String::new(); |
84 | for (i,c) in inp.to_string().chars().rev().enumerate() { | 80 | for (i, c) in inp.to_string().chars().rev().enumerate() { |
85 | if i % 3 == 0 && i != 0 && c.to_string() != "-" { | 81 | if i % 3 == 0 && i != 0 && c.to_string() != "-" { |
86 | result_string.push(','); | 82 | result_string.push(','); |
87 | } | 83 | } |
@@ -92,11 +88,10 @@ fn thousand_sep(inp: &str) -> String { | |||
92 | 88 | ||
93 | pub fn pprint(ans: f64) { | 89 | pub fn pprint(ans: f64) { |
94 | let ans_string = radix_fmt(ans, CONFIGURATION.base).unwrap(); | 90 | let ans_string = radix_fmt(ans, CONFIGURATION.base).unwrap(); |
95 | let ans_vector: Vec<&str> = ans_string.split(".").collect(); | 91 | let ans_vector: Vec<&str> = ans_string.split('.').collect(); |
96 | match ans_vector.len() { | 92 | match ans_vector.len() { |
97 | 1 => println!("{:>10}", thousand_sep(ans_vector[0])), | 93 | 1 => println!("{:>10}", thousand_sep(ans_vector[0])), |
98 | 2 => println!("{:>10}.{}", thousand_sep(ans_vector[0]),ans_vector[1]), | 94 | 2 => println!("{:>10}.{}", thousand_sep(ans_vector[0]), ans_vector[1]), |
99 | _ => unreachable!("N-nani?!") | 95 | _ => unreachable!("N-nani?!"), |
100 | } | 96 | } |
101 | } | 97 | } |
102 | |||