feat(*): print result
This commit is contained in:
parent
b92bdf74ee
commit
5a305e1246
12
src/lib.rs
12
src/lib.rs
|
@ -2,10 +2,22 @@ use std::error::Error;
|
|||
use crate::parser::tokenizer::tokenize;
|
||||
use crate::parser::sanitizer::sanitize_tokens;
|
||||
use crate::parser::ast_builder::{build_ast, Node};
|
||||
use crate::maths::GaussianRational;
|
||||
|
||||
pub mod parser;
|
||||
pub mod maths;
|
||||
|
||||
|
||||
pub fn pretty(v: Vec<GaussianRational>) {
|
||||
v[0].print();
|
||||
for i in 1..v.len() {
|
||||
print!(" + ");
|
||||
v[i].print();
|
||||
print!("x^{i}");
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
pub fn parse(query: &str) -> Result<Node, Box<dyn Error>> {
|
||||
let tokens = tokenize(query)?;
|
||||
println!("{:?}", tokens);
|
||||
|
|
|
@ -8,4 +8,5 @@ fn main() {
|
|||
process::exit(1);
|
||||
});
|
||||
let evaluated = computorv1::maths::evaluator::evaluate(equation);
|
||||
computorv1::pretty(evaluated);
|
||||
}
|
||||
|
|
14
src/maths.rs
14
src/maths.rs
|
@ -38,6 +38,13 @@ impl Rational {
|
|||
Rational::new(self.numerator, self.denominator * 10)
|
||||
}
|
||||
|
||||
pub fn print(&self) {
|
||||
match (self.numerator, self.denominator) {
|
||||
(_, 1) => print!("{}", self.numerator),
|
||||
_ => print!("{}/{}", self.numerator, self.denominator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn simplify(lhs: Rational, rhs: Rational) -> Self {
|
||||
let res = Rational::new(lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
|
||||
res.reduce()
|
||||
|
@ -158,6 +165,13 @@ impl GaussianRational {
|
|||
pub fn conjugate(&self) -> Self {
|
||||
GaussianRational::new(self.real, -1 * self.imaginary)
|
||||
}
|
||||
|
||||
pub fn print(&self) {
|
||||
self.real.print();
|
||||
print!("+");
|
||||
self.imaginary.print();
|
||||
print!("i");
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Add<GaussianRational> for GaussianRational {
|
||||
|
|
Loading…
Reference in New Issue