Author Topic: Something for my Project :P  (Read 2955 times)

0 Members and 1 Guest are viewing this topic.

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Something for my Project :P
« on: January 28, 2019, 09:51:35 pm »
I sat down to create the project we got in school about QBASIC. And then I thought to myself, "What would it be?" and then I scrapped all the huge ideas and started making a simple calculator(graphical). I made it, but I ran into ONE problem. And that is *drumroll please!* that it can't do more than 2 operations. Which is annoying cuz I planned to use this calculator myself xD. Here's the code I made up:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(450, 500, 256)
  2.     CLS
  3.     makecalculator
  4.     _PRINTSTRING (100, 50), a$
  5.     k$ = INKEY$
  6.     IF k$ = "=" THEN
  7.         a$ = calculate$(a$)
  8.     ELSEIF k$ = "+" THEN
  9.         a$ = a$ + " + "
  10.     ELSEIF k$ = "-" THEN
  11.         a$ = a$ + " - "
  12.     ELSEIF k$ = "*" THEN
  13.         a$ = a$ + " * "
  14.     ELSEIF k$ = "/" THEN
  15.         a$ = a$ + " / "
  16.     ELSEIF k$ = CHR$(8) THEN a$ = LEFT$(a$, LEN(a$) - 1)
  17.     ELSE
  18.         a$ = a$ + k$
  19.     END IF
  20.         IF mouseclick(50, 100, 100, 150) THEN a$ = a$ + "1"
  21.         IF mouseclick(150, 100, 200, 150) THEN a$ = a$ + "2"
  22.         IF mouseclick(250, 100, 300, 150) THEN a$ = a$ + "3"
  23.         IF mouseclick(350, 100, 400, 150) THEN a$ = a$ + " + "
  24.  
  25.         IF mouseclick(50, 200, 100, 250) THEN a$ = a$ + "4"
  26.         IF mouseclick(150, 200, 200, 250) THEN a$ = a$ + "5"
  27.         IF mouseclick(250, 200, 300, 250) THEN a$ = a$ + "6"
  28.         IF mouseclick(350, 200, 400, 250) THEN a$ = a$ + " - "
  29.  
  30.         IF mouseclick(50, 300, 100, 350) THEN a$ = a$ + "7"
  31.         IF mouseclick(150, 300, 200, 350) THEN a$ = a$ + "8"
  32.         IF mouseclick(250, 300, 300, 350) THEN a$ = a$ + "9"
  33.         IF mouseclick(350, 300, 400, 350) THEN a$ = a$ + " * "
  34.  
  35.         IF mouseclick(50, 400, 100, 450) THEN a$ = a$ + "."
  36.         IF mouseclick(150, 400, 200, 450) THEN a$ = a$ + "0"
  37.         IF mouseclick(250, 400, 300, 450) THEN a$ = calculate$(a$)
  38.         IF mouseclick(350, 400, 400, 450) THEN a$ = a$ + " / "
  39.     WEND
  40.     _DISPLAY
  41.  
  42. SUB makecalculator ()
  43.     LINE (0, 0)-(450, 500), 2, B
  44.  
  45.     LINE (50, 25)-(400, 75), 2, B 'Output Screen
  46.  
  47.     LINE (50, 100)-(100, 150), 2, B
  48.     LINE (150, 100)-(200, 150), 2, B
  49.     LINE (250, 100)-(300, 150), 2, B
  50.     LINE (350, 100)-(400, 150), 2, B
  51.     _PRINTSTRING (75, 125), "1"
  52.     _PRINTSTRING (175, 125), "2"
  53.     _PRINTSTRING (275, 125), "3"
  54.     _PRINTSTRING (375, 125), "+"
  55.  
  56.  
  57.     LINE (50, 200)-(100, 250), 2, B
  58.     LINE (150, 200)-(200, 250), 2, B
  59.     LINE (250, 200)-(300, 250), 2, B
  60.     LINE (350, 200)-(400, 250), 2, B
  61.     _PRINTSTRING (75, 225), "4"
  62.     _PRINTSTRING (175, 225), "5"
  63.     _PRINTSTRING (275, 225), "6"
  64.     _PRINTSTRING (375, 225), "-"
  65.     LINE (50, 300)-(100, 350), 2, B
  66.     LINE (150, 300)-(200, 350), 2, B
  67.     LINE (250, 300)-(300, 350), 2, B
  68.     LINE (350, 300)-(400, 350), 2, B
  69.     _PRINTSTRING (75, 325), "7"
  70.     _PRINTSTRING (175, 325), "8"
  71.     _PRINTSTRING (275, 325), "9"
  72.     _PRINTSTRING (375, 325), "*"
  73.     LINE (50, 400)-(100, 450), 2, B
  74.     LINE (150, 400)-(200, 450), 2, B
  75.     LINE (250, 400)-(300, 450), 2, B
  76.     LINE (350, 400)-(400, 450), 2, B
  77.     _PRINTSTRING (75, 425), "."
  78.     _PRINTSTRING (175, 425), "0"
  79.     _PRINTSTRING (275, 425), "="
  80.     _PRINTSTRING (375, 425), "/"
  81.  
  82. FUNCTION mouseclick (x1, y1, x2, y2)
  83.     mx = _MOUSEX
  84.     my = _MOUSEY
  85.     mb = _MOUSEBUTTON(1)
  86.  
  87.     IF mx >= x1 AND mx <= x2 AND my >= y1 AND my <= y2 AND mb THEN
  88.         mouseclick = -1
  89.     END IF
  90.  
  91.  
  92. FUNCTION calculate$ (a$)
  93.     i = 0
  94.     IF LEFT$(a$, 1) = " " THEN i = i + 1
  95.     IF RIGHT$(a$, 1) <> " " THEN a$ = a$ + " "
  96.     b$ = ""
  97.     c$ = ""
  98.     d$ = ""
  99.     DO UNTIL b$ = " "
  100.         i = i + 1
  101.         b$ = MID$(a$, i, 1)
  102.         c$ = c$ + b$
  103.     LOOP
  104.     PRINT c$
  105.     b$ = ""
  106.     DO UNTIL b$ = "+" OR b$ = "-" OR b$ = "*" OR b$ = "/" OR b$ = "%"
  107.         i = i + 1
  108.         b$ = MID$(a$, i, 1)
  109.     LOOP
  110.     op$ = b$
  111.     PRINT op$
  112.     i = i + 1
  113.     DO UNTIL b$ = " "
  114.         i = i + 1
  115.         b$ = MID$(a$, i, 1)
  116.         d$ = d$ + b$
  117.     LOOP
  118.     PRINT d$
  119.     SELECT CASE op$
  120.         CASE "+"
  121.             calculate$ = STR$(VAL(c$) + VAL(d$))
  122.         CASE "-"
  123.             calculate$ = STR$(VAL(c$) - VAL(d$))
  124.         CASE "*"
  125.             calculate$ = STR$(VAL(c$) * VAL(d$))
  126.         CASE "/"
  127.             calculate$ = STR$(VAL(c$) / VAL(d$))
  128.         CASE "%"
  129.             calculate$ = STR$(VAL(c$) MOD VAL(d$))
  130.     END SELECT
  131.  
