|
Калькулятор.Логика:1) по нажатию на цифрки - формируется первое число;2) при нажатии на операцию - запоминается первое число, которое уже было введено, запоминается производимая опереция и очищается строка для ввода второго числ;3) по нажатию на равно - получаем второе число, производим операцию, согласно запомненой, выводим результат;остальные функции допольнительные.<html> <head> <title>An onLoad script</title> <script language="JavaScript"> <!-- var ch1; var ch2; var m1, m2, mr=0; var op;
function vvod(x){ document.form1.entry.value=eval(document.form1.entry.value)*10+eval(x); } function cls(){ document.form1.entry.value="0"; } function oper(n){ op=n; ch1=eval(document.form1.entry.value); document.form1.entry.value="0"; } function ravno(){ ch2=eval(document.form1.entry.value); if (op=='+'){document.form1.entry.value=ch1+ch2;} else if (op=='-'){document.form1.entry.value=ch1-ch2;} else if (op=='*'){document.form1.entry.value=ch1*ch2;} else if (op=='/'){if (ch2==0){document.form1.entry.value="ERROR";} else{document.form1.entry.value=ch1/ch2;} } } function MR() { document.form1.entry.value=mr; } function M1() { mr=mr+eval(document.form1.entry.value); document.form1.lbl.value=mr; } function M2() { mr=mr-eval(document.form1.entry.value); document.form1.lbl.value=mr; } function MC() { mr=0; document.form1.lbl.value=''; } //-> </Script> </head> <body onLoad='document.form1.lbl.value="";'> <center> <Form name="form1"> <Input Type="text" Name="entry" value="0" style="height:30px; width:176px; background-color: #0ff; text-align:right; font-size:15pt;"> <Input Type="text" Name="lbl" value="0" style="height:30px; width:40px; background-color: #0ff; text-align:right; font-size:15pt;"> <br> <Input Type="button" Name="bt7" Value="7" onClick="vvod(7)" style="height:40px; width:40px;"> <Input Type="button" Name="bt8" Value="8" onClick="vvod(8)" style="height:40px; width:40px;"> <Input Type="button" Name="bt9" Value="9" onClick="vvod(9)" style="height:40px; width:40px;"> <Input Type="button" Name="plus" Value="+" onClick="oper('+')" style="height:40px; width:40px;"> <Input Type="button" Name="mr" Value="MR" onClick="MR()" style="height:40px; width:40px;"> <br> <Input Type="button" Name="bt4" Value="4" onClick="vvod(4)" style="height:40px; width:40px;"> <Input Type="button" Name="bt5" Value="5" onClick="vvod(5)" style="height:40px; width:40px;"> <Input Type="button" Name="bt6" Value="6" onClick="vvod(6)" style="height:40px; width:40px;"> <Input Type="button" Name="minus" Value="-" onClick="oper('-')" style="height:40px; width:40px;"> <Input Type="button" Name="m1" Value="M+" onClick="M1()" style="height:40px; width:40px;"> <br> <Input Type="button" Name="bt1" Value="1" onClick="vvod(1)" style="height:40px; width:40px;"> <Input Type="button" Name="bt2" Value="2" onClick="vvod(2)" style="height:40px; width:40px;"> <Input Type="button" Name="bt3" Value="3" onClick="vvod(3)" style="height:40px; width:40px;"> <Input Type="button" Name="um" Value="*" onClick="oper('*')" style="height:40px; width:40px;"> <Input Type="button" Name="m2" Value="M-" onClick="M2()" style="height:40px; width:40px;"> <br> <Input Type="button" Name="bt0" Value="0" onClick="vvod(0)" style="height:40px; width:40px;"> <Input Type="button" Name="cl" Value="C" onClick="cls()" style="height:40px; width:40px;"> <Input Type="button" Name="del" Value="/" onClick="oper('/')" style="height:40px; width:40px;"> <Input Type="button" Name="rav" Value="=" onClick="ravno()" style="height:40px; width:40px;"> <Input Type="button" Name="mc" Value="MC" onClick="MC()" style="height:40px; width:40px;"> <br> </form> </center> </body> </html>
|
© gruppa-43im2009 |