DOCTYPE html html script function value document getElementById inpStr

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
<!DOCTYPE html>
<html>
<script>
function r(value)
{
document.getElementById("inpStr").value += 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) {
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)
operand += s[i++];
--i;
if (isNaN(Number(operand)) == false)
st.push (Number(operand));
}
while (op.length > 0)
{
process_op (st, op.pop());
}
return st[0];
}
function solve()
{
var inputStroke = document.getElementById("inpStr").value;
document.getElementById("inpStr").value = calc(inputStroke);
};
</script>
<body>
<h2> Calculator </h2>
<INPUT id="inpStr" NAME= "inputValue">
<button id="sbm" onclick="solve()"> Solve </button>
<table>
<tr>
<td> <button id="b1" onclick="r('1')"> 1 </button> </td>
<td> <button id="b2" onclick="r('2')"> 2 </button> </td>
<td> <button id="b3" onclick="r('3')"> 3 </button> </td>
<td> <button id="bpls" onclick="r('+')"> + </button> </td>
</tr>
<tr>
<td> <button id="b4" onclick="r('4')"> 4 </button> </td>
<td> <button id="b5" onclick="r('5')"> 5 </button> </td>
<td> <button id="b6" onclick="r('6')"> 6 </button> </td>
<td> <button id="bmin" onclick="r('-')"> - </button> </td>
</tr>
<tr>
<td> <button id="b7" onclick="r('7')"> 7 </button> </td>
<td> <button id="b8" onclick="r('8')"> 8 </button> </td>
<td> <button id="b9" onclick="r('9')"> 9 </button> </td>
<td> <button id="bmul" onclick="r('*')"> * </button> </td>
</tr>
<tr>
<td> <button id="bz" onclick="r('0')"> 0 </button> </td>
<td> <button id="bdiv" onclick="r('/')"> / </button> </td>
</tr>
</table>
</body>
</html>