• March 29, 2021

Mobile Phone Scientific Calculator

A Scientific Calculator with Persistent Memory written using only 5K of Mobile BASIC source code.

The program can be made into a Mobile Phone MIDlet or a Website Applet without modification. The Applet to the left is controlled by moving the cursor with the arrow keys. The highlighted calculator key is pressed by hitting the space bar.

Calculator WAP Browser Installation

To install Calculator onto your Mobile Phone over WAP simply start your Mobile Phones WAP Browser and visit one of the following links:-

http://www.mobilebasic.com/FreeStuff/Calculator-Nokia.wml (Nokia Phones)
http://www.mobilebasic.com/FreeStuff/Calculator.wml (Other Phones)
Calculator for Nokia Mobile Phones
Calculator-Nokia.jad (Nokia Only)
Calculator-Nokia.jar (Nokia Only)
Calculator for Other Mobile Phones
Calculator.jad (Standard J2ME Phones)
Calculator.jar (Standard J2ME Phones)
Mobile BASIC Source Code for the above program

SUB MAIN
REM
REM Calculator Engine Rules
REM 1. Functions operate on displayed value
REM 2. Operators operate on Accumulator Values
REM

MENUFLAG%=0
TRAP NoMenus
EXIT$=”Finish”
MENU_ADD EXIT$,7,1
MENUFLAG%=1

NoMenus:
TRAP OFF

MAPLOAD “Display.map”,”Tiles.til”
GELLOAD “KeyReleased”,”Cursor.png”
GELLOAD “KeyPressed”,”Cursor2.png”

H%=STRINGHEIGHT(“X”)
VPW%=MAPWIDTH()
VPH%=MAPHEIGHT()
VPX%=(SCREENWIDTH()-VPW%)/2
VPY%=H%

SETCOLOR 0,0,0
FILLRECT 0,0,SCREENWIDTH(),SCREENHEIGHT()

SETVIEWPORT VPX%,VPY%,VPW%,VPH%

S$=”Calculator – Powered By Mobile BASIC – The easy way to write mobile phone programs. See http://www.mobilebasic.com/ for Details.”

X%=SCREENWIDTH()
W%=STRINGWIDTH(S$)
WHILE (INKEY()=0) AND (X%+W%>0)
SETCOLOR 0,0,0
FILLRECT 0,0,SCREENWIDTH(),H%
SETCOLOR 255,255,255
DRAWSTRING S$,X%,0
REPAINT
X%=X%-1
SLEEP 10
IF (MENUFLAG% = 1) THEN
IF (MENUITEM() = EXIT$) THEN GOSUB Finish
ENDIF
ENDWHILE

REPEAT
SLEEP 1
UNTIL INKEY()=0

SPRITEGEL “Cursor”,”KeyReleased”

X%=1
Y%=2

GETPROPERTY “M0”,M$

IF M$<>”” THEN
MEMORY=FLOAT(M$)
ELSE
MEMORY=0.0
ENDIF

ACCUMULATOR=0.0
DISPLAY=0.0
PREV_OPCODE%=0
NEW_NUMBER_FLAG%=1
ErrorState%=0

GOSUB MoveCursor
GOSUB UpdateDisplay

WHILE 1
IF (LEFT()<>0) THEN
X%=X%-1
IF (X% < 0) THEN X%=5
GOSUB MoveCursor
ENDIF

IF (RIGHT()<>0) THEN
X%=X%+1
IF (X% > 5) THEN X%=0
GOSUB MoveCursor
ENDIF

IF (UP()<>0) THEN
Y%=Y%-1
IF (Y% < 0) THEN Y%=5
GOSUB MoveCursor
ENDIF

IF (DOWN()<>0) THEN
Y%=Y%+1
IF (Y% > 5) THEN Y%=0
GOSUB MoveCursor
ENDIF

IF (FIRE()<>0) THEN
IF (ErrorState%) THEN
IF (Y%=0 AND X%=0) THEN
DISPLAY=0.0
NEW_NUMBER_FLAG%=1
ACCUMULATOR=0.0
PREV_OPCODE%=0
ErrorState%=0
GOSUB UpdateDisplay
ENDIF
ELSE
TRAP Error

  OPCODE%=-1
  DIGIT%=-2

  IF (Y%=0) THEN
    IF (X%=0) THEN DISPLAY=0.0:NEW_NUMBER_FLAG%=1:ACCUMULATOR=0.0:PREV_OPCODE%=0:ErrorState%=0
    IF (X%=1) THEN DISPLAY=0.0:NEW_NUMBER_FLAG%=1
    IF (X%=2) THEN
      MEMORY=DISPLAY
      SETPROPERTY "M0",STR$(MEMORY)
      NEW_NUMBER_FLAG%=1
    ENDIF
    IF (X%=3) THEN DISPLAY=MEMORY:NEW_NUMBER_FLAG%=1
    IF (X%=4) THEN MEMORY=MEMORY+DISPLAY:NEW_NUMBER_FLAG%=1
    IF (X%=5) THEN MEMORY=MEMORY-DISPLAY:NEW_NUMBER_FLAG%=1
  ENDIF

  IF (Y%=1) THEN
    IF (X%=0) THEN DIGIT%=7
    IF (X%=1) THEN DIGIT%=8
    IF (X%=2) THEN DIGIT%=9
    IF (X%=3) THEN OPCODE%=1
    IF (X%=4) THEN DISPLAY=SIND(DISPLAY):NEW_NUMBER_FLAG%=1
    IF (X%=5) THEN DISPLAY=ASIND(DISPLAY):NEW_NUMBER_FLAG%=1
  ENDIF

  IF (Y%=2) THEN
    IF (X%=0) THEN DIGIT%=4
    IF (X%=1) THEN DIGIT%=5
    IF (X%=2) THEN DIGIT%=6
    IF (X%=3) THEN OPCODE%=2
    IF (X%=4) THEN DISPLAY=COSD(DISPLAY):NEW_NUMBER_FLAG%=1
    IF (X%=5) THEN DISPLAY=ACOSD(DISPLAY):NEW_NUMBER_FLAG%=1
  ENDIF

  IF (Y%=3) THEN
    IF (X%=0) THEN DIGIT%=1
    IF (X%=1) THEN DIGIT%=2
    IF (X%=2) THEN DIGIT%=3
    IF (X%=3) THEN OPCODE%=3
    IF (X%=4) THEN DISPLAY=TAND(DISPLAY):NEW_NUMBER_FLAG%=1
    IF (X%=5) THEN DISPLAY=ATAND(DISPLAY):NEW_NUMBER_FLAG%=1
  ENDIF

  IF (Y%=4) THEN
    IF (X%=0) THEN DIGIT%=0
    IF (X%=1) THEN DIGIT%=-1
    IF (X%=2) THEN OPCODE%=5
    IF (X%=3) THEN OPCODE%=4
    IF (X%=4) THEN DISPLAY=LOG(DISPLAY):NEW_NUMBER_FLAG%=1
    IF (X%=5) THEN DISPLAY=EXP(DISPLAY):NEW_NUMBER_FLAG%=1
  ENDIF

  IF (Y%=5) THEN
    IF (X%=0) THEN DISPLAY=-DISPLAY
    IF (X%=1) THEN DISPLAY=3.1415926:NEW_NUMBER_FLAG%=1
    IF (X%=2) THEN DISPLAY=DISPLAY*DISPLAY:NEW_NUMBER_FLAG%=1
    IF (X%=3) THEN DISPLAY=SQR(DISPLAY):NEW_NUMBER_FLAG%=1
    IF (X%=4) THEN DISPLAY=LOG(DISPLAY)/LOG(10):NEW_NUMBER_FLAG%=1
    IF (X%=5) THEN DISPLAY=EXP(DISPLAY*LOG(10)):NEW_NUMBER_FLAG%=1
  ENDIF

  IF (DIGIT% <> -2) THEN
    IF (NEW_NUMBER_FLAG%) THEN
      DISPLAY=0.0
      MULT=10.0
      DIV=1.0
      NEW_NUMBER_FLAG% = 0
    ENDIF

    IF (DIGIT% = -1) THEN
      IF (DIV = 1.0) THEN
        MULT=1.0
        DIV=10.0
      ENDIF
    ELSE
      DISPLAY=DISPLAY*MULT+DIGIT%/DIV
      IF (DIV <> 1.0) THEN DIV=DIV*10
    ENDIF
  ENDIF

  IF (OPCODE% <> -1) THEN
    IF (PREV_OPCODE%=0) THEN ACCUMULATOR=DISPLAY
    IF (PREV_OPCODE%=1) THEN ACCUMULATOR=ACCUMULATOR*DISPLAY:DISPLAY=ACCUMULATOR
    IF (PREV_OPCODE%=2) THEN ACCUMULATOR=ACCUMULATOR/DISPLAY:DISPLAY=ACCUMULATOR
    IF (PREV_OPCODE%=3) THEN ACCUMULATOR=ACCUMULATOR+DISPLAY:DISPLAY=ACCUMULATOR
    IF (PREV_OPCODE%=4) THEN ACCUMULATOR=ACCUMULATOR-DISPLAY:DISPLAY=ACCUMULATOR
    NEW_NUMBER_FLAG%=1
    IF (OPCODE%=5) THEN
      PREV_OPCODE%=0
    ELSE
      PREV_OPCODE%=OPCODE%
    ENDIF
  ENDIF

  GOSUB UpdateDisplay
  GOTO NoError

Error:
TRAP OFF
ErrorState%=1
GOSUB ErrorDisplay

NoError:
ENDIF

SPRITEGEL "Cursor","KeyPressed"

ENDIF

REPAINT

REPEAT
SLEEP 1
UNTIL INKEY()=0

SPRITEGEL “Cursor”,”KeyReleased”
REPAINT

REPEAT
IF (MENUFLAG% = 1) THEN
IF (MENUITEM() = EXIT$) THEN GOSUB Finish
ENDIF
SLEEP 1
UNTIL INKEY()<>0
ENDWHILE
END

MoveCursor:
SPRITEMOVE “Cursor”,VPX%+X%16,VPY%+Y%8+1
RETURN

UpdateDisplay:
SETCOLOR 0,0,0
FILLRECT 0,0,SCREENWIDTH(),H%
SETCOLOR 255,255,255
S$=STR$(DISPLAY)
W%=STRINGWIDTH(S$)
DRAWSTRING S$,SCREENWIDTH()-W%,0
RETURN

ErrorDisplay:
SETCOLOR 0,0,0
FILLRECT 0,0,SCREENWIDTH(),H%
SETCOLOR 255,255,255
S$=”Error”
W%=STRINGWIDTH(S$)
DRAWSTRING S$,SCREENWIDTH()-W%,0
RETURN

Finish:
IF (MENUFLAG% = 1) THEN
I%=SELECT(“Exit Calculator?”,”Yes,No”)
IF I%=0 THEN END
RETURN
ENDIF
ENDSUB

Leave a Reply

Your email address will not be published. Required fields are marked *