From 347a11c8c4364c55c0097f89f4d11ee2859d64be Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sat, 8 Jan 2022 00:16:28 +0800 Subject: [PATCH] fix: format example code --- examples/calculator.rs | 146 ++++++++++++++++++++++------------------- 1 file changed, 79 insertions(+), 67 deletions(-) diff --git a/examples/calculator.rs b/examples/calculator.rs index 9525a076d..3d119c75d 100644 --- a/examples/calculator.rs +++ b/examples/calculator.rs @@ -17,74 +17,8 @@ fn main() { }); } -fn calc_val(val: String) -> f64 { - - // println!("{:?}", val); - - let mut result; - let mut temp = String::new(); - let mut operation = "+".to_string(); - - let mut start_index = 0; - let mut temp_value; - let mut fin_index = 0; - if &val[0..1] == "-" { - temp_value = String::from("-"); - fin_index = 1; - start_index += 1; - } else { - temp_value = String::from(""); - } - for c in val[fin_index..].chars() { - if c == '+' || c == '-' || c == '*' || c == '/' { - break; - } - temp_value.push(c); - start_index += 1; - } - result = temp_value.parse::().unwrap(); - - if start_index + 1 >= val.len() { - return result; - } - - for c in val[start_index..].chars() { - - // println!("{:?}", c); - - if c == '+' || c == '-' || c == '*' || c == '/' { - if temp != "" { - match &operation as &str { - "+" => {result += temp.parse::().unwrap();}, - "-" => {result -= temp.parse::().unwrap();}, - "*" => {result *= temp.parse::().unwrap();}, - "/" => {result /= temp.parse::().unwrap();}, - _ => unreachable!(), - }; - } - operation = c.to_string(); - temp = String::new(); - } else { - temp.push(c); - } - } - - if temp != "" { - match &operation as &str { - "+" => {result += temp.parse::().unwrap();}, - "-" => {result -= temp.parse::().unwrap();}, - "*" => {result *= temp.parse::().unwrap();}, - "/" => {result /= temp.parse::().unwrap();}, - _ => unreachable!(), - }; - } - - result -} - fn app(cx: Scope) -> Element { - - let display_value: UseState = use_state(&cx,|| String::from("0")); + let display_value: UseState = use_state(&cx, || String::from("0")); let input_digit = move |num: u8| { if display_value.get() == "0" { @@ -208,3 +142,81 @@ fn app(cx: Scope) -> Element { )) } + +fn calc_val(val: String) -> f64 { + + let mut result; + let mut temp = String::new(); + let mut operation = "+".to_string(); + + let mut start_index = 0; + let mut temp_value; + let mut fin_index = 0; + + if &val[0..1] == "-" { + temp_value = String::from("-"); + fin_index = 1; + start_index += 1; + } else { + temp_value = String::from(""); + } + + for c in val[fin_index..].chars() { + if c == '+' || c == '-' || c == '*' || c == '/' { + break; + } + temp_value.push(c); + start_index += 1; + } + result = temp_value.parse::().unwrap(); + + if start_index + 1 >= val.len() { + return result; + } + + for c in val[start_index..].chars() { + if c == '+' || c == '-' || c == '*' || c == '/' { + if temp != "" { + match &operation as &str { + "+" => { + result += temp.parse::().unwrap(); + } + "-" => { + result -= temp.parse::().unwrap(); + } + "*" => { + result *= temp.parse::().unwrap(); + } + "/" => { + result /= temp.parse::().unwrap(); + } + _ => unreachable!(), + }; + } + operation = c.to_string(); + temp = String::new(); + } else { + temp.push(c); + } + } + + if temp != "" { + match &operation as &str { + "+" => { + result += temp.parse::().unwrap(); + } + "-" => { + result -= temp.parse::().unwrap(); + } + "*" => { + result *= temp.parse::().unwrap(); + } + "/" => { + result /= temp.parse::().unwrap(); + } + _ => unreachable!(), + }; + } + + result +}