Author Topic: Calculator  (Read 7780 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculator
« Reply #15 on: July 29, 2020, 11:54:19 am »
Come to think of it - you may not need my icon stuff added at all.  Just do an _ICON _DISPLAY after drawing your calculator and that would look just as good.  Instant icon.  That would be a cool trick I suppose for showing game play in the task bar when playing games.

- Dav

This is interesting idea. I've never done this but I think you draw the calculator get an image of it and use it for an icon. Maybe when playing games you can constantly update the icon?

Sounds very interesting!

Ken those are nice improvements to your calculator, EXP usually goes with LOG and yes some of these advanced math operations need checks before you attempt them. A classic one is division by 0, don't attempt to divide if the divisor is 0!
« Last Edit: July 29, 2020, 11:57:24 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculator
« Reply #16 on: July 29, 2020, 12:11:07 pm »
Thanks B+. I can't seem to get SIN, COS, TAN to work with degrees though. Just by clicking SIN and then Rad2Deg doesn't give the right results as just SIN(degrees). I almost flunked out of Trig class, which was over 30 years ago, so does anyone have any suggestions? I keep SIN, COS, TAN though because the radians work fine. The funny part is, Rad2Deg works perfectly with Pi.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculator
« Reply #17 on: July 29, 2020, 12:18:36 pm »
When I use my regular calculator Radians/Degrees is a Toggle in Radians mode it put a little Radians up in corner when in Degrees mode a Degrees indication, you could do this by changing the name of button or just add the mode label in a corner so you know what mode you are in.

Anyway when you are in Radians Mode you do SIN, COS, TAN with VAL(num$) like SIN(VAL(num$))

In Degrees Mode you do SIN, COS, TAN with _D2R(VAL(num$)) so it's like SIN(_D2R(VAL(num$)))

If you decide to do ARC SIN, ARC COS, ARC TAN then you would show the result in the mode you are in (read as the Angle whose SIN = .xxxx) :

So if in Degrees mode for like ARC SIN: result =  _R2D(ARCSIN(VAL(num$)))
« Last Edit: July 29, 2020, 12:27:45 pm by bplus »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Calculator
« Reply #18 on: July 29, 2020, 01:07:25 pm »
Quote from: bplus
This is interesting idea. I've never done this but I think you draw the calculator get an image of it and use it for an icon. Maybe when playing games you can constantly update the icon?

By using _DISPLAY instead of an image, you can by pass getting an image at all - the _ICON will use the current program screen displayed.  Very easy to have an icon. I think I will be using this in future projects.

- Dav

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculator
« Reply #19 on: July 29, 2020, 01:38:47 pm »
By using _DISPLAY instead of an image, you can by pass getting an image at all - the _ICON will use the current program screen displayed.  Very easy to have an icon. I think I will be using this in future projects.

- Dav

Nice, no extra files at all!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Calculator
« Reply #20 on: July 29, 2020, 01:56:17 pm »
By using _DISPLAY instead of an image, you can by pass getting an image at all - the _ICON will use the current program screen displayed.  Very easy to have an icon. I think I will be using this in future projects.
I was unaware of this feature! Cool!
Shuwatch!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculator
« Reply #21 on: July 29, 2020, 03:49:27 pm »
Since B+ said I should add the EXP and showed me how to fix the SIN, COS, TAN with deg and rad modes, I decided to make 4 more buttons and change the rad2deg (and deg2rad) button to just show which mode you can change to next if you click it. B+ also mentioned that many calculators show which mode it's on up near the top, so I added that too. :) You can see the 4 new buttons on the picture below. I hope you all enjoy this as much as I have in programming it. Thanks to B+ and all of you guys for the inspiration and help. :))
Oh, one side note. I noticed QB64 uses "Inf" when you divide by 0 so I left it in there just to make it look like how calculators say similar things, like "E", etc. It also displays that when you make your exponent too high. I believe a power over 88 is too high for QB64 it said on the Wiki. So it just displays a "Inf", which stands for infinity.

Here is the update:

Code: QB64: [Select]
  1. 'Calculator
  2. 'By SierraKen on July 29, 2020.
  3. '------------------------------
  4. 'This is my very first regular calculator.
  5. 'Thank you to B+ and TempodiBasic for the help and everyone for the inspiration!
  6. 'This was a 4 day project and I've wanted to make this all my life. Ever since I was real little
  7. 'playing around with my parents' calculator.
  8. 'Thank you Dav for the icon code!
  9. 'Added Clipboard by clicking the output number.
  10. 'Added Rad2Deg (and Deg2Rad), second power, Base 10 Logarithm, and Pi buttons.
  11. 'Added 1/x, x/2, exp, postive or negative.
  12.  
  13. 'Load the icon - call this somewhere near the top of your program
  14. _ICON BASIMAGE1&
  15.  
  16. _TITLE "Calculator - Click output to copy."
  17. SCREEN _NEWIMAGE(400, 500, 32)
  18.  
  19. 'Setup calculator
  20. LINE (50, 25)-(265, 50), _RGB32(255, 255, 255), B
  21. LINE (50, 75)-(350, 350), _RGB32(255, 255, 255), B
  22. 'Buttons
  23. FOR buttony = 75 TO 405 STEP 55
  24.     FOR buttonx = 50 TO 275 STEP 75
  25.         FOR bb = 0 TO 10
  26.             c = c + 10
  27.             LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  28.         NEXT bb
  29.         PAINT (buttonx + 12, buttony + 12), _RGB32(100 + c, 100 + c, 100 + c)
  30.         c = 0
  31.     NEXT buttonx
  32. NEXT buttony
  33. buttonx = 275: buttony = 20
  34. 'Green C Button
  35. FOR bb = 0 TO 10
  36.     c = c + 10
  37.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  38. NEXT bb
  39. PAINT (buttonx + 12, buttony + 12), _RGB32(50, 100 + c, 50)
  40. COLOR _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
  41. _PRINTSTRING (50, 5), "Rad"
  42.  
  43. 'Label Buttons
  44. _PRINTSTRING (312, 47), "C"
  45. COLOR _RGB32(0, 0, 0), _RGB32(210, 210, 210)
  46. _PRINTSTRING (87, 102), CHR$(251) 'square root
  47. _PRINTSTRING (152, 102), "sin"
  48. _PRINTSTRING (227, 102), "cos"
  49. _PRINTSTRING (302, 102), "tan"
  50. _PRINTSTRING (87, 157), "7"
  51. _PRINTSTRING (162, 157), "8"
  52. _PRINTSTRING (237, 157), "9"
  53. _PRINTSTRING (312, 157), "/"
  54. _PRINTSTRING (87, 212), "4"
  55. _PRINTSTRING (162, 212), "5"
  56. _PRINTSTRING (237, 212), "6"
  57. _PRINTSTRING (312, 212), "x"
  58. _PRINTSTRING (87, 267), "1"
  59. _PRINTSTRING (162, 267), "2"
  60. _PRINTSTRING (237, 267), "3"
  61. _PRINTSTRING (312, 267), "-"
  62. _PRINTSTRING (87, 322), "0"
  63. _PRINTSTRING (162, 322), "."
  64. _PRINTSTRING (237, 322), "="
  65. _PRINTSTRING (312, 322), "+"
  66. _PRINTSTRING (79, 377), "deg"
  67. _PRINTSTRING (162, 377), "x" + CHR$(253) 'second power
  68. _PRINTSTRING (227, 377), "log"
  69. _PRINTSTRING (307, 377), "Pi"
  70. _PRINTSTRING (79, 432), "1/x"
  71. _PRINTSTRING (152, 432), "x/2"
  72. _PRINTSTRING (227, 432), "exp"
  73. _PRINTSTRING (312, 432), CHR$(241) 'Postive or Negative
  74.  
  75. COLOR _RGB32(255, 255, 255), _RGB32(0, 0, 0)
  76.     _LIMIT 20
  77.         mouseX = _MOUSEX
  78.         mouseY = _MOUSEY
  79.         mouseLeftButton = _MOUSEBUTTON(1)
  80.     LOOP
  81.     IF mouseLeftButton THEN
  82.         mouseLeftButton = 0
  83.  
  84.         'Clipboard
  85.         IF mouseX > 50 AND mouseX < 265 AND mouseY > 25 AND mouseY < 50 THEN
  86.             COLOR _RGB32(0, 255, 0), _RGB32(0, 0, 0)
  87.             _PRINTSTRING (55, 30), num$
  88.             _DELAY 1
  89.             num2$ = _TRIM$(num$)
  90.             _CLIPBOARD$ = num2$
  91.             COLOR _RGB32(255, 255, 255), _RGB32(0, 0, 0)
  92.             _PRINTSTRING (55, 30), num$
  93.         END IF
  94.  
  95.         'Clear
  96.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 20 AND mouseY < 75 THEN
  97.             a = 0: s = 0: t = 0: d = 0: num$ = ""
  98.             _PRINTSTRING (55, 30), "                          "
  99.             buttonx = 275: buttony = 20
  100.             GOSUB zero:
  101.         END IF
  102.  
  103.         '1/x
  104.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 405 AND mouseY < 460 THEN
  105.             num = VAL(num$)
  106.             IF num = 0 THEN GOTO skipthis:
  107.             num = 1 / num
  108.             num$ = STR$(num)
  109.             skipthis:
  110.             buttonx = 50: buttony = 405
  111.             GOSUB press:
  112.             GOSUB number:
  113.         END IF
  114.  
  115.         'x/2
  116.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 405 AND mouseY < 460 THEN
  117.             num = VAL(num$)
  118.             num = num / 2
  119.             num$ = STR$(num)
  120.             buttonx = 126: buttony = 405
  121.             GOSUB press:
  122.             GOSUB number2:
  123.         END IF
  124.  
  125.         'Exponents
  126.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 405 AND mouseY < 460 THEN
  127.             ee = 1: a = 0: s = 0: t = 0: d = 0
  128.             e = e + 1
  129.             IF e = 2 THEN GOTO expo:
  130.             oldnum$ = num$
  131.             _PRINTSTRING (55, 30), "                          "
  132.             num$ = ""
  133.             GOTO nextexpo:
  134.             expo:
  135.             e = 0
  136.             num = VAL(oldnum$) ^ VAL(num$)
  137.             ee = 0
  138.             over:
  139.             GOSUB number2:
  140.             nextexpo:
  141.             buttonx = 200: buttony = 405
  142.             GOSUB press:
  143.         END IF
  144.  
  145.         'Postive or Negative
  146.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 405 AND mouseY < 460 THEN
  147.             num$ = "-" + num$
  148.             buttonx = 275: buttony = 405
  149.             GOSUB press:
  150.             GOSUB number:
  151.         END IF
  152.  
  153.         'Radians and Degrees
  154.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 350 AND mouseY < 405 THEN
  155.             deg = deg + 1
  156.             IF deg = 2 THEN GOTO deg2rad:
  157.             COLOR _RGB32(0, 0, 0), _RGB32(210, 210, 210)
  158.             _PRINTSTRING (79, 377), "rad"
  159.             COLOR _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
  160.             _PRINTSTRING (50, 5), "Deg"
  161.             num = VAL(num$)
  162.             num = _R2D(num)
  163.             num$ = STR$(num)
  164.             GOTO skipdeg2rad:
  165.             deg2rad:
  166.             deg = 0
  167.             COLOR _RGB32(0, 0, 0), _RGB32(210, 210, 210)
  168.             _PRINTSTRING (79, 377), "deg"
  169.             COLOR _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
  170.             _PRINTSTRING (50, 5), "Rad"
  171.             num = VAL(num$)
  172.             num = _D2R(num)
  173.             num$ = STR$(num)
  174.             skipdeg2rad:
  175.             buttonx = 50: buttony = 350
  176.             GOSUB press:
  177.             GOSUB number:
  178.         END IF
  179.  
  180.         'Second Power
  181.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 350 AND mouseY < 405 THEN
  182.             num = VAL(num$)
  183.             num = num ^ 2
  184.             num$ = STR$(num)
  185.             buttonx = 126: buttony = 350
  186.             GOSUB press:
  187.             GOSUB number:
  188.         END IF
  189.  
  190.         'logarithm
  191.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 350 AND mouseY < 405 THEN
  192.             num = VAL(num$)
  193.             IF num < 0 THEN GOTO skiplog:
  194.             num = LOG(num) / LOG(10.#)
  195.             num$ = STR$(num)
  196.             skiplog:
  197.             buttonx = 200: buttony = 350
  198.             GOSUB press:
  199.             GOSUB number:
  200.         END IF
  201.  
  202.         'Pi
  203.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 350 AND mouseY < 405 THEN
  204.             num = _PI
  205.             num$ = STR$(num)
  206.             buttonx = 275: buttony = 350
  207.             GOSUB press:
  208.             GOSUB number:
  209.         END IF
  210.  
  211.         'Square Root
  212.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 75 AND mouseY < 130 THEN
  213.             IF num < 0 THEN GOTO skip1:
  214.             num = VAL(num$)
  215.             num = SQR(num)
  216.             num$ = STR$(num)
  217.             skip1:
  218.             buttonx = 50: buttony = 75
  219.             GOSUB press:
  220.             GOSUB number:
  221.         END IF
  222.  
  223.         'Sine
  224.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 75 AND mouseY < 130 THEN
  225.             IF deg = 1 THEN num = SIN(_D2R(VAL(num$)))
  226.             IF deg = 0 THEN num = SIN(num)
  227.             num$ = STR$(num)
  228.             buttonx = 126: buttony = 75
  229.             GOSUB press:
  230.             GOSUB number:
  231.         END IF
  232.  
  233.         'Cosine
  234.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 75 AND mouseY < 130 THEN
  235.             IF deg = 1 THEN num = COS(_D2R(VAL(num$)))
  236.             IF deg = 0 THEN num = COS(num)
  237.             num$ = STR$(num)
  238.             buttonx = 200: buttony = 75
  239.             GOSUB press:
  240.             GOSUB number:
  241.         END IF
  242.  
  243.         'Tangent
  244.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 75 AND mouseY < 130 THEN
  245.             IF deg = 1 THEN num = TAN(_D2R(VAL(num$)))
  246.             IF deg = 0 THEN num = TAN(num)
  247.             num$ = STR$(num)
  248.             buttonx = 275: buttony = 75
  249.             GOSUB press:
  250.             GOSUB number:
  251.         END IF
  252.  
  253.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 130 AND mouseY < 185 THEN
  254.             num$ = num$ + "7"
  255.             IF n = 1 THEN num$ = "-" + num$: n = 0
  256.             buttonx = 50: buttony = 130
  257.             GOSUB press:
  258.             GOSUB number:
  259.         END IF
  260.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 130 AND mouseY < 185 THEN
  261.             num$ = num$ + "8"
  262.             IF n = 1 THEN num$ = "-" + num$: n = 0
  263.             buttonx = 126: buttony = 130
  264.             GOSUB press:
  265.             GOSUB number:
  266.         END IF
  267.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 130 AND mouseY < 185 THEN
  268.             num$ = num$ + "9"
  269.             IF n = 1 THEN num$ = "-" + num$: n = 0
  270.             buttonx = 200: buttony = 130
  271.             GOSUB press:
  272.             GOSUB number:
  273.         END IF
  274.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 185 AND mouseY < 240 THEN
  275.             num$ = num$ + "4"
  276.             IF n = 1 THEN num$ = "-" + num$: n = 0
  277.             buttonx = 50: buttony = 185
  278.             GOSUB press:
  279.             GOSUB number:
  280.         END IF
  281.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 185 AND mouseY < 240 THEN
  282.             num$ = num$ + "5"
  283.             IF n = 1 THEN num$ = "-" + num$: n = 0
  284.             buttonx = 126: buttony = 185
  285.             GOSUB press:
  286.             GOSUB number:
  287.         END IF
  288.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 185 AND mouseY < 240 THEN
  289.             num$ = num$ + "6"
  290.             IF n = 1 THEN num$ = "-" + num$: n = 0
  291.             buttonx = 200: buttony = 185
  292.             GOSUB press:
  293.             GOSUB number:
  294.         END IF
  295.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 240 AND mouseY < 295 THEN
  296.             num$ = num$ + "1"
  297.             IF n = 1 THEN num$ = "-" + num$: n = 0
  298.             buttonx = 50: buttony = 240
  299.             GOSUB press:
  300.             GOSUB number:
  301.         END IF
  302.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 240 AND mouseY < 295 THEN
  303.             num$ = num$ + "2"
  304.             IF n = 1 THEN num$ = "-" + num$: n = 0
  305.             buttonx = 126: buttony = 240
  306.             GOSUB press:
  307.             GOSUB number:
  308.         END IF
  309.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 240 AND mouseY < 295 THEN
  310.             num$ = num$ + "3"
  311.             IF n = 1 THEN num$ = "-" + num$: n = 0
  312.             buttonx = 200: buttony = 240
  313.             GOSUB press:
  314.             GOSUB number:
  315.         END IF
  316.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 295 AND mouseY < 350 THEN
  317.             num$ = num$ + "0"
  318.             IF n = 1 THEN num$ = "-" + num$: n = 0
  319.             buttonx = 50: buttony = 295
  320.             GOSUB press:
  321.             GOSUB number:
  322.         END IF
  323.  
  324.         'Decimal
  325.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 295 AND mouseY < 350 THEN
  326.             buttonx = 126: buttony = 295
  327.             FOR check = 1 TO LEN(num$)
  328.                 IF MID$(num$, check, 1) = "." THEN GOTO skipdec:
  329.             NEXT check
  330.             num$ = num$ + "."
  331.             _PRINTSTRING (55, 30), num$
  332.             skipdec:
  333.             GOSUB press:
  334.         END IF
  335.  
  336.         'Equals
  337.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 295 AND mouseY < 350 THEN
  338.             IF a = 1 THEN num = VAL(oldnum$) + VAL(num$): a = 0
  339.             IF s = 1 THEN num = VAL(oldnum$) - VAL(num$): s = 0
  340.             IF t = 1 THEN num = VAL(oldnum$) * VAL(num$): t = 0
  341.             IF d = 1 THEN num = VAL(oldnum$) / VAL(num$): d = 0
  342.             IF ee = 1 THEN num = VAL(oldnum$) ^ VAL(num$): ee = 0
  343.             buttonx = 200: buttony = 295
  344.             GOSUB press:
  345.             GOSUB number2:
  346.         END IF
  347.  
  348.         'Add
  349.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 295 AND mouseY < 350 THEN
  350.             IF a = 0 THEN
  351.                 d = 0: a = 1: s = 0: t = 0: ee = 0
  352.                 oldnum$ = num$
  353.                 _PRINTSTRING (55, 30), "                          "
  354.                 num$ = ""
  355.                 GOTO nex4:
  356.             END IF
  357.             IF a = 1 THEN
  358.                 num = VAL(oldnum$) + VAL(num$)
  359.                 a = 0
  360.             END IF
  361.             GOSUB number2:
  362.             nex4:
  363.             buttonx = 275: buttony = 295
  364.             GOSUB press:
  365.         END IF
  366.  
  367.         'Subtract
  368.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 240 AND mouseY < 295 THEN
  369.             IF s = 0 THEN
  370.                 IF num$ = "0" OR num$ = "" OR num = 0 THEN n = 1: GOTO nex3:
  371.                 d = 0: a = 0: s = 1: t = 0: ee = 0
  372.                 oldnum$ = num$
  373.                 _PRINTSTRING (55, 30), "                          "
  374.                 num$ = ""
  375.                 GOTO nex3:
  376.             END IF
  377.             IF s = 1 THEN
  378.                 num = VAL(oldnum$) - VAL(num$)
  379.                 s = 0
  380.             END IF
  381.             GOSUB number2:
  382.             nex3:
  383.             buttonx = 275: buttony = 240
  384.             GOSUB press:
  385.         END IF
  386.  
  387.         'Multiply
  388.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 185 AND mouseY < 240 THEN
  389.             IF t = 0 THEN
  390.                 d = 0: a = 0: s = 0: t = 1: ee = 0
  391.                 oldnum$ = num$
  392.                 _PRINTSTRING (55, 30), "                          "
  393.                 num$ = ""
  394.                 GOTO nex2:
  395.             END IF
  396.             IF t = 1 THEN
  397.                 num = VAL(oldnum$) * VAL(num$)
  398.                 t = 0
  399.             END IF
  400.             GOSUB number2:
  401.             nex2:
  402.             buttonx = 275: buttony = 185
  403.             GOSUB press:
  404.         END IF
  405.  
  406.         'Divide
  407.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 130 AND mouseY < 185 THEN
  408.             IF d = 0 THEN
  409.                 d = 1: a = 0: s = 0: t = 0: ee = 0
  410.                 oldnum$ = num$
  411.                 _PRINTSTRING (55, 30), "                          "
  412.                 num$ = ""
  413.                 GOTO nex1:
  414.             END IF
  415.             IF d = 1 AND num$ <> "" THEN
  416.                 num = VAL(oldnum$) / VAL(num$)
  417.                 d = 0
  418.             END IF
  419.             GOSUB number2:
  420.             nex1:
  421.             buttonx = 275: buttony = 130
  422.             GOSUB press:
  423.         END IF
  424.     END IF
  425. 'For Number Buttons.
  426. number:
  427. num = VAL(num$)
  428.  
  429. 'For Math
  430. number2:
  431. COLOR _RGB32(0, 0, 0), _RGB32(0, 0, 0)
  432. _PRINTSTRING (55, 30), "                          "
  433. num$ = STR$(num)
  434. COLOR _RGB32(255, 255, 255), _RGB32(0, 0, 0)
  435. _PRINTSTRING (55, 30), num$
  436.  
  437.  
  438. 'Pressing Each Button
  439. press:
  440. c = 110
  441. FOR bb = 0 TO 10
  442.     c = c - 10
  443.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  444. NEXT bb
  445. _DELAY .25
  446. FOR bb = 0 TO 10
  447.     c = c + 10
  448.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  449. NEXT bb
  450.  
  451. 'Pressing the Green C Button.
  452. zero:
  453. c = 110
  454. FOR bb = 0 TO 10
  455.     c = c - 10
  456.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  457. NEXT bb
  458. _DELAY .25
  459. FOR bb = 0 TO 10
  460.     c = c + 10
  461.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  462. NEXT bb
  463.  
  464.  
  465.  
  466.  
  467. FUNCTION BASIMAGE1& 'calculatio icon data
  468.     v& = _NEWIMAGE(54, 48, 32)
  469.     DIM m AS _MEM: m = _MEMIMAGE(v&)
  470.     A$ = ""
  471.     A$ = A$ + "haIkJ^eBK\557GZJOl02m5YDX5J1A:YC0aGE##Oc?0RUD5A:HDOA=N=2N96`"
  472.     A$ = A$ + ";87A#=X?hm4S6=N3_[XV4CRgn4]>co5=SJdI?IR9R7K2lSmNFke\bj?c<[I]"
  473.     A$ = A$ + "3QXknHbT9:]dB;Y`kblMd7Tn#B3cF=CZmYb:[Tb<c<Y<b83Ua8c5QJnTNjY["
  474.     A$ = A$ + "X[go?_W<f^AbX=I\7O8IGRiA[eSAbZ7[ZZ[nj[?Z`2;DgD#15lTf;ZXRXlc?"
  475.     A$ = A$ + "okdUEiL`Q<>Ua]nAb]4c3\?_<S8[;_Z[[4mmQIWMfDNiUg3eeTb\[<1T9\n#"
  476.     A$ = A$ + ":\4SR=obhEVAOUX[moCNM[n#j6965KQTImDj:U^BY;mZ[O4j7D>[T3Ukfin\"
  477.     A$ = A$ + "eEomg?EB9U`DLa5[#dfRLlm]7mi0PVb:[\R;MmY>o4mUom;D>NbQnn1OoEee"
  478.     A$ = A$ + "[kSNee7]lAjKcl=b#03T1o6NcX[Lg:Gjc3mIj[CmEbP_g>jjeLM3Ee5jg0m:"
  479.     A$ = A$ + "4DO85DRAaV?IFkER^V#V5TaUnAbU4c3\?WaGln3OT^Jc8gk=gCOLPT=9V7]F"
  480.     A$ = A$ + "GRkWOjYWVZYVJ8cV=cSA07;2]l1[okJn=mc6o9IbR9jG=o[4:8V>Lj#EM]jZ"
  481.     A$ = A$ + "[B3>hPdHS=6chS?^bX84i3FKTA6Q\Je:3V7;a<F1a2ilJ[]VZj2[=jXSB;_l"
  482.     A$ = A$ + "bdNk]7M`17#k_onlX8Dc74S=fH3Jb9WDAGH>\59n?7#<#^RLf[G_JZ[eGOM:"
  483.     A$ = A$ + "H`PdeGO=c=gLS#DcW#Q2aJ>JM1KQ37V^mfKe5ARKTSAlA^VBGYdE:Mmg]^6H"
  484.     A$ = A$ + "P1X012lRZ;\o\>k\CZWNjQjZ[^hl\nj[WkCIXQ6RlkgOL[[;_lBnk7onLK^i"
  485.     A$ = A$ + "VD]eF;eJ[]B]dB;D5ED1O^9;MeNk]CmfK_T=KfX>jX3Zk^kVeDS=f8EGMeAW"
  486.     A$ = A$ + "NjY?k[GUFJY\N#O:8VEGMeDIUE6IaRUTV^P]S>j8>o?olcY3?l#nN6TWhh[^"
  487.     A$ = A$ + "jZW]^>iTChc3SPc>k<VkO_`o7On:IFgh]FOD_EkkMa5GTOn<JJ^iV5RFnPeA"
  488.     A$ = A$ + "]5#\4?m2RYCW>EEG[\b:LmKD_;2AZo92]l1_GX]fJS1ce:Nj5TcNlhAhcGhn"
  489.     A$ = A$ + "TiVK>Fo^LiBID4RlIQ5FPVHR9D^?4cQ=]RI\2bETcaBM3<jcW?jR;^P1e65Q"
  490.     A$ = A$ + "JnPJck^k^?Z^1\Qed:^J1ba8ilOceiQ_hLR>>RnnN:k8gCTj:JBY[Dj2m5^m"
  491.     A$ = A$ + "fKcnnFBGc>k\DiUG>_?UMfIWTR^>nhSiL1m<mBY;DSMZYVRZZZZX]fJ[4^^P"
  492.     A$ = A$ + "?XoXIVIVGd[GH_3hMR3?l`lnCBdjJniWWO?WXW_CEgh^gMSn1`i1#>82el1e"
  493.     A$ = A$ + "FD[O3\VFaE;#>RL=Fd5je2g3\dB;A^MkFID4Rl1m>j`Q35MQi`VFa<F1iZFm"
  494.     A$ = A$ + "7RNRAoff]KG1lOVjn7oDXU?XWKlKS0`L]RWN1i\Xnian_JZYVDf[D4P=AXU?"
  495.     A$ = A$ + "Hm6JX1VH9Na3A_o[o3Ph%%L2"
  496.     btemp$ = ""
  497.     FOR i& = 1 TO LEN(A$) STEP 4: B$ = MID$(A$, i&, 4)
  498.         IF INSTR(1, B$, "%") THEN
  499.             FOR C% = 1 TO LEN(B$): F$ = MID$(B$, C%, 1)
  500.                 IF F$ <> "%" THEN C$ = C$ + F$
  501.             NEXT: B$ = C$: END IF: FOR j = 1 TO LEN(B$)
  502.             IF MID$(B$, j, 1) = "#" THEN
  503.         MID$(B$, j) = "@": END IF: NEXT
  504.         FOR t% = LEN(B$) TO 1 STEP -1
  505.             B& = B& * 64 + ASC(MID$(B$, t%)) - 48
  506.             NEXT: X$ = "": FOR t% = 1 TO LEN(B$) - 1
  507.             X$ = X$ + CHR$(B& AND 255): B& = B& \ 256
  508.     NEXT: btemp$ = btemp$ + X$: NEXT
  509.     btemp$ = _INFLATE$(btemp$)
  510.     _MEMPUT m, m.OFFSET, btemp$: _MEMFREE m
  511.     BASIMAGE1& = _COPYIMAGE(v&): _FREEIMAGE v&
  512.  
Ken's Calculator.jpg
* Ken's Calculator.jpg (Filesize: 44.11 KB, Dimensions: 404x528, Views: 154)
« Last Edit: July 29, 2020, 03:53:02 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculator
« Reply #22 on: July 29, 2020, 06:44:59 pm »
Hi Ken,

The trig functions look OK. :)

I thought EXP was e^x, it is with QB64 but on my old calculator 1 EXP 2 = 100 and 1 (e^x) = 2.71... = e , your EXP isn't working either way.

The +- button could work better, if by toggling a minus sign next to number. When I go 12 -+ I get -12 but when I go +- again I get 0 ?

I tried _ICON _DISPLAY (after the calc was drawn before main loop) but the image is too small to make out any detail so your choice to keep Dav's image seems good.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculator
« Reply #23 on: July 29, 2020, 06:58:56 pm »
Thanks B+, I'll look more into EXP. Also I had no idea about the +/- problem, I'll try to fix that too.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculator
« Reply #24 on: July 29, 2020, 07:30:05 pm »
I'm at a roadblock with EXP. Everything I try doesn't work and Google has different outputs. I have no idea how to use it. I've tried to look at other websites but they are no help either. Do you think I should just remove LOG and EXP and put something else? I don't even know if it takes 2 numbers or 1. The wiki site says one but it also says it's the natural version. I use base 10 on my Log, which is different. Google uses 2 numbers and the wiki site just 1.
« Last Edit: July 29, 2020, 07:31:14 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculator
« Reply #25 on: July 29, 2020, 07:32:45 pm »
But I did fix the +/- problem, which was fairly easy to fix.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculator
« Reply #26 on: July 29, 2020, 08:20:53 pm »
Hi Ken,

For EXP just use QB64 EXP(num$), piece of cake. It just uses 1 number like SIN or LOG.

These are natural logarithms, base e, as opposed to base 10 logarithms
Code: QB64: [Select]
  1. PRINT "This is e:"; EXP(1)
  2. PRINT "This is e squared:"; EXP(2)
  3. ' LOG reverses EXP
  4.  

« Last Edit: July 29, 2020, 08:23:20 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculator
« Reply #27 on: July 29, 2020, 08:43:19 pm »
Awesome thanks again! I did it your way and removed the base 10 from the Log and just did num = LOG(VAL(num$)) and also the same thing with EXP with num = EXP(VAL(num$)). I did what you said and they switch back and forth almost perfectly except for like a .00000003 difference, which is no big deal lol. I think I am finished now!
Oh also, in case you missed me saying yesterday, if you left click the output number with your mouse, it copies it to the windows clipboard. :) Check it out :).

