Last active
December 13, 2022 00:10
-
-
Save fatihgokce/7bc04eee92ea09a504af7f5151cd7244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug,Copy, Clone,PartialEq)] | |
enum Expression{ | |
Digit(u32), | |
Add, | |
Multi, | |
Divide, | |
Subs, | |
None | |
} | |
#[derive(Debug)] | |
struct Node{ | |
exp:Expression, | |
left:Option<Box<Node>>, | |
right:Option<Box<Node>> | |
} | |
use std::rc::Rc; | |
#[derive(Debug)] | |
struct Node3{ | |
exp:Expression, | |
left:Option<Rc<Node3>>, | |
right:Option<Rc<Node3>> | |
} | |
#[allow(unused)] | |
fn main() { | |
use std::mem; | |
let mut v: Vec<i32> = vec![1, 2]; | |
let old_v = mem::take(&mut v); | |
let mut expr="2*3+1";//"2+3*4+1*5*6";//"2+3*4+1*5";2+3*4+1*5*6 | |
let mut tree:Option<Box<Node>>=None; | |
//let mut chars = expr.chars().fuse(); | |
let h=expr.chars().collect::<Vec<char>>(); | |
let get_exp=|c:char|->Expression{ | |
return match c { | |
'+'=>Expression::Add, | |
'-'=>Expression::Subs, | |
'*'=>Expression::Multi, | |
'/'=>Expression::Divide, | |
_=>panic!("expression not correct"), | |
}; | |
}; | |
let mut anchor=&mut tree; | |
let mut pre_exp:Option<Expression>=None; | |
let mut current_exp:Option<Expression>=None; | |
let mut list_exp:Vec<Expression>=Vec::new(); | |
for c in h{ | |
if c==' '{ | |
continue; | |
} | |
//c.is_digit(10); | |
if c.is_digit(10){ | |
match anchor { | |
None=>{ | |
*anchor=Some(Box::new(Node{exp:Expression::Digit(c.to_digit(10).unwrap()),left:None,right:None})); | |
}, | |
Some(ref mut a)=>{ | |
//println!("a:{:?}",a); | |
if pre_exp==None || [Expression::Add,Expression::Subs].iter().any(|x|*x==current_exp.unwrap()){ | |
a.right=Some(Box::new(Node{exp:Expression::Digit(c.to_digit(10).unwrap()),left:None,right:None})); | |
}else{ | |
let mut right=&mut a.right; | |
//let mut temp_right=right; | |
while let Some(ref r)=right{ | |
//let a=&mut right.as_mut().unwrap().right; | |
match r.right { | |
Some(_)=>right=&mut right.as_mut().unwrap().right, | |
None=>break, | |
} | |
} | |
//println!("temp_right:{:?}",temp_right); | |
//let r=right.as_mut().unwrap(); | |
//r.right= | |
right.as_mut().unwrap().right=Some(Box::new(Node{exp:Expression::Digit(c.to_digit(10).unwrap()),left:None,right:None})); | |
} | |
}, | |
} | |
}else{ | |
let next_exp= get_exp(c); | |
list_exp.push(next_exp); | |
let parent=anchor.as_mut().unwrap(); | |
let left=&mut parent.left; | |
match left { | |
Some(_)=>{ | |
match next_exp { | |
Expression::Add | Expression::Subs=>{ | |
let temp=tree; | |
tree=Some(Box::new(Node{exp:next_exp,left:temp,right:None})); | |
anchor=&mut tree; | |
}, | |
Expression::Multi | Expression::Divide=>{ | |
let temp=parent.right.take(); | |
let new_node=Some(Box::new(Node{exp:next_exp,left:temp,right:None})); | |
parent.right=new_node; | |
}, | |
_=>println!("nothing done"), | |
} | |
}, | |
None=>{ | |
let before_exp=parent.exp; | |
parent.exp=next_exp; | |
*left=Some(Box::new(Node{exp:before_exp,left:None,right:None})); | |
} | |
} | |
pre_exp=current_exp; | |
current_exp=Some(next_exp); | |
} | |
} | |
println!("tree:{:?}",tree); | |
} | |
type Link = Option<Box<Node2>>; | |
struct Node2 { | |
next: Link, | |
} | |
struct Recursive { | |
root: Link, | |
} | |
impl Recursive { | |
fn back(&mut self) -> &mut Link { | |
let mut anchor = &mut self.root; | |
while let Some(node) = anchor { | |
anchor = &mut node.next; | |
} | |
anchor | |
} | |
fn back2(&mut self) -> &mut Link { | |
let mut anchor = &mut self.root; | |
loop { | |
let tmp = anchor; | |
if let Some(ref mut node) = *tmp { | |
anchor = &mut node.next; | |
} else { | |
anchor = tmp; | |
break; | |
} | |
} | |
anchor | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment