calculator.rs 7.7 KB

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