home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / basic / interpre / liberty / windows / calc.bas < prev    next >
BASIC Source File  |  1993-04-22  |  6KB  |  226 lines

  1.  
  2.  
  3.  
  4.     'CALC.BAS - calculator
  5.     'Requires Liberty BASIC 0.9c
  6.     'Copyright 1993, Shoptalk Systems
  7.  
  8.     'With Liberty BASIC 0.9c, it is possible to overrun
  9.     'the calculator on slower machines, so wait until
  10.     'each next button produces its result before
  11.     'clicking the next button.  In the registered version,
  12.     'each mouse action or button-click is buffered, so
  13.     'you can click on buttons as fast as you like,
  14.     'and the calculator will not lose anything while
  15.     'catching up.
  16.  
  17.     'The version 1.0 version of this program takes
  18.     'advantage of some of the special features of that
  19.     'system to give a slicker appearance.
  20.  
  21.     button #calc, " 0 ", [button0], LL, 5, 4
  22.     button #calc, " . ", [buttonPoint], LL, 40, 4
  23.     button #calc, " = ", [buttonEquals], LL, 75, 4
  24.     button #calc, "CLR", [buttonClear], LL, 110, 4
  25.     button #calc, " 1 ", [button1], LL, 5, 30
  26.     button #calc, " 2 ", [button2], LL, 40, 30
  27.     button #calc, " 3 ", [button3], LL, 75, 30
  28.     button #calc, " / ", [buttonDivide], LL, 110, 30
  29.     button #calc, " 4 ", [button4], LL, 5, 58
  30.     button #calc, " 5 ", [button5], LL, 40, 58
  31.     button #calc, " 6 ", [button6], LL, 75, 58
  32.     button #calc, " X ", [buttonMultiply], LL, 110, 58
  33.     button #calc, " 7 ", [button7], LL, 5, 86
  34.     button #calc, " 8 ", [button8], LL, 40, 86
  35.     button #calc, " 9 ", [button9], LL, 75, 86
  36.     button #calc, " - ", [buttonSubtract], LL, 110, 86
  37.     button #calc, "Info", [info], LL, 5, 114
  38.     button #calc, " + ", [buttonAdd], LL, 110, 114
  39.     WindowWidth = 172
  40.     WindowHeight = 280
  41.     open "Calculator" for graphics as #calc
  42.     print #calc, "font helv 9 24"
  43.  
  44.     ' set display$ up as two spaces
  45.     display$ = "? "
  46.     ' set up a shortcut for printing the entry line only
  47.     blanks$ = "\" + chr$(13) + chr$(13) + chr$(13) + chr$(13) + " "
  48.  
  49.  
  50. [mainLoop]  ' wait for a button to be pressed
  51.  
  52.     input r$
  53.     goto [mainLoop]
  54.  
  55.  
  56. [display]  ' update the entire display
  57.  
  58.     buffer$ = chr$(13)
  59.     for i = 6 to 2 step -2
  60.         buffer$ = buffer$ + " " + word$(lines$, i - 1) + " " + word$(lines$, i) + "                 " + chr$(13)
  61.     next i
  62.     print #calc, "cls"
  63.     print #calc, "\" + buffer$ + "                   "
  64.     gosub [displayEntry]
  65.     print #calc, "flush"
  66.  
  67.   return
  68.  
  69.  
  70. [displayEntry]  ' display the current entry line only
  71.  
  72.     print #calc, "place 0 0"
  73.     print #calc, blanks$ + display$
  74.     print #calc, "flush"
  75.  
  76.   return
  77.  
  78.  
  79. [button0]  ' 0 was pressed
  80.  
  81.     display$ = display$ + "0"
  82.     gosub [displayEntry]
  83.     goto [mainLoop]
  84.  
  85. [button1]  ' 1 was pressed
  86.  
  87.     display$ = display$ + "1"
  88.     gosub [displayEntry]
  89.     goto [mainLoop]
  90.  
  91. [button2]  ' 2 was pressed
  92.  
  93.     display$ = display$ + "2"
  94.     gosub [displayEntry]
  95.     goto [mainLoop]
  96.  
  97. [button3]  ' 3 was pressed
  98.  
  99.     display$ = display$ + "3"
  100.     gosub [displayEntry]
  101.     goto [mainLoop]
  102.  
  103. [button4]  ' 4 was pressed
  104.  
  105.     display$ = display$ + "4"
  106.     gosub [displayEntry]
  107.     goto [mainLoop]
  108.  
  109. [button5]  ' 5 was pressed
  110.  
  111.     display$ = display$ + "5"
  112.     gosub [displayEntry]
  113.     goto [mainLoop]
  114.  
  115. [button6]  ' 6 was pressed
  116.  
  117.     display$ = display$ + "6"
  118.     gosub [displayEntry]
  119.     goto [mainLoop]
  120.  
  121. [button7]  ' 7 was pressed
  122.  
  123.     display$ = display$ + "7"
  124.     gosub [displayEntry]
  125.     goto [mainLoop]
  126.  
  127. [button8]  ' 8 was pressed
  128.  
  129.     display$ = display$ + "8"
  130.     gosub [displayEntry]
  131.     goto [mainLoop]
  132.  
  133. [button9]  ' 9 was pressed
  134.  
  135.     display$ = display$ + "9"
  136.     gosub [displayEntry]
  137.     goto [mainLoop]
  138.  
  139.  
  140. [buttonPoint]  ' the decimal point button was pressed
  141.  
  142.     ' only allow one decimal point per entry
  143.     if instr(display$, ".") > 0 then [mainLoop]
  144.  
  145.     display$ = display$ + "."
  146.     gosub [displayEntry]
  147.     goto [mainLoop]
  148.  
  149.  
  150. [buttonClear]  ' the CLR button was pressed, clear the entry
  151.  
  152.     display$ = "? "
  153.     gosub [display]
  154.     goto [mainLoop]
  155.  
  156.  
  157. [buttonAdd]  ' the + button was pressed
  158.  
  159.     if len(display$) = 2 then display$ = "+ " : gosub [displayEntry] : goto [mainLoop]
  160.     gosub [resolvePending]
  161.     display$ = "+ "
  162.     gosub [display]
  163.     goto [mainLoop]
  164.  
  165.  
  166. [buttonSubtract]  ' the - button was pressed
  167.  
  168.     if len(display$) = 2 then display$ = "- " : gosub [displayEntry] : goto [mainLoop]
  169.     gosub [resolvePending]
  170.     display$ = "- "
  171.     gosub [display]
  172.     goto [mainLoop]
  173.  
  174.  
  175. [buttonMultiply]  ' the X button was pressed
  176.  
  177.     if len(display$) = 2 then display$ = "X " : gosub [displayEntry] : goto [mainLoop]
  178.     gosub [resolvePending]
  179.     display$ = "X "
  180.     gosub [display]
  181.     goto [mainLoop]
  182.  
  183.  
  184. [buttonDivide]
  185.  
  186.     if len(display$) = 2 then display$ = "/ " : gosub [displayEntry] : goto [mainLoop]
  187.     gosub [resolvePending]
  188.     display$ = "/ "
  189.     gosub [display]
  190.     goto [mainLoop]
  191.  
  192.  
  193. [buttonEquals]  ' the = button was pressed
  194.  
  195.     if len(display$) = 2 then display$ = "= " : gosub [displayEntry] : goto [mainLoop]
  196.     gosub [resolvePending]
  197.     display$ = "? "
  198.     gosub [display]
  199.     goto [mainLoop]
  200.  
  201.  
  202. [resolvePending]
  203.  
  204.     'take the bottom-most two items and perform the appropriate
  205.     'operation (if any)
  206.  
  207.     first = val(word$(lines$, 2))
  208.     second = val(word$(display$, 2))
  209.     op$ = left$(display$, 1)
  210.  
  211.     lines$ = display$ + " " + lines$
  212.  
  213.     if op$ = "+" then lines$ = "= " + str$(first + second) + " " + lines$ : return
  214.     if op$ = "-" then lines$ = "= " + str$(first - second) + " " + lines$ : return
  215.     if op$ = "X" then lines$ = "= " + str$(first * second) + " " + lines$ : return
  216.     if op$ = "/" then lines$ = "= " + str$(first / second) + " " + lines$ : return
  217.     if op$ = "=" or op$ = "?" then lines$ = "= " + str$(second) + " " + lines$ : return
  218.  
  219.   return
  220.  
  221. [info]
  222.  
  223.     notice "About Calc"  + chr$(13) + "Calc, a Liberty BASIC Application"
  224.     goto [mainLoop]
  225.  
  226.