The only way I can do that is by equaling it multiple times which is REALLY annoying! Can anyone give me some ideas? Cuz, I really need some....

(This probably was the first time I got in this forum with a complete program lol)
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Something for my Project :P
« Reply #1 on: January 29, 2019, 12:21:49 am »
The mouse clicks cause multiple numbers to pop up. You can regulate that by waiting for a release. I added one line of code to the mouse function to achieve that. After that addition (excuse the pun) it seemed to work just fine.

As far as multiple operations, you'd need to store those in memory and then perform the operations after the = sign is pressed. I think a better way would be to have it automatically use the equals sign after the next operation is entered.

Code with mouse adjustment...

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(450, 500, 256)
  2.     CLS
  3.     makecalculator
  4.     _PRINTSTRING (100, 50), a$
  5.     k$ = INKEY$
  6.     IF k$ = "=" THEN
  7.         a$ = calculate$(a$)
  8.     ELSEIF k$ = "+" THEN
  9.         a$ = a$ + " + "
  10.     ELSEIF k$ = "-" THEN
  11.         a$ = a$ + " - "
  12.     ELSEIF k$ = "*" THEN
  13.         a$ = a$ + " * "
  14.     ELSEIF k$ = "/" THEN
  15.         a$ = a$ + " / "
  16.     ELSEIF k$ = CHR$(8) THEN a$ = LEFT$(a$, LEN(a$) - 1)
  17.     ELSE
  18.         a$ = a$ + k$
  19.     END IF
  20.         IF mouseclick(50, 100, 100, 150) THEN a$ = a$ + "1"
  21.         IF mouseclick(150, 100, 200, 150) THEN a$ = a$ + "2"
  22.         IF mouseclick(250, 100, 300, 150) THEN a$ = a$ + "3"
  23.         IF mouseclick(350, 100, 400, 150) THEN a$ = a$ + " + "
  24.  
  25.         IF mouseclick(50, 200, 100, 250) THEN a$ = a$ + "4"
  26.         IF mouseclick(150, 200, 200, 250) THEN a$ = a$ + "5"
  27.         IF mouseclick(250, 200, 300, 250) THEN a$ = a$ + "6"
  28.         IF mouseclick(350, 200, 400, 250) THEN a$ = a$ + " - "
  29.  
  30.         IF mouseclick(50, 300, 100, 350) THEN a$ = a$ + "7"
  31.         IF mouseclick(150, 300, 200, 350) THEN a$ = a$ + "8"
  32.         IF mouseclick(250, 300, 300, 350) THEN a$ = a$ + "9"
  33.         IF mouseclick(350, 300, 400, 350) THEN a$ = a$ + " * "
  34.  
  35.         IF mouseclick(50, 400, 100, 450) THEN a$ = a$ + "."
  36.         IF mouseclick(150, 400, 200, 450) THEN a$ = a$ + "0"
  37.         IF mouseclick(250, 400, 300, 450) THEN a$ = calculate$(a$)
  38.         IF mouseclick(350, 400, 400, 450) THEN a$ = a$ + " / "
  39.     WEND
  40.     _DISPLAY
  41.  
  42. SUB makecalculator ()
  43. LINE (0, 0)-(450, 500), 2, B
  44.  
  45. LINE (50, 25)-(400, 75), 2, B 'Output Screen
  46.  
  47. LINE (50, 100)-(100, 150), 2, B
  48. LINE (150, 100)-(200, 150), 2, B
  49. LINE (250, 100)-(300, 150), 2, B
  50. LINE (350, 100)-(400, 150), 2, B
  51. _PRINTSTRING (75, 125), "1"
  52. _PRINTSTRING (175, 125), "2"
  53. _PRINTSTRING (275, 125), "3"
  54. _PRINTSTRING (375, 125), "+"
  55.  
  56.  
  57. LINE (50, 200)-(100, 250), 2, B
  58. LINE (150, 200)-(200, 250), 2, B
  59. LINE (250, 200)-(300, 250), 2, B
  60. LINE (350, 200)-(400, 250), 2, B
  61. _PRINTSTRING (75, 225), "4"
  62. _PRINTSTRING (175, 225), "5"
  63. _PRINTSTRING (275, 225), "6"
  64. _PRINTSTRING (375, 225), "-"
  65. LINE (50, 300)-(100, 350), 2, B
  66. LINE (150, 300)-(200, 350), 2, B
  67. LINE (250, 300)-(300, 350), 2, B
  68. LINE (350, 300)-(400, 350), 2, B
  69. _PRINTSTRING (75, 325), "7"
  70. _PRINTSTRING (175, 325), "8"
  71. _PRINTSTRING (275, 325), "9"
  72. _PRINTSTRING (375, 325), "*"
  73. LINE (50, 400)-(100, 450), 2, B
  74. LINE (150, 400)-(200, 450), 2, B
  75. LINE (250, 400)-(300, 450), 2, B
  76. LINE (350, 400)-(400, 450), 2, B
  77. _PRINTSTRING (75, 425), "."
  78. _PRINTSTRING (175, 425), "0"
  79. _PRINTSTRING (275, 425), "="
  80. _PRINTSTRING (375, 425), "/"
  81.  
  82. FUNCTION mouseclick (x1, y1, x2, y2)
  83. mx = _MOUSEX
  84. my = _MOUSEY
  85. mb = _MOUSEBUTTON(1)
  86.  
  87. IF mx >= x1 AND mx <= x2 AND my >= y1 AND my <= y2 AND mb THEN
  88.     mouseclick = -1
  89.  
  90.  
  91. FUNCTION calculate$ (a$)
  92. i = 0
  93. IF LEFT$(a$, 1) = " " THEN i = i + 1
  94. IF RIGHT$(a$, 1) <> " " THEN a$ = a$ + " "
  95. b$ = ""
  96. c$ = ""
  97. d$ = ""
  98. DO UNTIL b$ = " "
  99.     i = i + 1
  100.     b$ = MID$(a$, i, 1)
  101.     c$ = c$ + b$
  102. b$ = ""
  103. DO UNTIL b$ = "+" OR b$ = "-" OR b$ = "*" OR b$ = "/" OR b$ = "%"
  104.     i = i + 1
  105.     b$ = MID$(a$, i, 1)
  106. op$ = b$
  107. PRINT op$
  108. i = i + 1
  109. DO UNTIL b$ = " "
  110.     i = i + 1
  111.     b$ = MID$(a$, i, 1)
  112.     d$ = d$ + b$
  113.     CASE "+"
  114.         calculate$ = STR$(VAL(c$) + VAL(d$))
  115.     CASE "-"
  116.         calculate$ = STR$(VAL(c$) - VAL(d$))
  117.     CASE "*"
  118.         calculate$ = STR$(VAL(c$) * VAL(d$))
  119.     CASE "/"
  120.         calculate$ = STR$(VAL(c$) / VAL(d$))
  121.     CASE "%"
  122.         calculate$ = STR$(VAL(c$) MOD VAL(d$))
  123.  

