calculator.rs 7.8 KB

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