home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / devtools / files / lb202win.exe / LB202W.EXE / CALC2.BAS < prev    next >
Encoding:
BASIC Source File  |  2000-12-09  |  4.9 KB  |  213 lines

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