calculator.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. This example is a simple iOS-style calculator. This particular example can run any platform - Web, Mobile, Desktop.
  3. This calculator version uses React-style state management. All state is held as individual use_states.
  4. */
  5. use dioxus::events::*;
  6. use dioxus::html::input_data::keyboard_types::Key;
  7. use dioxus::prelude::*;
  8. use dioxus_desktop::{Config, LogicalSize, WindowBuilder};
  9. fn main() {
  10. let config = Config::new().with_window(
  11. WindowBuilder::default()
  12. .with_title("Calculator")
  13. .with_inner_size(LogicalSize::new(300.0, 500.0)),
  14. );
  15. dioxus_desktop::launch_cfg(app, config);
  16. }
  17. fn app() -> Element {
  18. let val = use_signal(|| String::from("0"));
  19. let input_digit = move |num: u8| {
  20. if val.get() == "0" {
  21. val.set(String::new());
  22. }
  23. val.make_mut().push_str(num.to_string().as_str());
  24. };
  25. let input_operator = move |key: &str| val.make_mut().push_str(key);
  26. let handle_key_down_event = move |evt: KeyboardEvent| match evt.key() {
  27. Key::Backspace => {
  28. if !val.len() != 0 {
  29. val.make_mut().pop();
  30. }
  31. }
  32. Key::Character(character) => match character.as_str() {
  33. "+" => input_operator("+"),
  34. "-" => input_operator("-"),
  35. "/" => input_operator("/"),
  36. "*" => input_operator("*"),
  37. "0" => input_digit(0),
  38. "1" => input_digit(1),
  39. "2" => input_digit(2),
  40. "3" => input_digit(3),
  41. "4" => input_digit(4),
  42. "5" => input_digit(5),
  43. "6" => input_digit(6),
  44. "7" => input_digit(7),
  45. "8" => input_digit(8),
  46. "9" => input_digit(9),
  47. _ => {}
  48. },
  49. _ => {}
  50. };
  51. rsx! {
  52. style { {include_str!("./assets/calculator.css")} }
  53. div { id: "wrapper",
  54. div { class: "app",
  55. div { class: "calculator",
  56. tabindex: "0",
  57. onkeydown: handle_key_down_event,
  58. div { class: "calculator-display", "{val}" }
  59. div { class: "calculator-keypad",
  60. div { class: "input-keys",
  61. div { class: "function-keys",
  62. button {
  63. class: "calculator-key key-clear",
  64. onclick: move |_| {
  65. val.set(String::new());
  66. if !val.is_empty(){
  67. val.set("0".into());
  68. }
  69. },
  70. if val.is_empty() { "C" } else { "AC" }
  71. }
  72. button {
  73. class: "calculator-key key-sign",
  74. onclick: move |_| {
  75. let temp = calc_val(val.as_str());
  76. if temp > 0.0 {
  77. val.set(format!("-{temp}"));
  78. } else {
  79. val.set(format!("{}", temp.abs()));
  80. }
  81. },
  82. "±"
  83. }
  84. button {
  85. class: "calculator-key key-percent",
  86. onclick: move |_| {
  87. val.set(
  88. format!("{}", calc_val(val.as_str()) / 100.0)
  89. );
  90. },
  91. "%"
  92. }
  93. }
  94. div { class: "digit-keys",
  95. button { class: "calculator-key key-0", onclick: move |_| input_digit(0), "0" }
  96. button { class: "calculator-key key-dot", onclick: move |_| val.make_mut().push('.'), "●" }
  97. for k in 1..10 {
  98. button {
  99. class: "calculator-key {k}",
  100. name: "key-{k}",
  101. onclick: move |_| input_digit(k),
  102. "{k}"
  103. }
  104. }
  105. }
  106. }
  107. div { class: "operator-keys",
  108. button { class: "calculator-key key-divide", onclick: move |_| input_operator("/"), "÷" }
  109. button { class: "calculator-key key-multiply", onclick: move |_| input_operator("*"), "×" }
  110. button { class: "calculator-key key-subtract", onclick: move |_| input_operator("-"), "−" }
  111. button { class: "calculator-key key-add", onclick: move |_| input_operator("+"), "+" }
  112. button {
  113. class: "calculator-key key-equals",
  114. onclick: move |_| val.set(format!("{}", calc_val(val.as_str()))),
  115. "="
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. fn calc_val(val: &str) -> f64 {
  125. let mut temp = String::new();
  126. let mut operation = "+".to_string();
  127. let mut start_index = 0;
  128. let mut temp_value;
  129. let mut fin_index = 0;
  130. if &val[0..1] == "-" {
  131. temp_value = String::from("-");
  132. fin_index = 1;
  133. start_index += 1;
  134. } else {
  135. temp_value = String::from("");
  136. }
  137. for c in val[fin_index..].chars() {
  138. if c == '+' || c == '-' || c == '*' || c == '/' {
  139. break;
  140. }
  141. temp_value.push(c);
  142. start_index += 1;
  143. }
  144. let mut result = temp_value.parse::<f64>().unwrap();
  145. if start_index + 1 >= val.len() {
  146. return result;
  147. }
  148. for c in val[start_index..].chars() {
  149. if c == '+' || c == '-' || c == '*' || c == '/' {
  150. if !temp.is_empty() {
  151. match &operation as &str {
  152. "+" => result += temp.parse::<f64>().unwrap(),
  153. "-" => result -= temp.parse::<f64>().unwrap(),
  154. "*" => result *= temp.parse::<f64>().unwrap(),
  155. "/" => result /= temp.parse::<f64>().unwrap(),
  156. _ => unreachable!(),
  157. };
  158. }
  159. operation = c.to_string();
  160. temp = String::new();
  161. } else {
  162. temp.push(c);
  163. }
  164. }
  165. if !temp.is_empty() {
  166. match &operation as &str {
  167. "+" => result += temp.parse::<f64>().unwrap(),
  168. "-" => result -= temp.parse::<f64>().unwrap(),
  169. "*" => result *= temp.parse::<f64>().unwrap(),
  170. "/" => result /= temp.parse::<f64>().unwrap(),
  171. _ => unreachable!(),
  172. };
  173. }
  174. result
  175. }