I suppose if you wanted to try a memory string, it might go something like this...

Code: QB64: [Select]
  1. s$ = "123+456-55*3/6"
  2. a$ = "": x = 0
  3. FOR i = 1 TO LEN(s$) + 1
  4.     t$ = MID$(s$, i, 1)
  5.     IF INSTR("+-*/", t$) OR i > LEN(s$) THEN
  6.         IF flag = 0 THEN x = VAL(a$): flag = -1
  7.         SELECT CASE o$
  8.             CASE "+"
  9.                 x = x + VAL(a$)
  10.             CASE "-"
  11.                 x = x - VAL(a$)
  12.             CASE "*"
  13.                 x = x * VAL(a$)
  14.             CASE "/"
  15.                 x = x / VAL(a$)
  16.         END SELECT
  17.         a$ = ""
  18.         o$ = t$: ' PRINT o$: SLEEP
  19.     ELSE
  20.         a$ = a$ + t$
  21.     END IF
  22. PRINT x, "=", (123 + 456 - 55) * 3 / 6
  23.  

Before you get too excited, remember that computers are binary and math is base 10. That means that you will get erroneous results from tie tie to time with calculations. I used to work with just strings for ledger calculations to avoid those types of problems, and I wish I could recall a simple rounding trick that corrected for much of the math errors thrown by the binary system but its been too many years. I'm sure there will be other response here from folks who have worked with calculator programs. As for what you have so far, very nice! Good luck with it.

