<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="" />
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 630 | 19년 전 | 2360 | ||
| 629 |
|
19년 전 | 2017 | |
| 628 |
|
19년 전 | 3136 | |
| 627 |
|
19년 전 | 2035 | |
| 626 |
|
19년 전 | 2069 | |
| 625 |
|
19년 전 | 1726 | |
| 624 |
|
19년 전 | 2238 | |
| 623 | 19년 전 | 2208 | ||
| 622 |
|
19년 전 | 2514 | |
| 621 |
|
19년 전 | 2924 | |
| 620 |
|
19년 전 | 2418 | |
| 619 | 19년 전 | 2483 | ||
| 618 | 19년 전 | 3518 | ||
| 617 |
|
19년 전 | 3238 | |
| 616 |
|
19년 전 | 3229 | |
| 615 |
|
19년 전 | 3051 | |
| 614 |
|
19년 전 | 2050 | |
| 613 |
|
19년 전 | 2387 | |
| 612 |
|
19년 전 | 2244 | |
| 611 |
|
19년 전 | 2092 | |
| 610 |
|
19년 전 | 2524 | |
| 609 |
|
19년 전 | 2434 | |
| 608 |
|
19년 전 | 2823 | |
| 607 | 19년 전 | 2197 | ||
| 606 | 19년 전 | 3542 | ||
| 605 | 19년 전 | 1997 | ||
| 604 | 19년 전 | 2615 | ||
| 603 | 19년 전 | 1622 | ||
| 602 |
|
19년 전 | 2844 | |
| 601 | 19년 전 | 3076 | ||
| 600 |
|
19년 전 | 2157 | |
| 599 | 19년 전 | 2006 | ||
| 598 | 19년 전 | 2490 | ||
| 597 | 19년 전 | 2395 | ||
| 596 |
|
19년 전 | 3082 | |
| 595 |
|
19년 전 | 6765 | |
| 594 | 19년 전 | 2716 | ||
| 593 | 19년 전 | 3192 | ||
| 592 |
|
19년 전 | 3096 | |
| 591 |
|
19년 전 | 1943 | |
| 590 | 19년 전 | 3372 | ||
| 589 | 19년 전 | 2215 | ||
| 588 |
|
19년 전 | 2708 | |
| 587 | 19년 전 | 2349 | ||
| 586 |
|
19년 전 | 4371 | |
| 585 | 19년 전 | 2498 | ||
| 584 | 19년 전 | 2769 | ||
| 583 |
|
19년 전 | 3531 | |
| 582 |
|
19년 전 | 3727 | |
| 581 |
|
19년 전 | 3263 | |
| 580 | 19년 전 | 2490 | ||
| 579 | 19년 전 | 3084 | ||
| 578 | 19년 전 | 3994 | ||
| 577 | 19년 전 | 3827 | ||
| 576 | 19년 전 | 1765 | ||
| 575 |
|
19년 전 | 2225 | |
| 574 |
|
19년 전 | 5407 | |
| 573 | 19년 전 | 6735 | ||
| 572 | 19년 전 | 2578 | ||
| 571 | 19년 전 | 2115 | ||
| 570 | 19년 전 | 2605 | ||
| 569 | 19년 전 | 3315 | ||
| 568 | 19년 전 | 3542 | ||
| 567 | 19년 전 | 2986 | ||
| 566 | 19년 전 | 2606 | ||
| 565 |
|
19년 전 | 4729 | |
| 564 |
|
19년 전 | 6737 | |
| 563 |
|
19년 전 | 5017 | |
| 562 |
|
19년 전 | 5961 | |
| 561 |
|
19년 전 | 2709 | |
| 560 |
|
19년 전 | 2560 | |
| 559 |
|
19년 전 | 2227 | |
| 558 |
|
19년 전 | 2306 | |
| 557 | 19년 전 | 4596 | ||
| 556 |
|
19년 전 | 4673 | |
| 555 | 19년 전 | 2517 | ||
| 554 | 19년 전 | 2182 | ||
| 553 | 19년 전 | 2529 | ||
| 552 |
|
19년 전 | 3173 | |
| 551 | 19년 전 | 2947 | ||
| 550 |
|
19년 전 | 1905 | |
| 549 |
|
19년 전 | 1965 | |
| 548 |
|
19년 전 | 3156 | |
| 547 |
|
19년 전 | 2358 | |
| 546 |
|
19년 전 | 3629 | |
| 545 |
|
19년 전 | 2559 | |
| 544 |
|
19년 전 | 1860 | |
| 543 |
|
19년 전 | 2460 | |
| 542 |
|
19년 전 | 1732 | |
| 541 |
|
19년 전 | 1414 | |
| 540 |
|
19년 전 | 1554 | |
| 539 |
|
19년 전 | 1809 | |
| 538 |
|
19년 전 | 1591 | |
| 537 |
|
19년 전 | 1795 | |
| 536 |
|
19년 전 | 1568 | |
| 535 |
|
19년 전 | 1962 | |
| 534 |
|
19년 전 | 1853 | |
| 533 |
|
19년 전 | 1457 | |
| 532 |
|
19년 전 | 1449 | |
| 531 |
|
19년 전 | 1369 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기