calculator.rs 7.6 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::html::input_data::keyboard_types::Key;
  7. use dioxus::prelude::*;
  8. use dioxus_desktop::{Config, WindowBuilder};
  9. fn main() {
  10. let config = Config::new().with_window(
  11. WindowBuilder::default()
  12. .with_title("Calculator")
  13. .with_inner_size(dioxus_desktop::LogicalSize::new(300.0, 500.0)),
  14. );
  15. dioxus_desktop::launch_cfg(app, config);
  16. }
  17. fn app(cx: Scope) -> Element {
  18. let val = use_state(&cx, || 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. cx.render(rsx!(
  52. style { include_str!("./assets/calculator.css") }
  53. div { id: "wrapper",
  54. div { class: "app",
  55. div { class: "calculator",
  56. onkeydown: handle_key_down_event,
  57. div { class: "calculator-display", val.to_string() }
  58. div { class: "calculator-keypad",
  59. div { class: "input-keys",
  60. div { class: "function-keys",
  61. button {
  62. class: "calculator-key key-clear",
  63. onclick: move |_| {
  64. val.set(String::new());
  65. if !val.is_empty(){
  66. val.set("0".into());
  67. }
  68. },
  69. if val.is_empty() { "C" } else { "AC" }
  70. }
  71. button {
  72. class: "calculator-key key-sign",
  73. onclick: move |_| {
  74. let temp = calc_val(val.as_str());
  75. if temp > 0.0 {
  76. val.set(format!("-{}", temp));
  77. } else {
  78. val.set(format!("{}", temp.abs()));
  79. }
  80. },
  81. "±"
  82. }
  83. button {
  84. class: "calculator-key key-percent",
  85. onclick: move |_| {
  86. val.set(
  87. format!("{}", calc_val(val.as_str()) / 100.0)
  88. );
  89. },
  90. "%"
  91. }
  92. }
  93. div { class: "digit-keys",
  94. button { class: "calculator-key key-0", onclick: move |_| input_digit(0),
  95. "0"
  96. }
  97. button { class: "calculator-key key-dot", onclick: move |_| val.make_mut().push('.'),
  98. "●"
  99. }
  100. (1..10).map(|k| rsx!{
  101. button {
  102. class: "calculator-key {k}",
  103. name: "key-{k}",
  104. onclick: move |_| input_digit(k),
  105. "{k}"
  106. }
  107. }),
  108. }
  109. }
  110. div { class: "operator-keys",
  111. button { class: "calculator-key key-divide", onclick: move |_| input_operator("/"),
  112. "÷"
  113. }
  114. button { class: "calculator-key key-multiply", onclick: move |_| input_operator("*"),
  115. "×"
  116. }
  117. button { class: "calculator-key key-subtract", onclick: move |_| input_operator("-"),
  118. "−"
  119. }
  120. button { class: "calculator-key key-add", onclick: move |_| input_operator("+"),
  121. "+"
  122. }
  123. button { class: "calculator-key key-equals",
  124. onclick: move |_| {
  125. val.set(format!("{}", calc_val(val.as_str())));
  126. },
  127. "="
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. ))
  135. }
  136. fn calc_val(val: &str) -> f64 {
  137. let mut temp = String::new();
  138. let mut operation = "+".to_string();
  139. let mut start_index = 0;
  140. let mut temp_value;
  141. let mut fin_index = 0;
  142. if &val[0..1] == "-" {
  143. temp_value = String::from("-");
  144. fin_index = 1;
  145. start_index += 1;
  146. } else {
  147. temp_value = String::from("");
  148. }
  149. for c in val[fin_index..].chars() {
  150. if c == '+' || c == '-' || c == '*' || c == '/' {
  151. break;
  152. }
  153. temp_value.push(c);
  154. start_index += 1;
  155. }
  156. let mut result = temp_value.parse::<f64>().unwrap();
  157. if start_index + 1 >= val.len() {
  158. return result;
  159. }
  160. for c in val[start_index..].chars() {
  161. if c == '+' || c == '-' || c == '*' || c == '/' {
  162. if !temp.is_empty() {
  163. match &operation as &str {
  164. "+" => result += temp.parse::<f64>().unwrap(),
  165. "-" => result -= temp.parse::<f64>().unwrap(),
  166. "*" => result *= temp.parse::<f64>().unwrap(),
  167. "/" => result /= temp.parse::<f64>().unwrap(),
  168. _ => unreachable!(),
  169. };
  170. }
  171. operation = c.to_string();
  172. temp = String::new();
  173. } else {
  174. temp.push(c);
  175. }
  176. }
  177. if !temp.is_empty() {
  178. match &operation as &str {
  179. "+" => result += temp.parse::<f64>().unwrap(),
  180. "-" => result -= temp.parse::<f64>().unwrap(),
  181. "*" => result *= temp.parse::<f64>().unwrap(),
  182. "/" => result /= temp.parse::<f64>().unwrap(),
  183. _ => unreachable!(),
  184. };
  185. }
  186. result
  187. }