calculator.rs 8.1 KB

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