Показать сообщение отдельно
  #21 (permalink)  
Старый 03.02.2016, 01:42
Интересующийся
Отправить личное сообщение для hbsv Посмотреть профиль Найти все сообщения от hbsv
 
Регистрация: 06.01.2016
Сообщений: 27

Пока выходит так:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Calculator</title>
</head>
<link rel="stylesheet" type="text/css" href="main.css">
<body>


<from id="calculator">
    <table>
        <tr>
            <td>
                <div align="center">

                    <input id="screen" type="text" value="" size="16" class="display" readonly="readonly" />
                </div>
            </td>
        </tr>

<tr>
    <td class="buttons">
        <div align="center">

<input id="btn_1" type="button" value="1" onclick="addInput(1)"/>
<input id="btn_2" type="button" value="2" onclick="addInput(2)"/>
<input id="btn_3" type="button" value="3" onclick="addInput(3)" />
<input id="add" type="button" value="+" class="operator" onclick="add()" />
        </div>
        <div align="center">
<input id="btn_4" type="button" value="4" onclick="addInput(4)" />
<input id="btn_5" type="button" value="5" onclick="addInput(5)" />
<input id="btn_6" type="button" value="6" onclick="addInput(6)" />
<input id="sub" type="button" value=" - " onclick="sub()" class="operator" />
        </div>
<input id="btn_7" type="button" value="7" onclick="addInput(7)" />
<input id="btn_8" type="button" value="8" onclick="addInput(8)" />
<input id="btn_9" type="button" value="9" onclick="addInput(9)" />
<input id="mult" type="button" value=" * " onclick="mult()" class="operator" />
        <div align="center">
<input id="clr" type="button" value=" c " onclick="clr()" class="clear" />
<input id="btn_0" type="button" value="0" onclick="addInput(0)" />
<input id="calculate" type="button" value="  =  " onclick="calculate()" class="eval" />
<input id="div" type="button" value=" / " onclick="div()" class="operator" />


        </div>
            </td>
        </tr>
    </table>
</from>


</body>

<script type="text/javascript" language="javascript" src="main.js"></script>

</html>


и js

"use strict";

var a,b,c;
var action = "";



function getValue(value) {
    var res = document.getElementById(value).value - '';
    if (!isNaN(res))
        return res;
}

function addInput(val) {
    document.getElementById("screen").value = (document.getElementById("screen").value + val)-"";
}


function add () {
    a = getValue("screen");
    document.getElementById("screen").value = "";
    action = "+";
}

function sub() {
    a = getValue("screen");
    document.getElementById("screen").value = "";
    action = "-";
}

function mult() {
    a = getValue("screen");
    document.getElementById("screen").value = "";
    action = "*";
}

function div() {
    a = getValue("screen");
    document.getElementById("screen").value = "";
    action = "/";
}

function clr() {
    document.getElementById("screen").value = "";
}


function calculate() {
    b = getValue("screen");
    switch(action) {
        case "+":
            c = a + b;
            break;
        case "-":
            c = a - b;
            break;
        case "*":
            c = a * b;
            break;
        case "/":
            if (b == 0) {
                alert('Division durch Null kann nicht sein!');
            }
            else {
                c = a / b;
            }
            break;
    }
    document.getElementById("screen").value = c;
}
Ответить с цитированием