<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Polish notation</title>
</head>
<body>
<form class="" action="index.html" method="post" onsubmit="return false;">
<div class="">
Все операнды и операторы, в т.ч. и скобки, необходимо разделять пробелами, чтобы не сломался парсер.
<br>
Доступные операторы: *, \, -, +, ^
</div>
<input type="text" name="exampleString" value="">
<input type="button" name="submit" value="Отправить">
</form>
<div class="">
Польска нотация: <span id="answerPn"></span>
<br>
Ответ: <span id="answer"></span>
</div>
</body>
<script type="text/javascript">
var arrString = [];
document.forms[0].submit.onclick = function () {
arrString = document.forms[0].exampleString.value.split(" ");
// document.forms[0].exampleString.value = ("");
for (var i = 0; i < arrString.length; i++) {
if (arrString[i] == "") {
arrString.splice(i, 1);
}
}
answerPn.innerHTML = transformation(arrString);
answer.innerHTML = decisionPoleNot(transformation(arrString));
}
var arrExample = [8, 2, 5, "*", "+", 1, 3, 2, "*", "+", 4, "-", "/"],
exampleNormal = [3, "+", 4, "*", 2, "/", "(", 1, "-", 5, ")", "^", 2],
arrExampleNormal = [3, 4, 2, "*", 1, 5, "-", 2, "^", "/", "+"];
function decisionPoleNot(example) {
var operands = [];
for (var i = 0; i < example.length; i++) {
console.group(i);
if (!isNaN(parseFloat(example[i]))) {
operands.push(parseFloat(example.slice(i, i+1)[0]));
console.log("operands.push(example.splice(i, 1)[0]): " + operands);
}
console.log("switch start: " + example[i]);
switch (example[i]) {
case "*":
operands.push(operands.splice(-2, 1)[0] * operands.pop());
break;
case "+":
operands.push(operands.splice(-2, 1)[0] + operands.pop());
break;
case "-":
operands.push(operands.splice(-2, 1)[0] - operands.pop());
break;
case "/":
if (operands.slice(-1) == 0) {
return operans = ["Делить на ноль нельзя!"];
}
operands.push(operands.splice(-2, 1)[0] / operands.pop());
break;
case "^":
operands.push(Math.pow(operands.splice(-2, 1)[0], operands.pop()));
break;
}
console.log("example.splice(i, 1): " + example);
console.log("operands in switch: " + operands);
console.log("operands function end: " + operands);
console.groupEnd();
}
if (operands.length == 1) {
return operands[0];
}
}
function transformation(example) {
var outputLine = [],
operators = [];
for (var i = 0; i < example.length; i++) {
console.log("for start");
if (!isNaN(parseFloat(example[i]))) {
outputLine.push(parseFloat(example.slice(i, i+1)[0]));
}
console.log("switch start")
switch (example[i]) {
case ")":
while (operators.slice(-1)[0] != "(") {
outputLine.push(operators.pop());
console.log("while start");
}
operators.pop();
break;
case "/":
if (operators.slice(-1)[0] == "*" || operators.slice(-1)[0] == "/") {
outputLine.push(operators.pop());
operators.push(example.slice(i, i+1)[0]);
} else {
operators.push(example.slice(i, i+1)[0]);
}
break;
case "*":
if (operators.slice(-1)[0] == "*" || operators.slice(-1)[0] == "/") {
outputLine.push(operators.pop());
operators.push(example.slice(i, i+1)[0]);
} else {
operators.push(example.slice(i, i+1)[0]);
}
break;
case "+":
if (operators.slice(-1)[0] == "*" || operators.slice(-1)[0] == "/" ||
operators.slice(-1)[0] == "+" || operators.slice(-1)[0] == "-") {
outputLine.push(operators.pop());
operators.push(example.slice(i, i+1)[0]);
} else {
operators.push(example.slice(i, i+1)[0]);
}
break;
case "-":
if (operators.slice(-1)[0] == "*" || operators.slice(-1)[0] == "/" ||
operators.slice(-1)[0] == "+" || operators.slice(-1)[0] == "-") {
outputLine.push(operators.pop());
operators.push(example.slice(i, i+1)[0]);
} else {
operators.push(example.slice(i, i+1)[0]);
}
break;
case "^":
operators.push(example.slice(i, i+1)[0]);
break;
case "(":
operators.push(example.slice(i, i+1)[0]);
break;
default:
}
}
while (operators.length > 0) {
outputLine.push(operators.pop());
}
if (example.length == i) {
console.log(outputLine);
console.log(operators);
return outputLine;
}
}
/*
transformation(exampleNormal);
decisionPoleNot(transformation(exampleNormal));
decisionPoleNot(arrExample);
decisionPoleNot(arrExampleNormal);
*/
</script>
</html>