Pete
« Last Edit: January 29, 2019, 12:55:06 am by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Something for my Project :P
« Reply #2 on: January 29, 2019, 12:37:15 am »
To avoid pressing = all the time,  setup two operands a$ and b$ and track the operator key op$ separately.
When an op$ press is detected, if none is registered (op$ = "") handle as you have but if one is in the works then update a$ with the math calculation, clear b$ and set the op$ to the new operation k$ that was just detected.
Code: QB64: [Select]
  1. _TITLE "Prithak's calc, mod B+ 2019-01-29"
  2. SCREEN _NEWIMAGE(450, 500, 256)
  3. _SCREENMOVE 400, 100
  4.     CLS
  5.     makecalculator 'draw it
  6.     _PRINTSTRING (100, 50), a$ + " " + op$ + " " + b$ 'show current status
  7.  
  8.     'get the k$
  9.     k$ = INKEY$ 'update keypresses
  10.     WHILE _MOUSEINPUT: WEND 'update mouse
  11.     mb = _MOUSEBUTTON(1) 'getbutton status
  12.     IF mb THEN 'button down
  13.         WHILE mb 'wait until mouse button is released before handling click
  14.             something = _MOUSEINPUT
  15.             mx = _MOUSEX: my = _MOUSEY: mb = _MOUSEBUTTON(1)
  16.         WEND
  17.         textIndex = 0
  18.         FOR y1 = 100 TO 400 STEP 100
  19.             FOR x1 = 50 TO 350 STEP 100
  20.                 textIndex = textIndex + 1
  21.                 IF mx >= x1 AND mx <= x1 + 50 AND my >= y1 AND my <= y1 + 50 THEN
  22.                     k$ = MID$("123+456-789*.0=/", textIndex, 1)
  23.                 END IF
  24.             NEXT
  25.         NEXT
  26.     END IF
  27.  
  28.     'now handle the k$
  29.     IF LEN(k$) THEN
  30.         IF k$ = CHR$(8) THEN 'backspace, be nice if had mouse button too for backspace and clearing
  31.             IF LEN(b$) THEN
  32.                 b$ = LEFT$(b$, LEN(b$) - 1)
  33.             ELSEIF LEN(op$) THEN
  34.                 op$ = ""
  35.             ELSEIF LEN(a$) THEN
  36.                 a$ = LEFT$(a$, LEN(a$) - 1)
  37.             END IF
  38.         END IF
  39.         IF INSTR("+-/*", k$) > 0 THEN
  40.             IF op$ = "" THEN
  41.                 op$ = k$
  42.             ELSE
  43.                 GOSUB calc
  44.                 op$ = k$
  45.             END IF
  46.         END IF
  47.         IF INSTR(".0123456789", k$) > 0 THEN
  48.             IF op$ <> "" THEN b$ = b$ + k$ ELSE a$ = a$ + k$
  49.         END IF
  50.         IF k$ = "=" THEN
  51.             GOSUB calc
  52.         END IF
  53.     END IF
  54.  
  55.     _DISPLAY
  56.     _LIMIT 30
  57.  
  58. calc:
  59. va = VAL(a$): vb = VAL(b$)
  60.     CASE "+": a$ = STR$(va + vb)
  61.     CASE "-": a$ = STR$(va - vb)
  62.     CASE "/": IF vb <> 0 THEN a$ = STR$(va / vb) ELSE BEEP
  63.     CASE "*": a$ = STR$(va * vb)
  64. b$ = "": op$ = ""
  65.  
  66. SUB makecalculator ()
  67.     LINE (0, 0)-(450, 500), 2, B 'frame
  68.     LINE (50, 25)-(400, 75), 2, B 'Output Screen
  69.     FOR y = 100 TO 400 STEP 100
  70.         FOR x = 50 TO 350 STEP 100
  71.             textIndex = textIndex + 1
  72.             text$ = MID$("123+456-789*.0=/", textIndex, 1)
  73.             _PRINTSTRING (x + 21, y + 17), text$
  74.             LINE (x, y)-STEP(50, 50), 2, B
  75.         NEXT
  76.     NEXT
  77.  

EDIT: cut more redundant code
« Last Edit: January 29, 2019, 01:03:45 am by bplus »