diff --git a/src/calc.rs b/src/calc.rs index d2735bc..c8bea0c 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -1,31 +1,58 @@ -struct calcNode { +use std::io::Error; + +#[derive(Clone)] +struct CalcNode { item: Item, - left: Option>, - right: Option>, + left: Option>, + right: Option>, } -impl calcNode { - pub fn new(item: Item, left: Option>, right: Option>) -> Self { +impl CalcNode { + pub fn new(item: Item, left: Option>, right: Option>) -> Self { Self { item, left, right } } } +#[derive(Clone)] pub enum Item { Num(i32), Oper(Operation), } +#[derive(Clone)] pub enum Operation { Add, Sub, Div, Mult, } -pub struct calcTree { - root: calcNode, +pub struct CalcTree { + root: CalcNode, } -impl calcTree { +impl CalcTree { pub fn new(item: Item) -> Self { - let root = calcNode::new(item, None, None); + let root = CalcNode::new(item, None, None); Self { root } } + pub fn insert(&mut self, item: Item) { + use Item::*; + if let Oper(_) = item { + match &self.root.item { + Num(_) => self.root = CalcNode::new(item, Some(Box::new(self.root.clone())), None), + Oper(_) => panic!("fuck off"), + } + } else { + match &self.root.item { + Num(_) => panic!("fuck off"), + Oper(_) => { + if let None = self.root.right { + self.root.right = Some(Box::new(CalcNode::new(item, None, None))); + } else if let None = self.root.left { + self.root.left = Some(Box::new(CalcNode::new(item, None, None))); + } else { + self.insert(item) + } + } + } + } + } }