William Luis

0 %
William Luis
Fullstack Developer
  • Residence:
    Canada
  • City:
    Toronto
  • Age:
    26
French
English
Spanish
html
CSS
Js
PHP
WordPress
  • Bootstrap, Materialize
  • Stylus, Sass, Less
  • Gulp, Webpack, Grunt
  • GIT knowledge

Calculadora feito em Javascript

25 de outubro de 2015

Trago dois fontes de calculadora feito em Javascript.

Fonte1

[sourcecode language=”HTML”]
<html>

<head>
<title>calcul2</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<meta name="Microsoft Theme" content="none">
<meta name="Microsoft Border" content="none">
</head>

<body>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</p>

<form name="Keypad" action>
<table>
<b>
<tr>
<td><table border="2" width="50" height="60" cellpadding="1" cellspacing="5">
<tr>
<td colspan="3" align="middle"><input name="ReadOut" size="24" value="0" width="100%"> </td>
<td </TD>&nbsp;</td>
<td><input name="btnClear" type="Button" value=" C " onclick="Clear()"> </td>
<td><input name="btnClearEntry" type="Button" value=" CE " onclick="ClearEntry()"> </td>
</tr>
<tr>
<td><input name="btnSeven" type="Button" value=" 7 " onclick="NumPressed(7)"> </td>
<td><input name="btnEight" type="Button" value=" 8 " onclick="NumPressed(8)"> </td>
<td><input name="btnNine" type="Button" value=" 9 " onclick="NumPressed(9)"> </td>
<td>&nbsp;</td>
<td><input name="btnNeg" type="Button" value=" +/- " onclick="Neg()"> </td>
<td><input name="btnPercent" type="Button" value=" % " onclick="Percent()"> </td>
</tr>
<tr>
<td><input name="btnFour" type="Button" value=" 4 " onclick="NumPressed(4)"> </td>
<td><input name="btnFive" type="Button" value=" 5 " onclick="NumPressed(5)"> </td>
<td><input name="btnSix" type="Button" value=" 6 " onclick="NumPressed(6)"> </td>
<td>&nbsp;</td>
<td align="middle"><input name="btnPlus" type="Button" value=" + "
onclick="Operation(‘+’)"> </td>
<td align="middle"><input name="btnMinus" type="Button" value=" – "
onclick="Operation(‘-‘)"> </td>
</tr>
<tr>
<td><input name="btnOne" type="Button" value=" 1 " onclick="NumPressed(1)"> </td>
<td><input name="btnTwo" type="Button" value=" 2 " onclick="NumPressed(2)"> </td>
<td><input name="btnThree" type="Button" value=" 3 " onclick="NumPressed(3)"> </td>
<td>&nbsp;</td>
<td align="middle"><input name="btnMultiply" type="Button" value=" * "
onclick="Operation(‘*’)"> </td>
<td align="middle"><input name="btnDivide" type="Button" value=" / "
onclick="Operation(‘/’)"> </td>
</tr>
<tr>
<td><input name="btnZero" type="Button" value=" 0 " onclick="NumPressed(0)"> </td>
<td><input name="btnDecimal" type="Button" value=" . " onclick="Decimal()"> </td>
<td colspan="3">&nbsp;</td>
<td><input name="btnEquals" type="Button" value=" = " onclick="Operation(‘=’)"> </td>
</tr>
</table>
</td>
</tr>
</table>
</b>
</form>
<font face="Verdana, Arial, Helvetica" size="2">
<script LANGUAGE="JavaScript">
<!– Begin
var FKeyPad = document.Keypad;
var Accum = 0;
var FlagNewNum = false;
var PendingOp = "";
function NumPressed (Num) {
if (FlagNewNum) {
FKeyPad.ReadOut.value = Num;
FlagNewNum = false;
}
else {
if (FKeyPad.ReadOut.value == "0")
FKeyPad.ReadOut.value = Num;
else
FKeyPad.ReadOut.value += Num;
}
}
function Operation (Op) {
var Readout = FKeyPad.ReadOut.value;
if (FlagNewNum && PendingOp != "=");
else
{
FlagNewNum = true;
if ( ‘+’ == PendingOp )
Accum += parseFloat(Readout);
else if ( ‘-‘ == PendingOp )
Accum -= parseFloat(Readout);
else if ( ‘/’ == PendingOp )
Accum /= parseFloat(Readout);
else if ( ‘*’ == PendingOp )
Accum *= parseFloat(Readout);
else
Accum = parseFloat(Readout);
FKeyPad.ReadOut.value = Accum;
PendingOp = Op;
}
}
function Decimal () {
var curReadOut = FKeyPad.ReadOut.value;
if (FlagNewNum) {
curReadOut = "0.";
FlagNewNum = false;
}
else
{
if (curReadOut.indexOf(".") == -1)
curReadOut += ".";
}
FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry () {
FKeyPad.ReadOut.value = "0";
FlagNewNum = true;
}
function Clear () {
Accum = 0;
PendingOp = "";
ClearEntry();
}
function Neg () {
FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1;
}
function Percent () {
FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accum);
}
// End –>
</script>

<p>&nbsp;</p>
</font>
</body>
</html>
[/sourcecode]

Fonte2

[sourcecode language=”HTML”]
<html><head><title>Calculadora</title><SCRIPT LANGUAGE="JavaScript">
<!–

var wynik=0, op=0, nowe=0, nowe2=0, done=1, oset=0, kropka, temp;

