main.nim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import nodesnim
  2. import strutils
  3. Window("Calc")
  4. var
  5. first: string = ""
  6. second: string = ""
  7. sign: string = ""
  8. proc number(self: ButtonRef, x, y: float) =
  9. if sign == "":
  10. first &= self.text
  11. else:
  12. second &= self.text
  13. proc equal(): string =
  14. let f = parseFloat(first)
  15. if second == "":
  16. return first
  17. result =
  18. case sign:
  19. of "+":
  20. $(f + parseFloat(second))
  21. of "-":
  22. $(f - parseFloat(second))
  23. of "x":
  24. $(f * parseFloat(second))
  25. of "/":
  26. $(f / parseFloat(second))
  27. else:
  28. first
  29. proc on_sign(self: ButtonRef, x, y: float) =
  30. if sign != "" and second != "":
  31. first = equal()
  32. second = ""
  33. sign = $self.text
  34. elif first != "":
  35. sign = $self.text
  36. build:
  37. - Scene main:
  38. - Vbox vbox:
  39. call setSizeAnchor(1, 1)
  40. call setChildAnchor(0.5, 0.5, 0.5, 0.5)
  41. - Label result:
  42. call setTextAlign(1, 0, 1, 0)
  43. call resize(160, 32)
  44. - GridBox buttons:
  45. call setRow(4)
  46. - Button button_1(text: stext"1", on_touch: number)
  47. - Button button_2(text: stext"2", on_touch: number)
  48. - Button button_3(text: stext"3", on_touch: number)
  49. - Button button_add(text: stext"+", on_touch: on_sign)
  50. - Button button_4(text: stext"4", on_touch: number)
  51. - Button button_5(text: stext"5", on_touch: number)
  52. - Button button_6(text: stext"6", on_touch: number)
  53. - Button button_sub(text: stext"-", on_touch: on_sign)
  54. - Button button_7(text: stext"7", on_touch: number)
  55. - Button button_8(text: stext"8", on_touch: number)
  56. - Button button_9(text: stext"9", on_touch: number)
  57. - Button button_mul(text: stext"x", on_touch: on_sign)
  58. - Button button_0(text: stext"0")
  59. - Button button_00(text: stext"00")
  60. - Button button_div(text: stext"/", on_touch: on_sign)
  61. - Button button_eq:
  62. text: stext"="
  63. button_0@on_touch(self, x, y):
  64. if sign == "" and first != "":
  65. first &= "0"
  66. elif second != "":
  67. second &= "0"
  68. button_00@on_touch(self, x, y):
  69. if sign == "" and first != "":
  70. first &= "00"
  71. elif second != "":
  72. second &= "00"
  73. button_eq@on_touch(self, x, y):
  74. first = equal()
  75. if sign != "":
  76. second = ""
  77. sign = ""
  78. result@on_process(self):
  79. if sign == "":
  80. result.setText(first)
  81. elif second == "":
  82. result.setText(first & " " & sign)
  83. else:
  84. result.setText(first & " " & sign & " " & second)
  85. addMainScene(main)
  86. windowLaunch()