Показать сообщение отдельно
  #14 (permalink)  
Старый 01.02.2016, 13:09
Профессор
Отправить личное сообщение для Dilettante_Pro Посмотреть профиль Найти все сообщения от Dilettante_Pro
 
Регистрация: 27.11.2015
Сообщений: 2,899

Вот вроде сделал

<!DOCTYPE html>
<html>
<head>
  <title>Calc</title>
  <script type="text/javascript">
     var calcString = "";

     function control(testString) { 
         testString = testString.replace(/\++/g, "+");
         testString = testString.replace(/\--/g, "+");
         testString = testString.replace(/\.\./g, ".");
         testString = testString.replace(/[+-][-+]/g, "-");
         return testString;
     }
     function calculation(buffString) {
         buffString = buffString.replace(/([^[0-9.]{1})/g, " $1 ").trim(); // добавим пробелы вокруг не чисел
         buffString = buffString.replace(/ {1,}/g, " ");              // удаление сдвоенных пробелов
         var buffArray = buffString.split(/\s/);                   // Элементы - в массив
         var polishString = new Array;
         var polishStack = new Array;
         var stringId = -1;
         var stackId = -1;
         for (var i = 0; i < buffArray.length; i++) {                // формируем обратную польскую запись
             switch (buffArray[i]) {
                case "(":
                    stackId++;
                    polishStack[stackId] = buffArray[i];
                    break;
                case ")":
                    while (stackId >= 0 && polishStack[stackId] != "(") {
                        stringId++;
                        polishString[stringId] = polishStack[stackId];
                        stackId--;
                    }
                    stackId--;
                    break;
              case "+":
                  while (stackId >= 0 && (polishStack[stackId] == "+" || polishStack[stackId] == "-" || polishStack[stackId] == "*" || polishStack[stackId] == "/")) {
                      stringId++;
                      polishString[stringId] = polishStack[stackId];
                      stackId--;
                  }
                  stackId++;
                  polishStack[stackId] = buffArray[i];
                  break;
              case "-":
                  while (stackId >= 0 && (polishStack[stackId] == "+" || polishStack[stackId] == "-" || polishStack[stackId] == "*" || polishStack[stackId] == "/")) {
                      stringId++;
                      polishString[stringId] = polishStack[stackId];
                      stackId--;
                  }
                  stackId++;
                  polishStack[stackId] = buffArray[i];
                  break;
              case "*":
                  while (stackId >= 0 && (polishStack[stackId] == "*" || polishStack[stackId] == "/")) {
                      stringId++;
                      polishString[stringId] = polishStack[stackId];
                      stackId--;
                  }
                  stackId++;
                  polishStack[stackId] = buffArray[i];
                 break;
             case "/":
                 while (stackId >= 0 && (polishStack[stackId] == "*" || polishStack[stackId] == "/")) {
                     stringId++;
                     polishString[stringId] = polishStack[stackId];
                     stackId--;
                 }
                 stackId++;
                 polishStack[stackId] = buffArray[i];
                 break;
             default:
                 stringId++;
                 polishString[stringId] = buffArray[i];
           }
        }
        while (stackId >= 0) {
            stringId++;
            polishString[stringId] = polishStack[stackId];
            stackId--;
        }                                                                  // польская запись готова
     //   alert(polishString);
        stackId = -1;                                                      // Начинаем считать по польской записи
        var stringIdMax = stringId;

        for (stringId = 0; stringId <= stringIdMax; stringId++ ) {
            switch (polishString[stringId]) {
                case "+":
                    stackId--;
                    polishStack[stackId] = polishStack[stackId] + polishStack[stackId + 1];
                    break;
                case "-":
                    stackId--;
                    polishStack[stackId] = polishStack[stackId] - polishStack[stackId + 1];
                    break;
                case "*":
                    stackId--;
                    polishStack[stackId] = polishStack[stackId] * polishStack[stackId + 1];
                    break;
                case "/":
                    stackId--;
                    polishStack[stackId] = polishStack[stackId] / polishStack[stackId + 1];
                    break;
                default:
                    stackId++;
                    polishStack[stackId] = parseFloat(polishString[stringId]);
            }
        }
        return polishStack[stackId];
     }

     function kalk() {
          try {
              var res = calculation(calcString);
          } catch (e) { res = "Ошибка в выражении"; }
         document.getElementById("res").innerHTML = calcString + "=" + res;
     }
     function addToInput(value) {
          calcString += value;
          calcString = control(calcString);
          document.getElementById("res").innerHTML = calcString;
     }
     function clean() {
          calcString = "";
          document.getElementById("res").innerHTML = calcString;
     }
      function del() {
          calcString = calcString.substr(0,calcString.length - 1);
          document.getElementById("res").innerHTML = calcString;
     }
   </script>
</head>
<body>
    <table>
        <tr>
            <td>
                <div id="res" style="width:400px;min-height:20px;border:solid 1px black;">
                </div>
            </td>
        </tr>
        <tr>
            <td class="buttons">
             <div id="num1" align="center">
                <input type="button" onclick="addToInput(this.value)" name="rakam11" value="1" />
                <input onclick="addToInput(this.value)" type="button" name="rakam22" value="2" />
                <input onclick="addToInput(this.value)" type="button" name="rakam33" value="3" />
                <input onclick="addToInput(this.value)" type="button" name="rakam44" value="4" />
                <input onclick="addToInput(this.value)" type="button" name="rakam55" value="5" />
                <input onclick="addToInput(this.value)" type="button" name="rakam66" value="6" />
                <input onclick="addToInput(this.value)" type="button" name="rakam77" value="7" />
                <input onclick="addToInput(this.value)" type="button" name="rakam88" value="8" />
                <input onclick="addToInput(this.value)" type="button" name="rakam99" value="9" />
                <input onclick="addToInput(this.value)" type="button" name="rakam00" value="0" />
                <input onclick="addToInput(this.value)" type="button" name="rakam00" value="." />
            </div>
             <div id="act" align="center">
                <input onclick="addToInput(this.value)" type="button" name="plus" value="+" />
                <input onclick="addToInput(this.value)" type="button" name="minus" value="-" />
                <input onclick="addToInput(this.value)" type="button" name="umn" value="*" />
                <input onclick="addToInput(this.value)" type="button" name="divide" value="/" />
                <input onclick="addToInput(this.value)" type="button" name="lpar" value="(" />
                <input onclick="addToInput(this.value)" type="button" name="rpar" value=")" />
             </div>
            </td>
        <tr>
            <td ><input type="button" name="button" id="button" onclick="kalk()" value="Считать" />
                <input type="button" name="button1"  onclick="clean()" value="CLEAN" />
                <input type="button" name="button1"  onclick="del()" value="DEL" /></td>
        </tr>
    </table>
</body>
</html>

Осталась масса простору для доработок
Ответить с цитированием