<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="" />
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 130 | 20년 전 | 4000 | ||
| 129 | 20년 전 | 2938 | ||
| 128 | 20년 전 | 3670 | ||
| 127 | 20년 전 | 3503 | ||
| 126 | 20년 전 | 3759 | ||
| 125 | 20년 전 | 8588 | ||
| 124 | 20년 전 | 2601 | ||
| 123 | 20년 전 | 3744 | ||
| 122 | 20년 전 | 3205 | ||
| 121 | 20년 전 | 2609 | ||
| 120 | 20년 전 | 2670 | ||
| 119 | 20년 전 | 2583 | ||
| 118 | 20년 전 | 2859 | ||
| 117 |
|
20년 전 | 3056 | |
| 116 | 20년 전 | 5309 | ||
| 115 | 20년 전 | 3921 | ||
| 114 | 20년 전 | 4971 | ||
| 113 | 20년 전 | 6209 | ||
| 112 | 20년 전 | 7322 | ||
| 111 | 20년 전 | 18430 | ||
| 110 | 20년 전 | 6873 | ||
| 109 | 20년 전 | 2878 | ||
| 108 | 20년 전 | 4137 | ||
| 107 |
prosper
|
20년 전 | 2499 | |
| 106 |
prosper
|
20년 전 | 4316 | |
| 105 |
아우겐나이스
|
20년 전 | 2913 | |
| 104 | 20년 전 | 2258 | ||
| 103 | 20년 전 | 2477 | ||
| 102 | 20년 전 | 2256 | ||
| 101 | 20년 전 | 2574 | ||
| 100 | 20년 전 | 1746 | ||
| 99 | 20년 전 | 1566 | ||
| 98 | 20년 전 | 1617 | ||
| 97 | 20년 전 | 2129 | ||
| 96 | 20년 전 | 1871 | ||
| 95 | 20년 전 | 2374 | ||
| 94 | 20년 전 | 3564 | ||
| 93 | 20년 전 | 1559 | ||
| 92 | 20년 전 | 1756 | ||
| 91 | 20년 전 | 3180 | ||
| 90 | 20년 전 | 2340 | ||
| 89 | 20년 전 | 3169 | ||
| 88 | 20년 전 | 2870 | ||
| 87 | 20년 전 | 3294 | ||
| 86 | 20년 전 | 5132 | ||
| 85 | 20년 전 | 2520 | ||
| 84 | 20년 전 | 4824 | ||
| 83 | 20년 전 | 2502 | ||
| 82 | 20년 전 | 3119 | ||
| 81 | 20년 전 | 7624 | ||
| 80 | 20년 전 | 3822 | ||
| 79 | 20년 전 | 3206 | ||
| 78 | 20년 전 | 4694 | ||
| 77 | 20년 전 | 2897 | ||
| 76 | 20년 전 | 6216 | ||
| 75 | 20년 전 | 4470 | ||
| 74 | 20년 전 | 5782 | ||
| 73 | 20년 전 | 3625 | ||
| 72 | 20년 전 | 5967 | ||
| 71 | 20년 전 | 3117 | ||
| 70 | 20년 전 | 2839 | ||
| 69 | 20년 전 | 2640 | ||
| 68 | 20년 전 | 2445 | ||
| 67 | 20년 전 | 2655 | ||
| 66 | 20년 전 | 2671 | ||
| 65 | 20년 전 | 3789 | ||
| 64 | 20년 전 | 2825 | ||
| 63 | 20년 전 | 2457 | ||
| 62 | 20년 전 | 2265 | ||
| 61 | 20년 전 | 3070 | ||
| 60 | 20년 전 | 3134 | ||
| 59 | 20년 전 | 2515 | ||
| 58 | 20년 전 | 2589 | ||
| 57 | 20년 전 | 2967 | ||
| 56 | 20년 전 | 2317 | ||
| 55 | 20년 전 | 2761 | ||
| 54 | 20년 전 | 2128 | ||
| 53 | 20년 전 | 2349 | ||
| 52 | 20년 전 | 2699 | ||
| 51 |
prosper
|
21년 전 | 2351 | |
| 50 |
prosper
|
21년 전 | 2170 | |
| 49 | 21년 전 | 2179 | ||
| 48 | 21년 전 | 2340 | ||
| 47 | 21년 전 | 1932 | ||
| 46 | 21년 전 | 1928 | ||
| 45 | 21년 전 | 2134 | ||
| 44 | 21년 전 | 2360 | ||
| 43 | 21년 전 | 4576 | ||
| 42 |
prosper
|
21년 전 | 2715 | |
| 41 |
prosper
|
21년 전 | 2114 | |
| 40 | 21년 전 | 2178 | ||
| 39 | 21년 전 | 2146 | ||
| 38 | 21년 전 | 2418 | ||
| 37 | 21년 전 | 2568 | ||
| 36 | 21년 전 | 1771 | ||
| 35 | 21년 전 | 4072 | ||
| 34 | 21년 전 | 3850 | ||
| 33 | 21년 전 | 2990 | ||
| 32 |
prosper
|
21년 전 | 2903 | |
| 31 | 21년 전 | 5277 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기