<script>
function getOpPrec(op) {
switch(op) {
case '*': case '/':
return 5;
case '+': case '-':
return 3;
case '(':
return 1;
}
}
function whoPrecOp(op1, op2) {
var op1Prec = getOpPrec(op1), op2Prec = getOpPrec(op2);
if (op1Prec > op2Prec)
return 1;
else if (op1Prec < op2Prec)
return -1;
else
return 0;
}
function postfix(exp) {
var dst = [], src = [], op = [],
src = exp.replace(' ','').match(/[\d\.]+|[+\-*()/]/g);
while (src.length) {
var tok = src.shift();
if (!isNaN(tok)) {
dst.push(tok);
} else {
switch(tok) {
case '(':
op.push(tok);
break;
case ')':
while(1) {
var popOp = op.pop();
if (popOp == '(')
break;
dst.push(popOp);
}
break;
case '+': case '-':
case '*': case '/':
while (op.length && whoPrecOp(op[op.length-1], tok) > 0)
dst.push(op.pop());
op.push(tok);
break;
}
}
}
while (op.length)
dst.push(op.pop());
return dst;
}
function evalExp(exp) {
var dst = [], op1, op2;
while (exp.length) {
var tok = exp.shift();
if (!isNaN(tok))
dst.push(tok);
else {
op2 = Number(dst.pop());
op1 = Number(dst.pop());
switch (tok) {
case '+':
dst.push(op1 + op2);
break;
case '-':
dst.push(op1 - op2);
break;
case '*':
dst.push(op1 * op2);
break;
case '/':
dst.push(op1 / op2);
break;
}
}
}
return dst;
}
function cal(src) {
document.getElementById('result').value = evalExp(postfix(src));
}
</script>
입력 <input type="text" id="input" name="src" value="" onblur="cal(this.value)" /> =
결과 <input id="result" type="text" name="result" size="10" value="" />
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 330 |
prosper
|
20년 전 | 2283 | |
| 329 |
prosper
|
20년 전 | 1910 | |
| 328 |
prosper
|
20년 전 | 1706 | |
| 327 | 20년 전 | 3772 | ||
| 326 | 20년 전 | 4860 | ||
| 325 |
hwatta
|
20년 전 | 2484 | |
| 324 |
|
20년 전 | 3146 | |
| 323 | 20년 전 | 5866 | ||
| 322 |
hwatta
|
20년 전 | 2852 | |
| 321 |
hwatta
|
20년 전 | 2324 | |
| 320 |
yesmoa
|
20년 전 | 4575 | |
| 319 | 20년 전 | 2691 | ||
| 318 | 20년 전 | 2243 | ||
| 317 |
kyodon
|
20년 전 | 2761 | |
| 316 | 20년 전 | 2586 | ||
| 315 |
|
20년 전 | 2885 | |
| 314 |
|
20년 전 | 3345 | |
| 313 |
|
20년 전 | 2649 | |
| 312 |
yesmoa
|
20년 전 | 4724 | |
| 311 | 20년 전 | 3277 | ||
| 310 |
홀로남은자
|
20년 전 | 4570 | |
| 309 | 20년 전 | 3009 | ||
| 308 | 20년 전 | 4139 | ||
| 307 | 20년 전 | 4375 | ||
| 306 | 20년 전 | 7039 | ||
| 305 | 20년 전 | 3872 | ||
| 304 | 20년 전 | 2773 | ||
| 303 |
크리스탈처럼
|
20년 전 | 4409 | |
| 302 | 20년 전 | 2210 | ||
| 301 |
|
20년 전 | 4327 | |
| 300 | 20년 전 | 3811 | ||
| 299 | 20년 전 | 2621 | ||
| 298 | 20년 전 | 4835 | ||
| 297 |
|
20년 전 | 2531 | |
| 296 | 20년 전 | 4533 | ||
| 295 | 20년 전 | 3572 | ||
| 294 | 20년 전 | 3594 | ||
| 293 | 20년 전 | 3820 | ||
| 292 | 20년 전 | 3215 | ||
| 291 |
yesmoa
|
20년 전 | 5944 | |
| 290 | 20년 전 | 2942 | ||
| 289 | 20년 전 | 5865 | ||
| 288 |
|
20년 전 | 2381 | |
| 287 |
|
20년 전 | 1812 | |
| 286 |
|
20년 전 | 2151 | |
| 285 |
|
20년 전 | 3544 | |
| 284 |
|
20년 전 | 2039 | |
| 283 |
|
20년 전 | 4404 | |
| 282 | 20년 전 | 3382 | ||
| 281 |
|
20년 전 | 2216 | |
| 280 |
|
20년 전 | 7811 | |
| 279 | 20년 전 | 5560 | ||
| 278 | 20년 전 | 2999 | ||
| 277 |
|
20년 전 | 5572 | |
| 276 | 20년 전 | 2363 | ||
| 275 | 20년 전 | 2611 | ||
| 274 | 20년 전 | 2397 | ||
| 273 | 20년 전 | 2227 | ||
| 272 | 20년 전 | 2156 | ||
| 271 | 20년 전 | 2614 | ||
| 270 | 20년 전 | 2645 | ||
| 269 | 20년 전 | 2489 | ||
| 268 | 20년 전 | 2694 | ||
| 267 | 20년 전 | 2379 | ||
| 266 | 20년 전 | 2570 | ||
| 265 | 20년 전 | 3515 | ||
| 264 |
|
20년 전 | 5374 | |
| 263 |
|
20년 전 | 3735 | |
| 262 | 20년 전 | 3195 | ||
| 261 |
허저비
|
20년 전 | 5939 | |
| 260 |
|
20년 전 | 5721 | |
| 259 | 20년 전 | 4145 | ||
| 258 | 20년 전 | 2380 | ||
| 257 | 20년 전 | 3199 | ||
| 256 | 20년 전 | 1913 | ||
| 255 | 20년 전 | 1581 | ||
| 254 | 20년 전 | 3156 | ||
| 253 | 20년 전 | 3550 | ||
| 252 | 20년 전 | 5130 | ||
| 251 | 20년 전 | 5823 | ||
| 250 | 20년 전 | 3681 | ||
| 249 | 20년 전 | 5027 | ||
| 248 | 20년 전 | 3295 | ||
| 247 | 20년 전 | 3650 | ||
| 246 |
|
20년 전 | 7963 | |
| 245 |
|
20년 전 | 5920 | |
| 244 | 20년 전 | 4497 | ||
| 243 |
|
20년 전 | 4070 | |
| 242 | 20년 전 | 2797 | ||
| 241 | 20년 전 | 2744 | ||
| 240 | 20년 전 | 2381 | ||
| 239 | 20년 전 | 1673 | ||
| 238 |
아우겐나이스
|
20년 전 | 2283 | |
| 237 |
email
|
20년 전 | 3695 | |
| 236 | 20년 전 | 4171 | ||
| 235 | 20년 전 | 10473 | ||
| 234 | 20년 전 | 5079 | ||
| 233 | 20년 전 | 3381 | ||
| 232 | 20년 전 | 3222 | ||
| 231 | 20년 전 | 3859 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기