<html>
<head>
</head>
<body>
<table cellspacing="1">
<tr>
<td colspan="5"> <input id="inputExpression" style="width:232px;height:40px"/> </td>
</tr>
<tr>
<td colspan="2"> <button id="btnClear" onclick="bufferClear()" style="width:89px;height:40px"> ← </button> </td>
<td> <button id="btnReset" onclick="bufferReset()" style="width:40px;height:40px"> C </button> </td>
<td> <button id="root" onclick="sqrt()" style="width:40px;height:40px"> √ </button> </td>
<td> <button id="111" onclick="pow()" style="width:40px;height:40px"> 1/x </button> </td>
</tr>
<tr>
<td> <button id="1" onclick="bufferAdd('1')" style="width:40px;height:40px"> 1 </button> </td>
<td> <button id="2" onclick="bufferAdd('2')" style="width:40px;height:40px"> 2 </button> </td>
<td> <button id="3" onclick="bufferAdd('3')" style="width:40px;height:40px"> 3 </button> </td>
<td> <button id="div" onclick="bufferAdd('/')" style="width:40px;height:40px"> / </button> </td>
<td> <button id="plusmin" onclick="plusMin()" style="width:40px;height:40px"> ± </button> </td>
</tr>
<tr>
<td> <button id="4" onclick="bufferAdd('4')" style="width:40px;height:40px"> 4 </button> </td>
<td> <button id="5" onclick="bufferAdd('5')" style="width:40px;height:40px"> 5 </button> </td>
<td> <button id="6" onclick="bufferAdd('6')" style="width:40px;height:40px"> 6 </button> </td>
<td> <button id="mult" onclick="bufferAdd('*')" style="width:40px;height:40px"> * </button> </td>
<td rowspan="3"> <button id="calculation" style="width:40px;height:130px" onclick="solve()"> = </button> </td>
</tr>
<tr>
<td> <button id="7" onclick="bufferAdd('7')" style="width:40px;height:40px"> 7 </button> </td>
<td> <button id="8" onclick="bufferAdd('8')" style="width:40px;height:40px"> 8 </button> </td>
<td> <button id="9" onclick="bufferAdd('9')" style="width:40px;height:40px"> 9 </button> </td>
<td> <button id="min" onclick="bufferAdd('-')" style="width:40px;height:40px"> - </button> </td>
</tr>
<tr>
<td colspan="2"> <button id="0" onclick="bufferAdd('0')" style="width:86px;height:40px"> 0 </button> </td>
<td> <button id="10" onclick="bufferAdd('.')" style="width:40px;height:40px"> , </button> </td>
<td> <button id="plus" onclick="bufferAdd('+')" style="width:40px;height:40px"> + </button> </td>
</tr>
</table>
<script>
function pow()
{
var str = document.getElementById("inputExpression").value;
var check = 0;
for (var i = 0; i < str.length; ++i)
{
if (is_op(str[i]) != 0)
check = 1;
}
if (check == 1)
{
alert("Входная строка содержит операции");
}
else
{
var val = Number(document.getElementById("inputExpression").value);
var resultVal = 1 / val;
document.getElementById("inputExpression").value = resultVal;
}
}
function plusMin()
{
var str = document.getElementById("inputExpression").value;
var check = 0;
for (var i = 0; i < str.length; ++i)
{
if (is_op(str[i]) != 0)
check = 1;
}
if (check == 1)
{
alert("Входная строка содержит операции");
}
else
{
var val = document.getElementById("inputExpression").value;
if (val[0] == '-')
{
var resultVal = "";
for (var i = 1; i < val.length; ++i)
{
resultVal += val[i];
}
document.getElementById("inputExpression").value = resultVal;
}
else
{
var resultVal = "-";
for (var i = 0; i < val.length; ++i)
{
resultVal += val[i];
}
document.getElementById("inputExpression").value = resultVal;
}
}
}
function sqrt()
{
var str = document.getElementById("inputExpression").value;
var check = 0;
for (var i = 0; i < str.length; ++i)
{
if (is_op(str[i]) != 0)
{
check = 1;
}
}
if (check == 1)
{
alert("Входная строка содержит операции");
}
else
{
var val = Number(document.getElementById("inputExpression").value);
document.getElementById("inputExpression").value = Math.sqrt(val);
}
}
function bufferAdd(value)
{
document.getElementById("inputExpression").value += value;
}
function bufferClear()
{
var str = document.getElementById("inputExpression").value;
var resultStr = "";
for (var i = 0; i < str.length - 1; ++i)
{
resultStr += str[i];
}
document.getElementById("inputExpression").value = resultStr;
}
function bufferReset()
{
document.getElementById("inputExpression").value = "";
}
function delim (c)
{
return c == ' ';
}
function is_op (c)
{
return c == '+' || c == '-' || c == '*' || c == '/' || c == '%';
}
function priority (op)
{
if(op == '+' || op == '-')
return 1;
if(op == '*' || op == '/' || op == '%' )
return 2;
else
return -1;
}
function process_op (st,op)
{
var r = st.pop();
var l = st.pop();
switch (op)
{
case '+': st.push(l + r); break;
case '-': st.push(l - r); break;
case '*': st.push(l * r); break;
case '/': st.push(l / r); break;
case '%': st.push(l % r); break;
}
}
function calc (s)
{
var st = new Array();
var op = new Array();
for (var i = 0; i < s.length; ++i)
{
if (is_op (s[i]) != 0)
{
if(s[i] == '-' && i == 0)
{
st.push (0);
op.push ('-');
}
else
{
var curop = s[i];
while (op.length > 0 && priority(op[op.length - 1]) >= priority(s[i]))
process_op (st, op.pop());
op.push(curop);
}
}
else
{
var operand = "";
while (i < s.length && (isNaN(Number(s[i])) == false || s[i] == '.'))
operand += s[i++];
--i;
st.push (Number(operand));
}
}
while (op.length > 0)
process_op (st, op.pop());
return st[0];
}
function solve ()
{
var inputExpression = document.getElementById("inputExpression").value,
outputExpression = calc(inputExpression);
document.getElementById("inputExpression").value = outputExpression;
}
</script>
</body>
</html>