function reset(value)
{
document.form1.ekran.value = value;
wynik = 0,
op = 0,
nowe = 0,
nowe2 = 0;
done = 1;
oset = 0;
}

function wspolna(new_temp)
{
kropka = 1;

if(nowe || done) {
nowe = 0;
done = 0;
temp = new_temp;
}
{
if (temp.indexOf(".")!=-1) {
kropka=0;
}
}
}

function button(ktory,ktory2)
{

temp = document.form1.ekran.value;

if(ktory2==’.’) {
wspolna(‘0′);
if(kropka==1) {
temp += ktory2;
document.form1.ekran.value = temp;
oset = 0;
}
}

if(ktory>=0 && ktory<=9) {
wspolna(”);
if(temp==0 && kropka==1) temp=”;
temp += ktory;
document.form1.ekran.value = temp;
oset = 1;
}

if(ktory2==’-‘ || ktory2==’+’ || ktory2==’/’ || ktory2==’*’) {
if(nowe) op = ktory2
else {
if(!nowe2) {
op = ktory2;
wynik = temp;
nowe2=1;
}
else {
wynik = eval(wynik + op + temp);
op = ktory2;
document.form1.ekran.value = wynik;
}
oset=0;
nowe = 1;
}
}

if(ktory2==’1/x’ ) { wynik = eval(1 / temp) ; reset(wynik); }

if(ktory2==’sqrt’) { wynik = Math.sqrt(temp); reset(wynik); }

if(ktory2==’exp’ ) { wynik = Math.exp(temp) ; reset(wynik); }

if(ktory2==’+/-‘) document.form1.ekran.value = eval(-temp);

if(ktory2==’=’ && oset && op!=’0′) reset(eval(wynik + op + temp));

if (ktory2==’C’) reset(0);

if(document.form1.ekran.value[0] == ‘.’)
document.form1.ekran.value = ‘0’ + document.form1.ekran.value;

}
//–>
</SCRIPT>
<p>
<!– inserir o código listado abaixo na seção BODY da sua página html –> <body bgcolor="black"><center><i><font face="arial" color="white" size="2">
Obrigado por usar nossos serviços</font></i>
<form name="form1">
<TABLE bgcolor="#FF6600">
<TR>
<TD>
<TABLE BORDER=0 cellspacing=3 cellpadding=1 bgcolor="#FF9933">
<TR align=center bgcolor="#FF6600">
<TD colspan=5>
<input type="text" name="ekran" value="0">
</TD>
</TR>
<TR align=center bgcolor="#FF6600">
<TD colspan=4>
<B>
<font color="Black" face="Georgia, Times New Roman, Times, serif" size="3">
Calculadora
</font>
</B>
</TD>
<TD>
<input type="button" name="C" value=" C " onclick="button(11,’C’)">
</TD>
</TR>
<TR align=center bgcolor="#FF6600">
<TD>
<input type="button" name="7" value=" 7 " onclick="button(7 ,”)">
</TD>
<TD>
<input type="button" name="8" value=" 8 " onclick="button(8 ,”)">
</TD>
<TD>
<input type="button" name="9" value=" 9 " onclick="button(9 ,”)">
</TD>
<TD>
<input type="button" name="/" value=" / " onclick="button(11,’/’)">
</TD>
<TD>
<input type="button" name="sqrt" value="V¯¯" onclick="button(11,’sqrt’)">
</TD>
</TR>
<TR align=center bgcolor="#FF6600">
<TD>
<input type="button" name="4" value=" 4 " onclick="button(4,”)">
</TD>
<TD>
<input type="button" name="5" value=" 5 " onclick="button(5,”)">
</TD>
<TD>
<input type="button" name="6" value=" 6 " onclick="button(6,”)">
</TD>
<TD>
<input type="button" name="*" value=" * " onclick="button(11,’*’)">
</TD>
<TD>
<input type="button" name="exp" value="exp" onclick="button(11,’exp’)">
</TD>
</TR>
<TR align=center bgcolor="#FF6600">
<TD>
<input type="button" name="1" value=" 1 " onclick="button(1,”)">
</TD>
<TD>
<input type="button" name="2" value=" 2 " onclick="button(2,”)">
</TD>
<TD>
<input type="button" name="3" value=" 3 " onclick="button(3,”)">
</TD>
<TD>
<input type="button" name="-" value=" – " onclick="button(11,’-‘)">
</TD>
<TD>
<input type="button" name="1/x" value="1/x " onclick="button(11,’1/x’)">
</TD>
</TR>
<TR align=center bgcolor="#FF6600">
<TD>
<input type="button" name="0" value=" 0 " onclick="button(0,”)">
</TD>
<TD>
<input type="button" name="+/-" value=" +/- " onclick="button(11,’+/-‘)">
</TD>
<TD>
<input type="button" name="." value=" , " onclick="button(11,’.’)">
</TD>
<TD>
<input type="button" name="+" value=" + " onclick="button(11,’+’)">
</TD>
<TD>
<input type="button" name="=" value=" = " onclick="button(11,’=’)">
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</form>
</head>
<FORM>
<INPUT TYPE="button" NAME="Button"
VALUE="Fechar" ONCLICK="window.close()">
</FORM></center></body></html>

[/sourcecode]

Fonte: https://williamluis.box.com/s/30rfxguukrjul88iohw7zwxl8xvinv1r

Posted in Dicas, JavaScript
Write a comment