#USE obdefs.def
************ Declarations ************
dim i as long
dim x as long
dim row as long
dim tempstr as string *32
dim numarray(100,2) as float
dim opnumarray(100) as long
dim display as string * 11
dim acc as float
dim cur as float
dim tempfloat as float
dim memory as float
dim displayoperator as string * 6
dim opflag as byte
dim firstoperand as byte
dim pendingop as byte
dim keystrip as long
dim tmp1 as string * 12
dim tmp2 as string * 12
dim ftmp1 as float
dim ftmp2 as float
const div=1
const sq=2
const mul=3
const percent=4
const sub=5
const inverse=6
const add=7
const eq=8
const MC=1
const MR=2
const MS=3
const MA=4
const BackSpace = 1
const CE=2
const C=3
#SBRTN NumAction
#SBRTN OpsAction
#SBRTN MemAction
#SBRTN EditAction
************ Construct Objects ************
**************** OBMain ****************
OBMain.POSITION=210-4,195-22
OBMain.SIZE=252,360
OBMain.ENABLED=TRUE
OBMain.BACKCOLOR=$c0c0c0
OBMain.TEXT="OBMain"
**************** Edit(1) ****************
CONSTRUCT Edit(3) AS Button
Edit(1).POSITION=51,67
Edit(1).SIZE=62,28
Edit(1).VISIBLE=TRUE
Edit(1).ENABLED=TRUE
Edit(1).BACKCOLOR=$c0c0c0
Edit(1).TEXT="Backspace"
Edit(1).TEXTCOLOR=$80
Edit(1).FONT="Arial",8
**************** Edit(2) ****************
Edit(2).POSITION=118,67
Edit(2).SIZE=62,28
Edit(2).VISIBLE=TRUE
Edit(2).ENABLED=TRUE
Edit(2).BACKCOLOR=$c0c0c0
Edit(2).TEXT="CE"
Edit(2).TEXTCOLOR=$80
Edit(2).FONT="Arial",12
**************** Edit(3) ****************
Edit(3).POSITION=185,67
Edit(3).SIZE=62,28
Edit(3).VISIBLE=TRUE
Edit(3).ENABLED=TRUE
Edit(3).BACKCOLOR=$c0c0c0
Edit(3).TEXT="C"
Edit(3).TEXTCOLOR=$80
Edit(3).FONT="Arial",12
**************** Mem(1) ****************
CONSTRUCT Mem(4) AS Button
Mem(1).POSITION=9,104
Mem(1).SIZE=34,28
Mem(1).VISIBLE=TRUE
Mem(1).ENABLED=TRUE
Mem(1).BACKCOLOR=$c0c0c0
Mem(1).TEXT="MC"
Mem(1).TEXTCOLOR=$ff
Mem(1).FONT="Arial",11
**************** Mem(2) ****************
Mem(2).POSITION=9,137
Mem(2).SIZE=34,28
Mem(2).VISIBLE=TRUE
Mem(2).ENABLED=TRUE
Mem(2).BACKCOLOR=$c0c0c0
Mem(2).TEXT="MR"
Mem(2).TEXTCOLOR=$ff
Mem(2).FONT="Arial",11
**************** Mem(3) ****************
Mem(3).POSITION=9,170
Mem(3).SIZE=34,28
Mem(3).VISIBLE=TRUE
Mem(3).ENABLED=TRUE
Mem(3).BACKCOLOR=$c0c0c0
Mem(3).TEXT="MS"
Mem(3).TEXTCOLOR=$ff
Mem(3).FONT="Arial",11
**************** Mem(4) ****************
Mem(4).POSITION=9,203
Mem(4).SIZE=34,28
Mem(4).VISIBLE=TRUE
Mem(4).ENABLED=TRUE
Mem(4).BACKCOLOR=$c0c0c0
Mem(4).TEXT="M+"
Mem(4).TEXTCOLOR=$ff
Mem(4).FONT="Arial",11
**************** Num(1) ****************
CONSTRUCT Num(12) AS Button
Num(1).POSITION=51,203
Num(1).SIZE=34,28
Num(1).VISIBLE=TRUE
Num(1).ENABLED=TRUE
Num(1).BACKCOLOR=$c0c0c0
Num(1).TEXT="0"
Num(1).TEXTCOLOR=$ff0000
Num(1).FONT="Arial",11
**************** Num(2) ****************
Num(2).POSITION=89,203
Num(2).SIZE=34,28
Num(2).VISIBLE=TRUE
Num(2).ENABLED=TRUE
Num(2).BACKCOLOR=$c0c0c0
Num(2).TEXT="+/-"
Num(2).TEXTCOLOR=$ff0000
Num(2).FONT="Arial",11
**************** Num(3) ****************
Num(3).POSITION=127,203
Num(3).SIZE=34,28
Num(3).VISIBLE=TRUE
Num(3).ENABLED=TRUE
Num(3).BACKCOLOR=$c0c0c0
Num(3).TEXT="."
Num(3).TEXTCOLOR=$ff0000
Num(3).FONT="Arial",14
**************** Num(4) ****************
Num(4).POSITION=51,170
Num(4).SIZE=34,28
Num(4).VISIBLE=TRUE
Num(4).ENABLED=TRUE
Num(4).BACKCOLOR=$c0c0c0
Num(4).TEXT="1"
Num(4).TEXTCOLOR=$ff0000
Num(4).FONT="Arial",11
**************** Num(5) ****************
Num(5).POSITION=89,170
Num(5).SIZE=34,28
Num(5).VISIBLE=TRUE
Num(5).ENABLED=TRUE
Num(5).BACKCOLOR=$c0c0c0
Num(5).TEXT="2"
Num(5).TEXTCOLOR=$ff0000
Num(5).FONT="Arial",11
**************** Num(6) ****************
Num(6).POSITION=127,170
Num(6).SIZE=34,28
Num(6).VISIBLE=TRUE
Num(6).ENABLED=TRUE
Num(6).BACKCOLOR=$c0c0c0
Num(6).TEXT="3"
Num(6).TEXTCOLOR=$ff0000
Num(6).FONT="Arial",11
**************** Num(7) ****************
Num(7).POSITION=51,137
Num(7).SIZE=34,28
Num(7).VISIBLE=TRUE
Num(7).ENABLED=TRUE
Num(7).BACKCOLOR=$c0c0c0
Num(7).TEXT="4"
Num(7).TEXTCOLOR=$ff0000
Num(7).FONT="Arial",11
**************** Num(8) ****************
Num(8).POSITION=89,137
Num(8).SIZE=34,28
Num(8).VISIBLE=TRUE
Num(8).ENABLED=TRUE
Num(8).BACKCOLOR=$c0c0c0
Num(8).TEXT="5"
Num(8).TEXTCOLOR=$ff0000
Num(8).FONT="Arial",11
**************** Num(9) ****************
Num(9).POSITION=127,137
Num(9).SIZE=34,28
Num(9).VISIBLE=TRUE
Num(9).ENABLED=TRUE
Num(9).BACKCOLOR=$c0c0c0
Num(9).TEXT="6"
Num(9).TEXTCOLOR=$ff0000
Num(9).FONT="Arial",11
**************** Num(10) ****************
Num(10).POSITION=51,104
Num(10).SIZE=34,28
Num(10).VISIBLE=TRUE
Num(10).ENABLED=TRUE
Num(10).BACKCOLOR=$c0c0c0
Num(10).TEXT="7"
Num(10).TEXTCOLOR=$ff0000
Num(10).FONT="Arial",11
**************** Num(11) ****************
Num(11).POSITION=89,104
Num(11).SIZE=34,28
Num(11).VISIBLE=TRUE
Num(11).ENABLED=TRUE
Num(11).BACKCOLOR=$c0c0c0
Num(11).TEXT="8"
Num(11).TEXTCOLOR=$ff0000
Num(11).FONT="Arial",11
**************** Num(12) ****************
Num(12).POSITION=127,104
Num(12).SIZE=34,28
Num(12).VISIBLE=TRUE
Num(12).ENABLED=TRUE
Num(12).BACKCOLOR=$c0c0c0
Num(12).TEXT="9"
Num(12).TEXTCOLOR=$ff0000
Num(12).FONT="Arial",11
**************** Ops(1) ****************
CONSTRUCT Ops(8) AS Button
Ops(1).POSITION=173,104
Ops(1).SIZE=34,28
Ops(1).VISIBLE=TRUE
Ops(1).ENABLED=TRUE
Ops(1).BACKCOLOR=$c0c0c0
Ops(1).TEXT="/"
Ops(1).TEXTCOLOR=$ff
Ops(1).FONT="Arial",11
**************** Ops(2) ****************
Ops(2).POSITION=212,104
Ops(2).SIZE=34,28
Ops(2).VISIBLE=TRUE
Ops(2).ENABLED=TRUE
Ops(2).BACKCOLOR=$c0c0c0
Ops(2).TEXT="sqrt"
Ops(2).TEXTCOLOR=$800000
Ops(2).FONT="Arial",11
**************** Ops(3) ****************
Ops(3).POSITION=173,137
Ops(3).SIZE=34,28
Ops(3).VISIBLE=TRUE
Ops(3).ENABLED=TRUE
Ops(3).BACKCOLOR=$c0c0c0
Ops(3).TEXT="*"
Ops(3).TEXTCOLOR=$ff
Ops(3).FONT="Arial",11
**************** Ops(4) ****************
Ops(4).POSITION=212,137
Ops(4).SIZE=34,28
Ops(4).VISIBLE=TRUE
Ops(4).ENABLED=TRUE
Ops(4).BACKCOLOR=$c0c0c0
Ops(4).TEXT="%"
Ops(4).TEXTCOLOR=$800000
Ops(4).FONT="Arial",11
**************** Ops(5) ****************
Ops(5).POSITION=173,170
Ops(5).SIZE=34,28
Ops(5).VISIBLE=TRUE
Ops(5).ENABLED=TRUE
Ops(5).BACKCOLOR=$c0c0c0
Ops(5).TEXT="-"
Ops(5).TEXTCOLOR=$ff
Ops(5).FONT="Arial",11
**************** Ops(6) ****************
Ops(6).POSITION=212,170
Ops(6).SIZE=34,28
Ops(6).VISIBLE=TRUE
Ops(6).ENABLED=TRUE
Ops(6).BACKCOLOR=$c0c0c0
Ops(6).TEXT="1/x"
Ops(6).TEXTCOLOR=$800000
Ops(6).FONT="Arial",11
**************** Ops(7) ****************
Ops(7).POSITION=173,203
Ops(7).SIZE=34,28
Ops(7).VISIBLE=TRUE
Ops(7).ENABLED=TRUE
Ops(7).BACKCOLOR=$c0c0c0
Ops(7).TEXT="+"
Ops(7).TEXTCOLOR=$ff
Ops(7).FONT="Arial",11
**************** Ops(8) ****************
Ops(8).POSITION=212,203
Ops(8).SIZE=34,28
Ops(8).VISIBLE=TRUE
Ops(8).ENABLED=TRUE
Ops(8).BACKCOLOR=$c0c0c0
Ops(8).TEXT="="
Ops(8).TEXTCOLOR=$ff
Ops(8).FONT="Arial",11
**************** table ****************
CONSTRUCT table AS Table
table.POSITION=9,240
table.SIZE=235,112
table.VISIBLE=TRUE
table.ENABLED=TRUE
table.BACKCOLOR=$ffffff
**************** TextField1 ****************
CONSTRUCT TextField1 AS TextField
TextField1.POSITION=8,32
TextField1.SIZE=236,28
TextField1.VISIBLE=TRUE
TextField1.ENABLED=TRUE
TextField1.BACKCOLOR=$ffffff
TextField1.TEXT="0"
TextField1.TEXTCOLOR=$0
TextField1.FONT="Arial",11
**************** MemW ****************
CONSTRUCT MemW AS TextField
MemW.POSITION=15,70
MemW.SIZE=24,22
MemW.VISIBLE=TRUE
MemW.ENABLED=TRUE
MemW.BACKCOLOR=$ffffff
MemW.TEXT=""
MemW.TEXTCOLOR=$0
MemW.FONT="Helvetica",8
************ Initialize ************
table.tablesize=100,3
table.backcolor=OBYellow
table.gridcolor=OBGray
table.shadowcolor=OBMint
table.textcolor=OBDkGreen
table.selbackcolor=OBViolet
table.seltextcolor=OBYellow
table.colwidth=0,100
table.colwidth=1,20
table.colwidth=2,100
table.fixedrowstop=0
table.fixedrowsbottom=0
table.fixedcolsleft=0
table.width = 238
for i=0 to 99
table.rowheight=i,15
next i
tablepointer = 0
firstoperand = 0
DECIMALS 5
OBMain.Text = "Calculator"
************ Functions ************
*************************************************
NumAction
*************************************************
if opflag<>0 then
opflag=0
display=""
endif
IF OBSel=2 THEN
display = str$(FVAL(display)*(-1))
ELSE
tempstr=Num(OBSel).text
display=display+tempstr
ENDIF
cur = VAL(display) \variable cur equals display
TextField1.text=display \puts display in text field
Return
*************************************************
EditAction
*************************************************
Select Case OBSel
Case BackSpace
tempstr = display
IF LEN(tempstr)>1 THEN
display = LEFT$(tempstr,(LEN(tempstr)-1))
TextField1.text = display
ENDIF
Case CE
display="0"
TextField1.text=display
cur = 0
x=0
Case C
display = "0"
TextField1.text=display
cur = 0
x=0
acc = 0
pendingop = 0 \clears pending operation array
firstoperand = 0
for i = 0 to 99
table.textmatrix = i,0,"" \clears table
table.textmatrix = i,1,""
table.textmatrix = i,2,""
next i
row = 0
End Select
Return
*************************************************
MemAction
*************************************************
Select Case OBSel
Case MC
memory = 0
MemW.text = " "
Case MR
cur = memory
display = str$(cur)
TextField1.text=display
Case MS
memory = FVAL(display)
MemW.text = " M"
Case MA
memory = memory+FVAL(display)
MemW.text = " M"
End Select
Return
*************************************************
OpsAction
*************************************************
display=TextField1.text
x=val(display)
*getdisplay()
opflag=$ff
*The first row of table takes a special case
IF row = 0 then
IF firstoperand = 0 then
table.textmatrix = row,0,str$(cur) \puts current display in 1st row, 1st & 3rd col's
table.textmatrix = row,2,str$(cur)
ELSE
table.textmatrix = row+1,0,str$(cur) \puts successive displays in next row down
ENDIF
ELSE
table.textmatrix = row,0,str$(cur)
table.textmatrix = row,2,str$(cur)
END IF
pendingop = OBSel \key selection becomes pending operation
displayoperator = Ops(pendingop).text
table.textmatrix = row,1,displayoperator \displays operation in center col
opnumarray(row) = pendingop \puts pending operation in an array
*All Rows after the first one solves and places the result on the
*next row, first column. The first row doesn't solve until after
*the second operator.
IF row > 0 Then \after first row, it's necessary to always perform the previous operation
SELECT CASE opnumarray(row-1)
case add
tmp1= table.textmatrix(row,0) \typ example of current display operating on previous result
tmp2 = table.textmatrix(row-1,2)
ftmp1=fval(tmp1)
ftmp2=fval(tmp2)
acc=ftmp1 + ftmp2
case sub
tmp1= table.textmatrix(row,0)
tmp2 = table.textmatrix(row-1,2)
ftmp1=fval(tmp1)
ftmp2=fval(tmp2)
acc=ftmp2 - ftmp1
case div
tmp1= table.textmatrix(row,0)
tmp2 = table.textmatrix(row-1,2)
ftmp1=fval(tmp1)
ftmp2=fval(tmp2)
* * Test for divide by zero condition
IF ftmp1<>0 THEN
acc=ftmp2/ftmp1
ELSE
TextField1.text = "Attempted divide by zero"
ENDIF
case mul
tmp1= table.textmatrix(row,0)
tmp2 = table.textmatrix(row-1,2)
ftmp1=fval(tmp1)
ftmp2=fval(tmp2)
acc=ftmp1 * ftmp2
case sq
tmp1= table.textmatrix(row,0)
ftmp1=fval(tmp1)
acc=SQR(ftmp1)
case percent
tmp1= table.textmatrix(row,0)
tmp2 = table.textmatrix(row-1,2)
ftmp1=fval(tmp1)
ftmp2=fval(tmp2)
tempfloat = ftmp2/100
acc = ftmp1 * tempfloat
case inverse
tempfloat = FVAL(display)
acc=tempfloat/1
display = str$(acc)
TextField1.text = display
END SELECT
table.textmatrix = row,2,str$(acc)
ELSE
acc=cur \for first row, the current value becomes the accumulated value
ENDIF
firstoperand = 1
row = row + 1
IF row = 100 then
row = 0
ENDIF
display=str$(acc) \displays accumulated value
TextField1.text=display
Return
************ Events ************
REM *** Number Keys
Num.click
Gosub NumAction
EXIT EVENT
REM *** Edit Keys
Edit.click
Gosub EditAction
Exit Event
REM *** Memory Keys
Mem.click
Gosub MemAction
Exit Event
REM *** Operations Keys
Ops.click
Gosub OpsAction
Exit Event
OBMain.keydown
keystrip=OBCode
IF OBCode > $ffaf AND OBCode< $ffba Then
keystrip = OBCode - $ffb0
Endif
IF OBCode >$2f AND OBCode < $3a Then
keystrip= OBCode - $30
Endif
IF OBCode=$ff0d OR OBCode=$3d THEN
keystrip=$3d
ENDIF
IF OBCode>$ffa9 AND OBCode<$ffb0 THEN
keystrip=OBCode-$ff80
ENDIF
IF keystrip < 10 AND keystrip > 0 THEN
Num(keystrip+3).state = 1
OBSel = keystrip+3
Gosub NumAction
ENDIF
If keystrip = 0 THEN
Num(1).state = 1
OBSel = 1
Gosub NumAction
ENDIF
IF keystrip >9 THEN
Select Case keystrip
Case $2f \ divide key /
OBSel=1
Ops(1).state=1
Gosub OpsAction
Case $2e \ decimal key .
OBSel=3
Num(3).state=1
Gosub NumAction
Case $2d \ minus key -
OBSel=5
Ops(5).state=1
Gosub OpsAction
Case $2b \ plus key +
OBSel=7
Ops(7).state=1
Gosub OpsAction
Case $2a \ multiply key *
OBSel=3
Ops(3).state=1
Gosub OpsAction
Case $3d \ equal key =
OBSel=8
Ops(8).state=1
Gosub OpsAction
Case $d \ Enterkey
OBSel=8
Ops(8).state=1
Gosub OpsAction
Case $25 \ percent key %
OBSel=4
Ops(4).state=1
Gosub OpsAction
Case $ff1b \ Esc key for clear
OBSel=3
Edit(3).state=1
Gosub EditAction
Case $ffff \ Del key for Clear Entry
OBSel=2
Edit(2).state=1
Gosub EditAction
Case $ff08 \ Backspace key
OBSel=1
Edit(1).state=1
Gosub EditAction
EndSelect
EndIf
keystrip =$ff & OBCode
MemW.Text=HEX$(keystrip)
Exit Event
OBMain.keyup
For i=1 to 12
If i <4 THEN
Edit(i).state=0
ENDIF
If i < 5 THEN
Mem(i).state=0
ENDIF
If i <9 THEN
Ops(i).state=0
ENDIF
Num(i).state=0
Next i
Exit Event
|