Here is the update:

Code: QB64: [Select]
  1. 'Calculator
  2. 'By SierraKen on July 29, 2020.
  3. '------------------------------
  4. 'This is my very first regular calculator.
  5. 'Thank you to B+ and TempodiBasic for the help and everyone for the inspiration!
  6. 'This was a 4 day project and I've wanted to make this all my life. Ever since I was real little
  7. 'playing around with my parents' calculator.
  8. 'Thank you Dav for the icon code!
  9. 'Added Clipboard by clicking the output number.
  10. 'Added Rad2Deg (and Deg2Rad), second power, Logarithm, and Pi buttons.
  11. 'Added 1/x, x/2, exp, postive or negative.
  12.  
  13. 'Load the icon - call this somewhere near the top of your program
  14. _ICON BASIMAGE1&
  15.  
  16. _TITLE "Calculator - Click output to copy."
  17. SCREEN _NEWIMAGE(400, 500, 32)
  18.  
  19. 'Setup calculator
  20. LINE (50, 25)-(265, 50), _RGB32(255, 255, 255), B
  21. LINE (50, 75)-(350, 350), _RGB32(255, 255, 255), B
  22. 'Buttons
  23. FOR buttony = 75 TO 405 STEP 55
  24.     FOR buttonx = 50 TO 275 STEP 75
  25.         FOR bb = 0 TO 10
  26.             c = c + 10
  27.             LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  28.         NEXT bb
  29.         PAINT (buttonx + 12, buttony + 12), _RGB32(100 + c, 100 + c, 100 + c)
  30.         c = 0
  31.     NEXT buttonx
  32. NEXT buttony
  33. buttonx = 275: buttony = 20
  34. 'Green C Button
  35. FOR bb = 0 TO 10
  36.     c = c + 10
  37.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  38. NEXT bb
  39. PAINT (buttonx + 12, buttony + 12), _RGB32(50, 100 + c, 50)
  40. COLOR _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
  41. _PRINTSTRING (50, 5), "Rad"
  42.  
  43. 'Label Buttons
  44. _PRINTSTRING (312, 47), "C"
  45. COLOR _RGB32(0, 0, 0), _RGB32(210, 210, 210)
  46. _PRINTSTRING (87, 102), CHR$(251) 'square root
  47. _PRINTSTRING (152, 102), "sin"
  48. _PRINTSTRING (227, 102), "cos"
  49. _PRINTSTRING (302, 102), "tan"
  50. _PRINTSTRING (87, 157), "7"
  51. _PRINTSTRING (162, 157), "8"
  52. _PRINTSTRING (237, 157), "9"
  53. _PRINTSTRING (312, 157), "/"
  54. _PRINTSTRING (87, 212), "4"
  55. _PRINTSTRING (162, 212), "5"
  56. _PRINTSTRING (237, 212), "6"
  57. _PRINTSTRING (312, 212), "x"
  58. _PRINTSTRING (87, 267), "1"
  59. _PRINTSTRING (162, 267), "2"
  60. _PRINTSTRING (237, 267), "3"
  61. _PRINTSTRING (312, 267), "-"
  62. _PRINTSTRING (87, 322), "0"
  63. _PRINTSTRING (162, 322), "."
  64. _PRINTSTRING (237, 322), "="
  65. _PRINTSTRING (312, 322), "+"
  66. _PRINTSTRING (79, 377), "deg"
  67. _PRINTSTRING (162, 377), "x" + CHR$(253) 'second power
  68. _PRINTSTRING (227, 377), "log"
  69. _PRINTSTRING (307, 377), "Pi"
  70. _PRINTSTRING (79, 432), "1/x"
  71. _PRINTSTRING (152, 432), "x/2"
  72. _PRINTSTRING (227, 432), "exp"
  73. _PRINTSTRING (312, 432), CHR$(241) 'Postive or Negative
  74.  
  75. COLOR _RGB32(255, 255, 255), _RGB32(0, 0, 0)
  76.     _LIMIT 20
  77.         mouseX = _MOUSEX
  78.         mouseY = _MOUSEY
  79.         mouseLeftButton = _MOUSEBUTTON(1)
  80.     LOOP
  81.     IF mouseLeftButton THEN
  82.         mouseLeftButton = 0
  83.  
  84.         'Clipboard
  85.         IF mouseX > 50 AND mouseX < 265 AND mouseY > 25 AND mouseY < 50 THEN
  86.             COLOR _RGB32(0, 255, 0), _RGB32(0, 0, 0)
  87.             _PRINTSTRING (55, 30), num$
  88.             _DELAY 1
  89.             num2$ = _TRIM$(num$)
  90.             _CLIPBOARD$ = num2$
  91.             COLOR _RGB32(255, 255, 255), _RGB32(0, 0, 0)
  92.             _PRINTSTRING (55, 30), num$
  93.         END IF
  94.  
  95.         'Clear
  96.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 20 AND mouseY < 75 THEN
  97.             a = 0: s = 0: t = 0: d = 0: num$ = ""
  98.             _PRINTSTRING (55, 30), "                          "
  99.             buttonx = 275: buttony = 20
  100.             GOSUB zero:
  101.         END IF
  102.  
  103.         '1/x
  104.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 405 AND mouseY < 460 THEN
  105.             num = VAL(num$)
  106.             IF num = 0 THEN GOTO skipthis:
  107.             num = 1 / num
  108.             num$ = STR$(num)
  109.             skipthis:
  110.             buttonx = 50: buttony = 405
  111.             GOSUB press:
  112.             GOSUB number:
  113.         END IF
  114.  
  115.         'x/2
  116.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 405 AND mouseY < 460 THEN
  117.             num = VAL(num$)
  118.             num = num / 2
  119.             num$ = STR$(num)
  120.             buttonx = 126: buttony = 405
  121.             GOSUB press:
  122.             GOSUB number2:
  123.         END IF
  124.  
  125.         'EXP
  126.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 405 AND mouseY < 460 THEN
  127.             _PRINTSTRING (55, 30), "                          "
  128.             num = EXP(VAL(num$))
  129.             num$ = STR$(num)
  130.             buttonx = 200: buttony = 405
  131.             GOSUB number2:
  132.             GOSUB press:
  133.         END IF
  134.  
  135.         'Postive or Negative
  136.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 405 AND mouseY < 460 THEN
  137.             IF VAL(num$) < 0 THEN
  138.                 num = -VAL(num$)
  139.                 num$ = STR$(num)
  140.                 GOTO skipplusnegative:
  141.             END IF
  142.             num$ = "-" + num$
  143.             skipplusnegative:
  144.             buttonx = 275: buttony = 405
  145.             GOSUB press:
  146.             GOSUB number:
  147.         END IF
  148.  
  149.         'Radians and Degrees
  150.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 350 AND mouseY < 405 THEN
  151.             deg = deg + 1
  152.             IF deg = 2 THEN GOTO deg2rad:
  153.             COLOR _RGB32(0, 0, 0), _RGB32(210, 210, 210)
  154.             _PRINTSTRING (79, 377), "rad"
  155.             COLOR _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
  156.             _PRINTSTRING (50, 5), "Deg"
  157.             num = VAL(num$)
  158.             num = _R2D(num)
  159.             num$ = STR$(num)
  160.             GOTO skipdeg2rad:
  161.             deg2rad:
  162.             deg = 0
  163.             COLOR _RGB32(0, 0, 0), _RGB32(210, 210, 210)
  164.             _PRINTSTRING (79, 377), "deg"
  165.             COLOR _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
  166.             _PRINTSTRING (50, 5), "Rad"
  167.             num = VAL(num$)
  168.             num = _D2R(num)
  169.             num$ = STR$(num)
  170.             skipdeg2rad:
  171.             buttonx = 50: buttony = 350
  172.             GOSUB press:
  173.             GOSUB number:
  174.         END IF
  175.  
  176.         'Second Power
  177.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 350 AND mouseY < 405 THEN
  178.             num = VAL(num$)
  179.             num = num ^ 2
  180.             num$ = STR$(num)
  181.             buttonx = 126: buttony = 350
  182.             GOSUB press:
  183.             GOSUB number:
  184.         END IF
  185.  
  186.         'logarithm
  187.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 350 AND mouseY < 405 THEN
  188.             num = VAL(num$)
  189.             IF num = 0 THEN GOTO skiplog:
  190.             IF num < 0 THEN GOTO skiplog:
  191.             num = LOG(num)
  192.             num$ = STR$(num)
  193.             skiplog:
  194.             buttonx = 200: buttony = 350
  195.             GOSUB press:
  196.             GOSUB number:
  197.         END IF
  198.  
  199.         'Pi
  200.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 350 AND mouseY < 405 THEN
  201.             num = _PI
  202.             num$ = STR$(num)
  203.             buttonx = 275: buttony = 350
  204.             GOSUB press:
  205.             GOSUB number:
  206.         END IF
  207.  
  208.         'Square Root
  209.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 75 AND mouseY < 130 THEN
  210.             IF num < 0 THEN GOTO skip1:
  211.             num = VAL(num$)
  212.             num = SQR(num)
  213.             num$ = STR$(num)
  214.             skip1:
  215.             buttonx = 50: buttony = 75
  216.             GOSUB press:
  217.             GOSUB number:
  218.         END IF
  219.  
  220.         'Sine
  221.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 75 AND mouseY < 130 THEN
  222.             IF deg = 1 THEN num = SIN(_D2R(VAL(num$)))
  223.             IF deg = 0 THEN num = SIN(num)
  224.             num$ = STR$(num)
  225.             buttonx = 126: buttony = 75
  226.             GOSUB press:
  227.             GOSUB number:
  228.         END IF
  229.  
  230.         'Cosine
  231.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 75 AND mouseY < 130 THEN
  232.             IF deg = 1 THEN num = COS(_D2R(VAL(num$)))
  233.             IF deg = 0 THEN num = COS(num)
  234.             num$ = STR$(num)
  235.             buttonx = 200: buttony = 75
  236.             GOSUB press:
  237.             GOSUB number:
  238.         END IF
  239.  
  240.         'Tangent
  241.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 75 AND mouseY < 130 THEN
  242.             IF deg = 1 THEN num = TAN(_D2R(VAL(num$)))
  243.             IF deg = 0 THEN num = TAN(num)
  244.             num$ = STR$(num)
  245.             buttonx = 275: buttony = 75
  246.             GOSUB press:
  247.             GOSUB number:
  248.         END IF
  249.  
  250.         'Number Buttons
  251.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 130 AND mouseY < 185 THEN
  252.             num$ = num$ + "7"
  253.             IF n = 1 THEN num$ = "-" + num$: n = 0
  254.             buttonx = 50: buttony = 130
  255.             GOSUB press:
  256.             GOSUB number:
  257.         END IF
  258.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 130 AND mouseY < 185 THEN
  259.             num$ = num$ + "8"
  260.             IF n = 1 THEN num$ = "-" + num$: n = 0
  261.             buttonx = 126: buttony = 130
  262.             GOSUB press:
  263.             GOSUB number:
  264.         END IF
  265.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 130 AND mouseY < 185 THEN
  266.             num$ = num$ + "9"
  267.             IF n = 1 THEN num$ = "-" + num$: n = 0
  268.             buttonx = 200: buttony = 130
  269.             GOSUB press:
  270.             GOSUB number:
  271.         END IF
  272.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 185 AND mouseY < 240 THEN
  273.             num$ = num$ + "4"
  274.             IF n = 1 THEN num$ = "-" + num$: n = 0
  275.             buttonx = 50: buttony = 185
  276.             GOSUB press:
  277.             GOSUB number:
  278.         END IF
  279.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 185 AND mouseY < 240 THEN
  280.             num$ = num$ + "5"
  281.             IF n = 1 THEN num$ = "-" + num$: n = 0
  282.             buttonx = 126: buttony = 185
  283.             GOSUB press:
  284.             GOSUB number:
  285.         END IF
  286.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 185 AND mouseY < 240 THEN
  287.             num$ = num$ + "6"
  288.             IF n = 1 THEN num$ = "-" + num$: n = 0
  289.             buttonx = 200: buttony = 185
  290.             GOSUB press:
  291.             GOSUB number:
  292.         END IF
  293.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 240 AND mouseY < 295 THEN
  294.             num$ = num$ + "1"
  295.             IF n = 1 THEN num$ = "-" + num$: n = 0
  296.             buttonx = 50: buttony = 240
  297.             GOSUB press:
  298.             GOSUB number:
  299.         END IF
  300.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 240 AND mouseY < 295 THEN
  301.             num$ = num$ + "2"
  302.             IF n = 1 THEN num$ = "-" + num$: n = 0
  303.             buttonx = 126: buttony = 240
  304.             GOSUB press:
  305.             GOSUB number:
  306.         END IF
  307.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 240 AND mouseY < 295 THEN
  308.             num$ = num$ + "3"
  309.             IF n = 1 THEN num$ = "-" + num$: n = 0
  310.             buttonx = 200: buttony = 240
  311.             GOSUB press:
  312.             GOSUB number:
  313.         END IF
  314.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 295 AND mouseY < 350 THEN
  315.             num$ = num$ + "0"
  316.             IF n = 1 THEN num$ = "-" + num$: n = 0
  317.             buttonx = 50: buttony = 295
  318.             GOSUB press:
  319.             GOSUB number:
  320.         END IF
  321.  
  322.         'Decimal
  323.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 295 AND mouseY < 350 THEN
  324.             buttonx = 126: buttony = 295
  325.             FOR check = 1 TO LEN(num$)
  326.                 IF MID$(num$, check, 1) = "." THEN GOTO skipdec:
  327.             NEXT check
  328.             num$ = num$ + "."
  329.             _PRINTSTRING (55, 30), num$
  330.             skipdec:
  331.             GOSUB press:
  332.         END IF
  333.  
  334.         'Equals
  335.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 295 AND mouseY < 350 THEN
  336.             IF a = 1 THEN num = VAL(oldnum$) + VAL(num$): a = 0
  337.             IF s = 1 THEN num = VAL(oldnum$) - VAL(num$): s = 0
  338.             IF t = 1 THEN num = VAL(oldnum$) * VAL(num$): t = 0
  339.             IF d = 1 THEN num = VAL(oldnum$) / VAL(num$): d = 0
  340.             buttonx = 200: buttony = 295
  341.             GOSUB press:
  342.             GOSUB number2:
  343.         END IF
  344.  
  345.         'Add
  346.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 295 AND mouseY < 350 THEN
  347.             IF a = 0 THEN
  348.                 d = 0: a = 1: s = 0: t = 0
  349.                 oldnum$ = num$
  350.                 _PRINTSTRING (55, 30), "                          "
  351.                 num$ = ""
  352.                 GOTO nex4:
  353.             END IF
  354.             IF a = 1 THEN
  355.                 num = VAL(oldnum$) + VAL(num$)
  356.                 a = 0
  357.             END IF
  358.             GOSUB number2:
  359.             nex4:
  360.             buttonx = 275: buttony = 295
  361.             GOSUB press:
  362.         END IF
  363.  
  364.         'Subtract
  365.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 240 AND mouseY < 295 THEN
  366.             IF s = 0 THEN
  367.                 IF num$ = "0" OR num$ = "" OR num = 0 THEN n = 1: GOTO nex3:
  368.                 d = 0: a = 0: s = 1: t = 0
  369.                 oldnum$ = num$
  370.                 _PRINTSTRING (55, 30), "                          "
  371.                 num$ = ""
  372.                 GOTO nex3:
  373.             END IF
  374.             IF s = 1 THEN
  375.                 num = VAL(oldnum$) - VAL(num$)
  376.                 s = 0
  377.             END IF
  378.             GOSUB number2:
  379.             nex3:
  380.             buttonx = 275: buttony = 240
  381.             GOSUB press:
  382.         END IF
  383.  
  384.         'Multiply
  385.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 185 AND mouseY < 240 THEN
  386.             IF t = 0 THEN
  387.                 d = 0: a = 0: s = 0: t = 1
  388.                 oldnum$ = num$
  389.                 _PRINTSTRING (55, 30), "                          "
  390.                 num$ = ""
  391.                 GOTO nex2:
  392.             END IF
  393.             IF t = 1 THEN
  394.                 num = VAL(oldnum$) * VAL(num$)
  395.                 t = 0
  396.             END IF
  397.             GOSUB number2:
  398.             nex2:
  399.             buttonx = 275: buttony = 185
  400.             GOSUB press:
  401.         END IF
  402.  
  403.         'Divide
  404.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 130 AND mouseY < 185 THEN
  405.             IF d = 0 THEN
  406.                 d = 1: a = 0: s = 0: t = 0
  407.                 oldnum$ = num$
  408.                 _PRINTSTRING (55, 30), "                          "
  409.                 num$ = ""
  410.                 GOTO nex1:
  411.             END IF
  412.             IF d = 1 AND num$ <> "" THEN
  413.                 num = VAL(oldnum$) / VAL(num$)
  414.                 d = 0
  415.             END IF
  416.             GOSUB number2:
  417.             nex1:
  418.             buttonx = 275: buttony = 130
  419.             GOSUB press:
  420.         END IF
  421.     END IF
  422. 'For Number Buttons.
  423. number:
  424. num = VAL(num$)
  425.  
  426. 'For Math
  427. number2:
  428. COLOR _RGB32(0, 0, 0), _RGB32(0, 0, 0)
  429. _PRINTSTRING (55, 30), "                          "
  430. num$ = STR$(num)
  431. COLOR _RGB32(255, 255, 255), _RGB32(0, 0, 0)
  432. _PRINTSTRING (55, 30), num$
  433.  
  434.  
  435. 'Pressing Each Button
  436. press:
  437. c = 110
  438. FOR bb = 0 TO 10
  439.     c = c - 10
  440.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  441. NEXT bb
  442. _DELAY .25
  443. FOR bb = 0 TO 10
  444.     c = c + 10
  445.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  446. NEXT bb
  447.  
  448. 'Pressing the Green C Button.
  449. zero:
  450. c = 110
  451. FOR bb = 0 TO 10
  452.     c = c - 10
  453.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  454. NEXT bb
  455. _DELAY .25
  456. FOR bb = 0 TO 10
  457.     c = c + 10
  458.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  459. NEXT bb
  460.  
  461. 'This Function is from Dav which makes the icon picture. It uses code at the start of this program.
  462. FUNCTION BASIMAGE1& 'calculatio icon data
  463.     v& = _NEWIMAGE(54, 48, 32)
  464.     DIM m AS _MEM: m = _MEMIMAGE(v&)
  465.     A$ = ""
  466.     A$ = A$ + "haIkJ^eBK\557GZJOl02m5YDX5J1A:YC0aGE##Oc?0RUD5A:HDOA=N=2N96`"
  467.     A$ = A$ + ";87A#=X?hm4S6=N3_[XV4CRgn4]>co5=SJdI?IR9R7K2lSmNFke\bj?c<[I]"
  468.     A$ = A$ + "3QXknHbT9:]dB;Y`kblMd7Tn#B3cF=CZmYb:[Tb<c<Y<b83Ua8c5QJnTNjY["
  469.     A$ = A$ + "X[go?_W<f^AbX=I\7O8IGRiA[eSAbZ7[ZZ[nj[?Z`2;DgD#15lTf;ZXRXlc?"
  470.     A$ = A$ + "okdUEiL`Q<>Ua]nAb]4c3\?_<S8[;_Z[[4mmQIWMfDNiUg3eeTb\[<1T9\n#"
  471.     A$ = A$ + ":\4SR=obhEVAOUX[moCNM[n#j6965KQTImDj:U^BY;mZ[O4j7D>[T3Ukfin\"
  472.     A$ = A$ + "eEomg?EB9U`DLa5[#dfRLlm]7mi0PVb:[\R;MmY>o4mUom;D>NbQnn1OoEee"
  473.     A$ = A$ + "[kSNee7]lAjKcl=b#03T1o6NcX[Lg:Gjc3mIj[CmEbP_g>jjeLM3Ee5jg0m:"
  474.     A$ = A$ + "4DO85DRAaV?IFkER^V#V5TaUnAbU4c3\?WaGln3OT^Jc8gk=gCOLPT=9V7]F"
  475.     A$ = A$ + "GRkWOjYWVZYVJ8cV=cSA07;2]l1[okJn=mc6o9IbR9jG=o[4:8V>Lj#EM]jZ"
  476.     A$ = A$ + "[B3>hPdHS=6chS?^bX84i3FKTA6Q\Je:3V7;a<F1a2ilJ[]VZj2[=jXSB;_l"
  477.     A$ = A$ + "bdNk]7M`17#k_onlX8Dc74S=fH3Jb9WDAGH>\59n?7#<#^RLf[G_JZ[eGOM:"
  478.     A$ = A$ + "H`PdeGO=c=gLS#DcW#Q2aJ>JM1KQ37V^mfKe5ARKTSAlA^VBGYdE:Mmg]^6H"
  479.     A$ = A$ + "P1X012lRZ;\o\>k\CZWNjQjZ[^hl\nj[WkCIXQ6RlkgOL[[;_lBnk7onLK^i"
  480.     A$ = A$ + "VD]eF;eJ[]B]dB;D5ED1O^9;MeNk]CmfK_T=KfX>jX3Zk^kVeDS=f8EGMeAW"
  481.     A$ = A$ + "NjY?k[GUFJY\N#O:8VEGMeDIUE6IaRUTV^P]S>j8>o?olcY3?l#nN6TWhh[^"
  482.     A$ = A$ + "jZW]^>iTChc3SPc>k<VkO_`o7On:IFgh]FOD_EkkMa5GTOn<JJ^iV5RFnPeA"
  483.     A$ = A$ + "]5#\4?m2RYCW>EEG[\b:LmKD_;2AZo92]l1_GX]fJS1ce:Nj5TcNlhAhcGhn"
  484.     A$ = A$ + "TiVK>Fo^LiBID4RlIQ5FPVHR9D^?4cQ=]RI\2bETcaBM3<jcW?jR;^P1e65Q"
  485.     A$ = A$ + "JnPJck^k^?Z^1\Qed:^J1ba8ilOceiQ_hLR>>RnnN:k8gCTj:JBY[Dj2m5^m"
  486.     A$ = A$ + "fKcnnFBGc>k\DiUG>_?UMfIWTR^>nhSiL1m<mBY;DSMZYVRZZZZX]fJ[4^^P"
  487.     A$ = A$ + "?XoXIVIVGd[GH_3hMR3?l`lnCBdjJniWWO?WXW_CEgh^gMSn1`i1#>82el1e"
  488.     A$ = A$ + "FD[O3\VFaE;#>RL=Fd5je2g3\dB;A^MkFID4Rl1m>j`Q35MQi`VFa<F1iZFm"
  489.     A$ = A$ + "7RNRAoff]KG1lOVjn7oDXU?XWKlKS0`L]RWN1i\Xnian_JZYVDf[D4P=AXU?"
  490.     A$ = A$ + "Hm6JX1VH9Na3A_o[o3Ph%%L2"
  491.     btemp$ = ""
  492.     FOR i& = 1 TO LEN(A$) STEP 4: B$ = MID$(A$, i&, 4)
  493.         IF INSTR(1, B$, "%") THEN
  494.             FOR C% = 1 TO LEN(B$): F$ = MID$(B$, C%, 1)
  495.                 IF F$ <> "%" THEN C$ = C$ + F$
  496.             NEXT: B$ = C$: END IF: FOR j = 1 TO LEN(B$)
  497.             IF MID$(B$, j, 1) = "#" THEN
  498.         MID$(B$, j) = "@": END IF: NEXT
  499.         FOR t% = LEN(B$) TO 1 STEP -1
  500.             B& = B& * 64 + ASC(MID$(B$, t%)) - 48
  501.             NEXT: X$ = "": FOR t% = 1 TO LEN(B$) - 1
  502.             X$ = X$ + CHR$(B& AND 255): B& = B& \ 256
  503.     NEXT: btemp$ = btemp$ + X$: NEXT
  504.     btemp$ = _INFLATE$(btemp$)
  505.     _MEMPUT m, m.OFFSET, btemp$: _MEMFREE m
  506.     BASIMAGE1& = _COPYIMAGE(v&): _FREEIMAGE v&
  507.  
Ken's Calculator.jpg
* Ken's Calculator.jpg (Filesize: 44.11 KB, Dimensions: 404x528, Views: 119)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculator
« Reply #28 on: July 30, 2020, 11:26:53 am »
What is really handy for calculators is an extra Memory key to hold a value and modify it, also this not being a real calculator you could show the previous value and the operation button. In fact, if you aren't a big fan of Ln(x) or e^(x) you might rearrange buttons for extra memory value. Also more handy than Ln and e is X^Y so you could do 2^8 or 3^5...

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Calculator
« Reply #29 on: July 30, 2020, 12:10:50 pm »
That's a good-looking calculator. Works quite well. Have you thought about adding keyboard functionality?
Shuwatch!