<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년 전 | 4003 | ||
| 129 | 20년 전 | 2941 | ||
| 128 | 20년 전 | 3675 | ||
| 127 | 20년 전 | 3509 | ||
| 126 | 20년 전 | 3764 | ||
| 125 | 20년 전 | 8593 | ||
| 124 | 20년 전 | 2602 | ||
| 123 | 20년 전 | 3747 | ||
| 122 | 20년 전 | 3209 | ||
| 121 | 20년 전 | 2611 | ||
| 120 | 20년 전 | 2673 | ||
| 119 | 20년 전 | 2585 | ||
| 118 | 20년 전 | 2861 | ||
| 117 |
|
20년 전 | 3061 | |
| 116 | 20년 전 | 5313 | ||
| 115 | 20년 전 | 3924 | ||
| 114 | 20년 전 | 4973 | ||
| 113 | 20년 전 | 6212 | ||
| 112 | 20년 전 | 7324 | ||
| 111 | 20년 전 | 18431 | ||
| 110 | 20년 전 | 6875 | ||
| 109 | 20년 전 | 2882 | ||
| 108 | 20년 전 | 4138 | ||
| 107 |
prosper
|
20년 전 | 2501 | |
| 106 |
prosper
|
20년 전 | 4319 | |
| 105 |
아우겐나이스
|
20년 전 | 2913 | |
| 104 | 20년 전 | 2259 | ||
| 103 | 20년 전 | 2478 | ||
| 102 | 20년 전 | 2259 | ||
| 101 | 20년 전 | 2576 | ||
| 100 | 20년 전 | 1749 | ||
| 99 | 20년 전 | 1571 | ||
| 98 | 20년 전 | 1618 | ||
| 97 | 20년 전 | 2130 | ||
| 96 | 20년 전 | 1876 | ||
| 95 | 20년 전 | 2377 | ||
| 94 | 20년 전 | 3567 | ||
| 93 | 20년 전 | 1563 | ||
| 92 | 20년 전 | 1757 | ||
| 91 | 20년 전 | 3183 | ||
| 90 | 20년 전 | 2343 | ||
| 89 | 20년 전 | 3171 | ||
| 88 | 20년 전 | 2872 | ||
| 87 | 20년 전 | 3297 | ||
| 86 | 20년 전 | 5135 | ||
| 85 | 20년 전 | 2520 | ||
| 84 | 20년 전 | 4825 | ||
| 83 | 20년 전 | 2507 | ||
| 82 | 20년 전 | 3121 | ||
| 81 | 20년 전 | 7630 | ||
| 80 | 20년 전 | 3825 | ||
| 79 | 20년 전 | 3210 | ||
| 78 | 20년 전 | 4697 | ||
| 77 | 20년 전 | 2902 | ||
| 76 | 20년 전 | 6219 | ||
| 75 | 20년 전 | 4470 | ||
| 74 | 20년 전 | 5783 | ||
| 73 | 20년 전 | 3626 | ||
| 72 | 20년 전 | 5970 | ||
| 71 | 20년 전 | 3118 | ||
| 70 | 20년 전 | 2844 | ||
| 69 | 20년 전 | 2642 | ||
| 68 | 20년 전 | 2447 | ||
| 67 | 20년 전 | 2656 | ||
| 66 | 20년 전 | 2673 | ||
| 65 | 20년 전 | 3791 | ||
| 64 | 20년 전 | 2827 | ||
| 63 | 20년 전 | 2457 | ||
| 62 | 20년 전 | 2265 | ||
| 61 | 20년 전 | 3078 | ||
| 60 | 20년 전 | 3134 | ||
| 59 | 20년 전 | 2520 | ||
| 58 | 20년 전 | 2591 | ||
| 57 | 20년 전 | 2967 | ||
| 56 | 20년 전 | 2321 | ||
| 55 | 20년 전 | 2762 | ||
| 54 | 20년 전 | 2128 | ||
| 53 | 20년 전 | 2354 | ||
| 52 | 20년 전 | 2700 | ||
| 51 |
prosper
|
21년 전 | 2351 | |
| 50 |
prosper
|
21년 전 | 2170 | |
| 49 | 21년 전 | 2180 | ||
| 48 | 21년 전 | 2344 | ||
| 47 | 21년 전 | 1935 | ||
| 46 | 21년 전 | 1930 | ||
| 45 | 21년 전 | 2136 | ||
| 44 | 21년 전 | 2362 | ||
| 43 | 21년 전 | 4583 | ||
| 42 |
prosper
|
21년 전 | 2716 | |
| 41 |
prosper
|
21년 전 | 2114 | |
| 40 | 21년 전 | 2180 | ||
| 39 | 21년 전 | 2148 | ||
| 38 | 21년 전 | 2420 | ||
| 37 | 21년 전 | 2569 | ||
| 36 | 21년 전 | 1777 | ||
| 35 | 21년 전 | 4073 | ||
| 34 | 21년 전 | 3851 | ||
| 33 | 21년 전 | 2990 | ||
| 32 |
prosper
|
21년 전 | 2905 | |
| 31 | 21년 전 | 5284 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기