Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
2db19e7a46 |
3 changed files with 30 additions and 22 deletions
|
|
@ -49,20 +49,20 @@ pub fn pretty_eval(mut input: String, max_digits: usize) -> Result<String, Error
|
|||
}
|
||||
#[test]
|
||||
fn test_character_readdition() {
|
||||
assert_eq!(pretty_eval(" = 11".into(), 10).unwrap(), " = 11");
|
||||
assert_eq!(pretty_eval(" &= 11".into(), 10).unwrap(), " & = 11");
|
||||
assert_eq!(pretty_eval(" & 11".into(), 10).unwrap(), " & 11");
|
||||
assert_eq!(pretty_eval(" & 11 \\".into(), 10).unwrap(), " & 11 \\");
|
||||
assert_eq!(pretty_eval(" = 11".into()).unwrap(), " = 11");
|
||||
assert_eq!(pretty_eval(" &= 11".into()).unwrap(), " & = 11");
|
||||
assert_eq!(pretty_eval(" & 11".into()).unwrap(), " & 11");
|
||||
assert_eq!(pretty_eval(" & 11 \\".into()).unwrap(), " & 11 \\");
|
||||
}
|
||||
#[test]
|
||||
fn test_basic_up_to_999() {
|
||||
assert_eq!(pretty_eval("1".into(), 10).unwrap(), "1");
|
||||
assert_eq!(pretty_eval("10".into(), 10).unwrap(), "10");
|
||||
assert_eq!(pretty_eval("100".into(), 10).unwrap(), "100");
|
||||
assert_eq!(pretty_eval("999".into(), 10).unwrap(), "999");
|
||||
assert_eq!(pretty_eval("1000".into(), 10).unwrap(), "1 * 10^3");
|
||||
assert_eq!(pretty_eval("1".into()).unwrap(), "1");
|
||||
assert_eq!(pretty_eval("10".into()).unwrap(), "10");
|
||||
assert_eq!(pretty_eval("100".into()).unwrap(), "100");
|
||||
assert_eq!(pretty_eval("999".into()).unwrap(), "999");
|
||||
assert_eq!(pretty_eval("1000".into()).unwrap(), "1 * 10^3");
|
||||
|
||||
assert_eq!(pretty_eval("11.111111".into(), 10).unwrap(), "11.111111");
|
||||
assert_eq!(pretty_eval("11.111111".into()).unwrap(), "11.111111");
|
||||
}
|
||||
|
||||
pub fn get_significant_digits(n: &Float) -> usize {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ use std::{
|
|||
process::ExitCode,
|
||||
};
|
||||
|
||||
use tylc as lib;
|
||||
mod formatting;
|
||||
mod nodes;
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let input = match env::args().skip(1).rev().reduce(|x, a| a + &x) {
|
||||
|
|
@ -16,7 +17,7 @@ fn main() -> ExitCode {
|
|||
}
|
||||
};
|
||||
|
||||
match lib::formatting::pretty_eval(input, 10) {
|
||||
match formatting::pretty_eval(input, 10) {
|
||||
Ok(s) => println!("{s}"),
|
||||
|
||||
Err(e) => {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use core::f64;
|
||||
use std::str::FromStr;
|
||||
|
||||
use rug::{Float, float::ParseFloatError};
|
||||
use rug::{Float, float::ParseFloatError, ops::CompleteRound};
|
||||
|
||||
use crate::nodes::{Function, Node, Nodes, Operator};
|
||||
use crate::nodes::{Function, Node, Nodes, Operator, PREC};
|
||||
|
||||
impl Node {
|
||||
fn from_name(s: &str) -> Result<Self, String> {
|
||||
|
|
@ -124,10 +124,15 @@ impl FromStr for Nodes {
|
|||
&& !(c == 'e' && filtered_string.chars().nth(i + 1).is_some_and(|c| c == '-'))
|
||||
&& !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e'))
|
||||
{
|
||||
nodes.push(Node::from(
|
||||
Float::parse(&filtered_string[start_index..i]).map_err(|e| {
|
||||
Error::NumberParsing(filtered_string[start_index..i].to_owned(), e)
|
||||
})?,
|
||||
nodes.push(Node::Number(
|
||||
Float::parse(&filtered_string[start_index..i])
|
||||
.map_err(|e| {
|
||||
Error::NumberParsing(
|
||||
filtered_string[start_index..i].to_owned(),
|
||||
e,
|
||||
)
|
||||
})?
|
||||
.complete(PREC),
|
||||
));
|
||||
|
||||
state = ParsingState::Start;
|
||||
|
|
@ -177,10 +182,12 @@ impl FromStr for Nodes {
|
|||
if matches!(nodes.last(), Some(Node::ParenthesisEnd)) {
|
||||
nodes.push(Node::Operator(Default::default()));
|
||||
}
|
||||
nodes.push(Node::from(
|
||||
Float::parse(&filtered_string[start_index..i]).map_err(|e| {
|
||||
nodes.push(Node::Number(
|
||||
Float::parse(&filtered_string[start_index..i])
|
||||
.map_err(|e| {
|
||||
Error::NumberParsing(filtered_string[start_index..i].to_owned(), e)
|
||||
})?,
|
||||
})?
|
||||
.complete(PREC),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue