fix: calculator display
This commit is contained in:
1 parent
11172db7f6
commit
0742deed82
1 file changed
+49
-19
+49
-19
@@ -5,7 +5,6 @@ This calculator version uses React-style state management. All state is held as
|
|||||||
|
|
||||||
use dioxus::events::*;
|
use dioxus::events::*;
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use separator::Separatable;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use dioxus::desktop::tao::dpi::LogicalSize;
|
use dioxus::desktop::tao::dpi::LogicalSize;
|
||||||
@@ -20,22 +19,49 @@ fn main() {
|
|||||||
|
|
||||||
fn calc_val(val: String) -> f64 {
|
fn calc_val(val: String) -> f64 {
|
||||||
|
|
||||||
let mut result = 0.0_f64;
|
// println!("{:?}", val);
|
||||||
|
|
||||||
|
let mut result;
|
||||||
let mut temp = String::new();
|
let mut temp = String::new();
|
||||||
let mut operation = "+".to_string();
|
let mut operation = "+".to_string();
|
||||||
|
|
||||||
for c in val.chars() {
|
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::<f64>().unwrap();
|
||||||
|
|
||||||
println!("{:?}: {:?} - {:?}", result, c, val);
|
if start_index + 1 >= val.len() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for c in val[start_index..].chars() {
|
||||||
|
|
||||||
|
// println!("{:?}", c);
|
||||||
|
|
||||||
if c == '+' || c == '-' || c == '*' || c == '/' {
|
if c == '+' || c == '-' || c == '*' || c == '/' {
|
||||||
match &operation as &str {
|
if temp != "" {
|
||||||
"+" => {result += temp.parse::<f64>().unwrap();},
|
match &operation as &str {
|
||||||
"-" => {result -= temp.parse::<f64>().unwrap();},
|
"+" => {result += temp.parse::<f64>().unwrap();},
|
||||||
"*" => {result *= temp.parse::<f64>().unwrap();},
|
"-" => {result -= temp.parse::<f64>().unwrap();},
|
||||||
"/" => {result /= temp.parse::<f64>().unwrap();},
|
"*" => {result *= temp.parse::<f64>().unwrap();},
|
||||||
_ => unreachable!(),
|
"/" => {result /= temp.parse::<f64>().unwrap();},
|
||||||
};
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
}
|
||||||
operation = c.to_string();
|
operation = c.to_string();
|
||||||
temp = String::new();
|
temp = String::new();
|
||||||
} else {
|
} else {
|
||||||
@@ -58,10 +84,12 @@ fn calc_val(val: String) -> f64 {
|
|||||||
|
|
||||||
fn app(cx: Scope) -> Element {
|
fn app(cx: Scope) -> Element {
|
||||||
|
|
||||||
let cur_val = use_state(&cx, || 0.0_f64);
|
let display_value: UseState<String> = use_state(&cx,|| String::from("0"));
|
||||||
let display_value: UseState<String> = use_state(&cx,String::new);
|
|
||||||
|
|
||||||
let input_digit = move |num: u8| {
|
let input_digit = move |num: u8| {
|
||||||
|
if display_value.get() == "0" {
|
||||||
|
display_value.set(String::new());
|
||||||
|
}
|
||||||
display_value.modify().push_str(num.to_string().as_str());
|
display_value.modify().push_str(num.to_string().as_str());
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -96,7 +124,7 @@ fn app(cx: Scope) -> Element {
|
|||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
div { class: "calculator-display", [cur_val.separated_string()] }
|
div { class: "calculator-display", [display_value.to_string()] }
|
||||||
div { class: "calculator-keypad",
|
div { class: "calculator-keypad",
|
||||||
div { class: "input-keys",
|
div { class: "input-keys",
|
||||||
div { class: "function-keys",
|
div { class: "function-keys",
|
||||||
@@ -105,7 +133,7 @@ fn app(cx: Scope) -> Element {
|
|||||||
onclick: move |_| {
|
onclick: move |_| {
|
||||||
display_value.set(String::new());
|
display_value.set(String::new());
|
||||||
if display_value != "" {
|
if display_value != "" {
|
||||||
cur_val.set(0.0);
|
display_value.set("0".into());
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[if display_value == "" { "C" } else { "AC" }]
|
[if display_value == "" { "C" } else { "AC" }]
|
||||||
@@ -115,9 +143,9 @@ fn app(cx: Scope) -> Element {
|
|||||||
onclick: move |_| {
|
onclick: move |_| {
|
||||||
let temp = calc_val(display_value.get().clone());
|
let temp = calc_val(display_value.get().clone());
|
||||||
if temp > 0.0 {
|
if temp > 0.0 {
|
||||||
cur_val.set(temp - (temp * 2.0));
|
display_value.set(format!("-{}", temp));
|
||||||
} else {
|
} else {
|
||||||
cur_val.set(temp);
|
display_value.set(format!("{}", temp.abs()));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"±"
|
"±"
|
||||||
@@ -125,7 +153,9 @@ fn app(cx: Scope) -> Element {
|
|||||||
button {
|
button {
|
||||||
class: "calculator-key key-percent",
|
class: "calculator-key key-percent",
|
||||||
onclick: move |_| {
|
onclick: move |_| {
|
||||||
cur_val.set(calc_val(display_value.get().clone()) / 100.0);
|
display_value.set(
|
||||||
|
format!("{}", calc_val(display_value.get().clone()) / 100.0)
|
||||||
|
);
|
||||||
},
|
},
|
||||||
"%"
|
"%"
|
||||||
}
|
}
|
||||||
@@ -166,7 +196,7 @@ fn app(cx: Scope) -> Element {
|
|||||||
}
|
}
|
||||||
button { class: "calculator-key key-equals",
|
button { class: "calculator-key key-equals",
|
||||||
onclick: move |_| {
|
onclick: move |_| {
|
||||||
cur_val.set(calc_val(display_value.get().clone()));
|
display_value.set(format!("{}", calc_val(display_value.get().clone())));
|
||||||
},
|
},
|
||||||
"="
|
"="
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user