Post by marcioc on Jan 8, 2009 17:07:51 GMT -5
One more Calculator program for the collection. Enjoy. Comments are very welcome.
Stefan: moved source code into formated text box
'*************Calculator ***********
'**************Programmed by Marcio Cracel 01/07/2009 Potomac MD USA *****************
global loga$
cssclass "table", "{ width: 300px; border: 6px ridge #CCF ; border-spacing: 2px }"
cssclass "tr", "{ background: #CCF }"
tot=0
swork$=""
work$=""
salva=0
sign$=""
loga$="Calculator log "+date$()+chr$(13)
[loop]
cls
a$=stylex$()
html "<table border='2' align='center'><th>":print "Calculator": html"</th></table>"
html "<table border='2' align='center'>"
html "<th><input type='text' size='28' readonly style='text-align:right' value='"+swork$+"' /></th></table>"
html "<table border='2' align='center' >"
html "<th>":print " ":html "</th>"
html "<th>":button #a,"Bsp ",[bsp]:html "</th><th>":button #b," CE ",[ce]:html "</th>"
html "<th>":button #c," C ",[clear]:html "</th><th>":button #d,"Exit",[exit]:html "</th>"
html "<tr></tr>"
html "<th>":button #7," 7 ",[proc]:html "</th><th>":button #8," 8 ",[proc]:html "</th>"
html "<th>":button #9," 9 ",[proc]:html "</th><th>":button #/," / ",[oper]:html "</th>"
html "<th>":button #sqrt,"sqrt",[sqrt]:html "</th>"
html "<tr></tr>"
html "<th>":button #4," 4 ",[proc]:html "</th><th>":button #5," 5 ",[proc]
html "<th>":button #6," 6 ",[proc]:html "</th><th>":button #*," * ",[oper]
html "<th>":button #%," % ",[%]:html "</th>"
html "<tr></tr>"
html "<th>":button #1," 1 ",[proc]:html "</th><th>":button #2," 2 ",[proc]:html "</th>"
html "<th>":button #3," 3 ",[proc]:html "</th><th>":button #-," - ",[oper]:html "</th>"
html "<th>":button #x," 1/x ",[1/x]:html "</th>"
html "<tr></tr>"
html "<th>":button #0," 0 ",[proc]:html "</th><th>":button #mm," +/- ",[+/-]:html "</th>"
html "<th>":button #.," . ",[proc]:html "</th><th>":button #+," + ",[oper]:html "</th>"
html "<th>":button #=," = ",[oper]:html "</th>"
html "</table>"
html "<table border='2' align='center'>"
html "<th>":button #cl,"Clear Log",[clog]:html "</th><th>":button #sl,"Save Log",[slog]:html "<th></th></table>"
html "<table border='2' align='center'>"
html "<th><textarea readonly rows='6' cols='32'>"+loga$+"</textarea></th></table>"
wait
[exit]
cls
end
'********Clear Log *********
[clog]
loga$="Calculator log "+date$()+chr$(13)
goto [loop]
'********* Save Log *********
[slog]
html "<table border='2' align='center'><th>":Print "Inform File Name : "
html "<input type='text' size='10' name='file' id='file'/></th><th>"
button #aa, "Confirm",[conf]: print " ":button #cc,"Cancel",[loop]:html "</th></table>"
a$=setfocus$("file")
wait
[conf]
nfile$= #request get$("file")
if len(nfile$)=0 then goto [loop]
open nfile$ for output as #f
i=1
while len(word$(loga$,i,chr$(13))) <> 0
lina$=word$(loga$,i,chr$(13))
print #f,lina$
i=i+1
wend
close #f
goto [loop]
'********* Backspace ********
[bsp]
if len(work$)>0 then
work$=mid$(work$,1,len(work$)-1)
swork$=work$
end if
goto [loop]
'*****+/-************
[+/-]
if len(work$)=0 then
tot=tot*-1
swork$=str$(tot)
else
work$=str$(val(work$)*-1)
swork$=work$
end if
a$=wlog$("Total = "+str$(tot))
goto [loop]
'***** CE ************
[ce]
work$=""
swork$=""
goto [loop]
'********* Accumulate ********
[proc]
if mid$(EventKey$,2,1)= "." and instr(work$,".") <> 0 then goto [loop] 'allows just 1 decimal
work$=work$+mid$(EventKey$,2,1)
swork$=work$
goto [loop]
'*******SQRT **********
[sqrt]
if len(work$)=0 then
tot=sqr(tot)
swork$=str$(tot)
else
work$=str$(sqr(val(work$)))
swork$=work$
end if
a$=wlog$("sqrt")
a$=wlog$("Total = "+str$(tot))
goto [loop]
'******* % ***********
[%]
tot=val(work$)*tot/100
a$=wlog$("%")
goto [out]
'****** 1/x **********
[1/x]
if tot=0 then goto [loop] 'divide by zero
if len(work$)=0 then
tot=1/tot
swork$=str$(tot)
else
work$=str$(1/val(work$))
swork$=work$
end if
a$=wlog$("1/x")
a$=wlog$("Total = "+str$(tot))
goto [loop]
'******Calculate ********
[oper]
if len(work$)=0 then
work$=str$(salva)
end if
if sign$ = "=" then goto [out]
if len(sign$)=0 then
tot=val(work$)
goto [out]
end if
select case sign$
case "+"
tot=tot+val(work$)
case "-"
tot=tot-val(work$)
case "*"
tot=tot*val(work$)
case "/"
if val(work$) <> 0 then tot=tot/val(work$) 'divide by zero
end select
[out]
if len(sign$) > 0 then
a$=wlog$(sign$+" "+work$)
else
a$=wlog$(work$)
end if
a$=wlog$("Total = "+str$(tot))
sign$=mid$(EventKey$,2,1)
salva=val(work$)
work$=""
swork$=str$(tot)
goto [loop]
'******* Clear all ******
[clear]
tot=o
work$=""
swork$=""
sign$=""
a$=wlog$("Clear")
goto [loop]
'*********Function Record Log *********
function wlog$(mes$)
loga$=mes$+chr$(13)+loga$
end function
'********** Function Set focus ***************
function setfocus$(name$)
html "<script
type='text/javascript'
language='javascript'>
document.getElementById('"+name$+"').focus();
</script>"
end function
'*********Function Style **********
function stylex$()
cssclass "table", "{ width: 300px; border: 6px ridge #CCF ; border-spacing: 2px }"
cssclass "tr", "{ background: #CCF }"
html "<style type='text/css'> <!--INPUT { color: red;
font-size:15px;
font-weight: bold;
Border-Width: Thick;
Border-Style: Outset;
Border-Color: #AAAAAA
}</style>"
end function
Stefan: moved source code into formated text box