main.nim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 setChildAnchor(0.5, 0.5, 0.5, 0.5)
  40. call setSizeAnchor(1, 1)
  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_7(text: stext"7", on_touch: number)
  47. - Button button_8(text: stext"8", on_touch: number)
  48. - Button button_9(text: stext"9", on_touch: number)
  49. - Button button_4(text: stext"4", on_touch: number)
  50. - Button button_5(text: stext"5", on_touch: number)
  51. - Button button_6(text: stext"6", on_touch: number)
  52. - Button button_1(text: stext"1", on_touch: number)
  53. - Button button_2(text: stext"2", on_touch: number)
  54. - Button button_3(text: stext"3", on_touch: number)
  55. - Button button_0(text: stext"0")
  56. - Button button_00(text: stext"00")
  57. - Button button_add(text: stext"+", on_touch: on_sign)
  58. # Signs
  59. - Button button_sub(text: stext"-", on_touch: on_sign)
  60. - Button button_mul(text: stext"x", on_touch: on_sign)
  61. - Button button_div(text: stext"/", on_touch: on_sign)
  62. - Button button_eq:
  63. text: stext"="
  64. button_0@on_touch(self, x, y):
  65. if sign == "" and first != "":
  66. first &= "0"
  67. elif second != "":
  68. second &= "0"
  69. button_00@on_touch(self, x, y):
  70. if sign == "" and first != "":
  71. first &= "00"
  72. elif second != "":
  73. second &= "00"
  74. button_eq@on_touch(self, x, y):
  75. first = equal()
  76. if sign != "":
  77. second = ""
  78. sign = ""
  79. result@on_process(self):
  80. if sign == "":
  81. result.setText(first)
  82. elif second == "":
  83. result.setText(first & " " & sign)
  84. else:
  85. result.setText(first & " " & sign & " " & second)
  86. addMainScene(main)
  87. windowLaunch()