html head head body table cellspacing tr td colspan input id inputExpr

  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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<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"> &larr; </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"> &radic; </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"> &plusmn; </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>