Calculadora 2


3 parts to this script

=====================================================

PART 1

====================================================

<SCRIPT>

<!--


var opStack = new Array(4)

opStack[0] = 0

opStack[1] = ''

opStack[2] = ''

opStack[3] = ''

function start()

{

document.PAD.SUM.value= "0"

}

function KeyinNum()

{

if (opStack[0] >= 2) { opStack[0] = 3 } else { opStack[0] = 1 }

opStack[opStack[0]] = '' + document.PAD.SUM.value

}

function display ()

{

var sum = parseFloat(opStack[1])

var huge = Math.pow(10,14)

sum = Math.round (sum * huge) / huge

document.PAD.SUM.value = '' + sum

}

function resetNum ()

{

opStack[0] = 0

opStack[1] = 0

document.PAD.CAL.value = ''

document.PAD.SUM.value = '0'

}

function entry (x)

{

if (opStack[0] == -1) { opStack[0] = 1; opStack[1] = ''}

if (opStack[0] == 0) { opStack[0] = 1; opStack[1] = ''}

if (opStack[0] == 2) { opStack[0] = 3; opStack[3] = ''}

var result = result = opStack[opStack[0]]

if (result == '0') { result = '' }

//---------------------------------------------------------------------

if (x>='1' && x<='9') { result = '' + result + x }

else if (x == 'P') { result = '' + Math.PI }

else if (x == '0') { if (result != '') result = '' + result + '0' }

else if (x == 'B') { if (result != '') result = result.substring(0, result.length - 1) }

else if (x == '.') {

if (result != '')

{ if (result.indexOf(".") == -1) result += "." }

else

{ result = '0.' }

}

//-----------------------------------------------------------------

if (result =='') result = '0'

opStack[opStack[0]] = result

document.PAD.SUM.value = result

}

function neg()

{

if (opStack[0] != 2 && opStack[0] != 0)

{

opStack[0] = Math.abs(opStack[0])

var result = opStack[opStack[0]]

if (result != '0' && result != '')

{

if (result.charAt(0) == '-')

{ result = result.substring(1, result.length) }

else

{ result = '-' + result }

opStack[opStack[0]] = result

document.PAD.SUM.value = result

}

}

}

function calc1 (x)

{

var opFlag = opStack[0]

if (opFlag == -1 || opFlag == 1)

{

count(x)

}

else if (opFlag == 3)

{

opStack[1] = '' + eval(opStack[1] + opStack[2] + opStack[3])

count(x)

}

}

function count (x)

{

if (x == 'Si') { opStack[1] = '' + Math.sin (opStack[1]) }

else if (x == 'aS') { opStack[1] = '' + Math.asin(opStack[1]) }

else if (x == 'Co') { opStack[1] = '' + Math.cos (opStack[1]) }

else if (x == 'aC') { opStack[1] = '' + Math.acos(opStack[1]) }

else if (x == 'Ta') { opStack[1] = '' + Math.tan (opStack[1]) }

else if (x == 'aT') { opStack[1] = '' + Math.atan(opStack[1]) }

else if (x == '¡Ô') { opStack[1] = '' + Math.pow (opStack[1],.5) }

else if (x == '^2') { opStack[1] = '' + Math.pow (opStack[1], 2) }

else if (x == '^3') { opStack[1] = '' + Math.pow (opStack[1], 3) }

else if (x == '^4') { opStack[1] = '' + Math.pow (opStack[1], 4) }

else if (x == 'AB') { opStack[1] = '' + Math.abs (opStack[1]) }

else if (x == '¡×') { }

document.PAD.CAL.value = ''

opStack[0] = -1

display()

document.PAD.SUM.focus()

document.PAD.SUM.select()

}

function calc2 (x)

{

var opFlag = opStack[0]

if (opFlag != 2)

{

if (opFlag == 3)

{

opStack[1]=''+eval(opStack[1]+opStack[2]+opStack[3])

display()

}

opStack[0] = 2

opStack[2] = x

document.PAD.CAL.value = x

document.PAD.SUM.focus()

document.PAD.SUM.select()

}

}

function fra()

{

var i

var j = 1

var k = document.PAD.SUM.value

if (k >= 70)

{opStack[1] = 0; document.PAD.SUM.value = "---Error---"; return}

for (i = 1; i <= k; i++)

{

j = j * i

}

opStack[1] = document.PAD.SUM.value = j

document.PAD.SUM.focus()

document.PAD.SUM.select()

}

function TurnOff()

{

var ask = confirm("This will close the window, continue?")

if (ask == true) {window.close()}

else {return}

}

//-->

</SCRIPT>

==============================================================

PART 2

===========================================================

<BODY ONLOAD="start()">


===============================================================

PART 3 - put this where you want the calculator to show

================================================================

<!-- --><FORM NAME="PAD">

<TABLE BORDER>

<TR><TD COLSPAN=8>

<INPUT NAME="CAL" TYPE="TEXT" SIZE=1 VALUE="">

Result:

<INPUT NAME="SUM" TYPE="TEXT" SIZE=36 VALUE="" onChange="KeyinNum()">

<TR>

<TD><INPUT TYPE="BUTTON" VALUE="C" onClick="resetNum()" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="OFF" onClick="TurnOff()" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="Square" onClick="calc1('^2')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="X !" onClick="fra()" WIDTH=100>

<TR>

<TD><INPUT TYPE="BUTTON" VALUE="1" onClick="entry('1')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="2" onClick="entry('2')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="3" onClick="entry('3')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="+" onClick="calc2('+')" WIDTH=100>

<TR>

<TD><INPUT TYPE="BUTTON" VALUE="4" onClick="entry('4')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="5" onClick="entry('5')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="6" onClick="entry('6')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="-" onClick="calc2('-')" WIDTH=100>

<TR>

<TD><INPUT TYPE="BUTTON" VALUE="7" onClick="entry('7')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="8" onClick="entry('8')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="9" onClick="entry('9')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="X" onClick="calc2('*')" WIDTH=100>

<TR>

<TD><INPUT TYPE="BUTTON" VALUE="0" onClick="entry('0')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="." onClick="entry('.')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="=" onClick="calc1('=')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="÷" onClick="calc2('/')" WIDTH=100>

<TR>

<TD><INPUT TYPE="BUTTON" VALUE="Backspace" onClick="entry('B')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="SQRT" onClick="calc1('¡Ô')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="Pi" onClick="entry('P')" WIDTH=100>

<TD><INPUT TYPE="BUTTON" VALUE="+/-" onClick="neg()" WIDTH=100>

</TABLE>

</FORM><!-- -->



0 comentários: