main.nim 2.3 KB

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