'===========================================================================
' Subject: 4 FUNCTION CALCULATOR              Date: 06-28-96 (02:33)       
'  Author: Denis Boyles                       Code: QB, QBasic, PDS        
'  Origin: FidoNet QUIK_BAS Echo            Packet: ALGOR.ABC
'===========================================================================
' > I am working on a calculator program, but the biggest problem is how
' > do I enter the numbers from right to left, instead of the normal left
' > to right?  I want to do this with INKEY$,

' Here's my 00000011 bits worth. The following code I whipped up does that
' and serves as a simple 4 function calculator too. Perhaps it will give
' you some ideas.

' Enter a number and it will move in left on the "display". Then pick a
' function and finally press = for your answer. You're not limited to single
' one time calculations either. You can keep going until you press = for
' the final result.

' ie:
'  100 * 320 + 160 = 32160

DECLARE FUNCTION GetNum$ (lcd$)

CLS
PRINT "+---------+"
PRINT "|         |"
PRINT "+---------+"

lcd$ = SPACE$(9)
op$ = GetNum(lcd$)
n& = VAL(LTRIM$(lcd$))

DO
  SELECT CASE op$
    CASE "+"
      lcd$ = SPACE$(9)
      op$ = GetNum(lcd$)
      n& = n& + VAL(LTRIM$(lcd$))

    CASE "-"
      lcd$ = SPACE$(9)
      op$ = GetNum(lcd$)
      n& = n& - VAL(LTRIM$(lcd$))

    CASE "*"
      lcd$ = SPACE$(9)
      op$ = GetNum(lcd$)
      n& = n& * VAL(LTRIM$(lcd$))

    CASE "/"
      lcd$ = SPACE$(9)
      op$ = GetNum(lcd$)
      n& = n& / VAL(LTRIM$(lcd$))
  END SELECT
LOOP UNTIL op$ = "="

a$ = LTRIM$(STR$(n&))
lcd$ = SPACE$(9 - LEN(a$)) + a$
LOCATE 2, 2
PRINT lcd$

FUNCTION GetNum$ (lcd$)
  DO
    key$ = INKEY$
    IF key$ >= "0" AND key$ <= "9" THEN
      lcd$ = MID$(lcd$, 2, 8) + key$
      LOCATE 2, 2
      PRINT lcd$
    END IF
  LOOP UNTIL key$ = "+" OR key$ = "-" OR key$ = "*" OR key$ = "/" OR key$ = "="
  GetNum$ = key$
END FUNCTION
