Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Web программирование > JavaScript, Ajax
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 08.12.2015, 17:08   #1
deconi
 
Регистрация: 08.12.2015
Сообщений: 4
Лампочка Предусмотреть соответствующее сообщение при делении на ноль.

Код HTML:
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>Калькулятор</title>
    <link rel="stylesheet" href="style.css" />
</head>
    <body>
        <div id="calculator">
            <form name="calculator">
                <table border="1">
                    <tr>
                        <td colspan="5"><input id="disp" type="text" class="area" /></td>
                    </tr>
                    <tr>
                        <td><input id="seven" type="button" name="button" value="7" /></td>
                        <td><input id="eight" type="button" name="button" value="8" /></td>
                        <td><input id="nine" type="button" name="button" value="9" /></td>
                        <td><input id="divide" type="button" name="button" value="/" /></td>
                        <td><input id="btnclear" type="button" name="button" value="C" /></td>
                    </tr>
                    <tr>
                        <td><input id="four" type="button" name="button" value="4" /></td>
                        <td><input id="five" type="button" name="button" value="5" /></td>
                        <td><input id="six" type="button" name="button" value="6" /></td>
                        <td><input id="multiply" type="button" name="button" value="*" /></td>
                        <td><input id="btnneg" type="button" name="button" value="&plusmn;" /></td>
                    </tr>
                    <tr>
                        <td><input id="one" type="button" name="button" value="1" /></td>
                        <td><input id="two" type="button" name="button" value="2" /></td>
                        <td><input id="three" type="button" name="button" value="3" /></td>
                        <td><input id="substract" type="button" name="button" value="-" /></td>
                        <td rowspan="2"><input id="btnequals" class="anyway" type="button" name="button" value="=" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input id="zero" class="zero" type="button" name="button" value="0" /></td>
                        <td><input id="decimal" type="button" name="button" value="." /></td>
                        <td><input id="add" type="button" name="button" value="+" /></td>
                    </tr>       
                </table>
            </form> 
        </div>  
        
    <script src="JS.js"></script>
    </body>
    </html>
Код:
document.getElementById("one").onclick = function(){btnVal(1);};
document.getElementById("two").onclick = function(){btnVal(2);};
document.getElementById("three").onclick = function(){btnVal(3);};
document.getElementById("four").onclick = function(){btnVal(4);};
document.getElementById("five").onclick = function(){btnVal(5);};
document.getElementById("six").onclick = function(){btnVal(6);};
document.getElementById("seven").onclick = function(){btnVal(7);};
document.getElementById("eight").onclick = function(){btnVal(8);};
document.getElementById("nine").onclick = function(){btnVal(9);};
document.getElementById("zero").onclick = function(){btnVal(0);};
document.getElementById("btnequals").onclick = function(){equals("=");};
document.getElementById("btnclear").onclick = function(){clear();};
document.getElementById("decimal").onclick = function(){decimal();};
document.getElementById("btnneg").onclick = function(){plusMinus();};
document.getElementById("add").onclick = function(){operator("+");};
document.getElementById("multiply").onclick = function(){operator("*");};
document.getElementById("divide").onclick = function(){operator("/");};
document.getElementById("substract").onclick = function(){operator("-");};
 
 
 
    var calc = document.getElementById('disp');
    var oper = "";
    var num = "";
 
function btnVal(n){
  calc.value = calc.value + n;
}
 
function operator(op){
    oper = op;
    num = calc.value;
    calc.value = "";
}
 
 
function equals(){
    rez(eval(num), eval(calc.value), oper);
}
 
 
function rez(n1, n2, op){
    if(op == "+"){
        calc.value = n1 + n2;
    }
    else if(op == "-"){
        calc.value = n1 - n2;
    }
    else if(op == "*"){
        calc.value = n1 * n2;
    }
    else if(op == "/"){
        calc.value = n1 / n2;
    }
}
 
function plusMinus(){
    calc.value = calc.value * -1;
}
 
function clear(){
    calc.value = "";
    num = "";
}
     
function decimal() {
        calc.value = calc.value + "."
}
deconi вне форума Ответить с цитированием
Старый 08.12.2015, 17:10   #2
deconi
 
Регистрация: 08.12.2015
Сообщений: 4
По умолчанию

Что то типа - "Делить на 0 не надо". Просто что бы выводилось на экран при делении на 0. И если возможно, что бы точку ставить можно было только один раз
deconi вне форума Ответить с цитированием
Старый 09.12.2015, 09:30   #3
Kazik
Форумчанин
 
Регистрация: 27.05.2009
Сообщений: 192
По умолчанию

Код:
function divByZero(op){
  if (op == "/" && n2 == 0){
    calc.value = "Деление на 0 невозможно."
  }
}
не факт что будет работать в таком виде, но думаю смысл вы уловили

Последний раз редактировалось Kazik; 09.12.2015 в 09:52.
Kazik вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
подскажите пожалуйста как остановить цикл сообщением об ошибке при делении на ноль и вернуться на начало программы Nata Golden rose Общие вопросы C/C++ 1 22.05.2014 08:02
При делении на 0 пишет -1.#INF Tatyana64 Помощь студентам 1 04.11.2012 12:25
Ошибка при делении на 0 roxy7 Общие вопросы Delphi 3 25.05.2012 10:54
ошибка при делении на ноль.. vityanya Общие вопросы Delphi 2 13.01.2011 05:29
Переполнение при делении JeyKip Assembler - Ассемблер (FASM, MASM, WASM, NASM, GoASM, Gas, RosAsm, HLA) и не рекомендуем TASM 1 20.03.2010 14:04