<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="" />
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 230 | 20년 전 | 2039 | ||
| 229 | 20년 전 | 3131 | ||
| 228 | 20년 전 | 3322 | ||
| 227 | 20년 전 | 2384 | ||
| 226 | 20년 전 | 5460 | ||
| 225 | 20년 전 | 2512 | ||
| 224 | 20년 전 | 2957 | ||
| 223 | 20년 전 | 4198 | ||
| 222 | 20년 전 | 2597 | ||
| 221 | 20년 전 | 2280 | ||
| 220 | 20년 전 | 3671 | ||
| 219 | 20년 전 | 2063 | ||
| 218 | 20년 전 | 3555 | ||
| 217 | 20년 전 | 2471 | ||
| 216 | 20년 전 | 2899 | ||
| 215 | 20년 전 | 2207 | ||
| 214 | 20년 전 | 3326 | ||
| 213 | 20년 전 | 2909 | ||
| 212 | 20년 전 | 3040 | ||
| 211 | 20년 전 | 2141 | ||
| 210 | 20년 전 | 1876 | ||
| 209 | 20년 전 | 2335 | ||
| 208 | 20년 전 | 1965 | ||
| 207 | 20년 전 | 1668 | ||
| 206 | 20년 전 | 1849 | ||
| 205 | 20년 전 | 3940 | ||
| 204 | 20년 전 | 1671 | ||
| 203 | 20년 전 | 2003 | ||
| 202 | 20년 전 | 2349 | ||
| 201 | 20년 전 | 1816 | ||
| 200 | 20년 전 | 2947 | ||
| 199 | 20년 전 | 2001 | ||
| 198 | 20년 전 | 2079 | ||
| 197 | 20년 전 | 3643 | ||
| 196 | 20년 전 | 2967 | ||
| 195 | 20년 전 | 2086 | ||
| 194 | 20년 전 | 10235 | ||
| 193 | 20년 전 | 2238 | ||
| 192 | 20년 전 | 1583 | ||
| 191 | 20년 전 | 2655 | ||
| 190 | 20년 전 | 2283 | ||
| 189 | 20년 전 | 1676 | ||
| 188 | 20년 전 | 1465 | ||
| 187 | 20년 전 | 1898 | ||
| 186 | 20년 전 | 1697 | ||
| 185 | 20년 전 | 1735 | ||
| 184 | 20년 전 | 2331 | ||
| 183 | 20년 전 | 1541 | ||
| 182 | 20년 전 | 1475 | ||
| 181 | 20년 전 | 1614 | ||
| 180 | 20년 전 | 2710 | ||
| 179 | 20년 전 | 1790 | ||
| 178 | 20년 전 | 1843 | ||
| 177 | 20년 전 | 1968 | ||
| 176 | 20년 전 | 1794 | ||
| 175 | 20년 전 | 1866 | ||
| 174 | 20년 전 | 1689 | ||
| 173 | 20년 전 | 2054 | ||
| 172 | 20년 전 | 1779 | ||
| 171 | 20년 전 | 2551 | ||
| 170 | 20년 전 | 2260 | ||
| 169 | 20년 전 | 2537 | ||
| 168 | 20년 전 | 1467 | ||
| 167 | 20년 전 | 1565 | ||
| 166 | 20년 전 | 2142 | ||
| 165 | 20년 전 | 1607 | ||
| 164 | 20년 전 | 3754 | ||
| 163 | 20년 전 | 2648 | ||
| 162 | 20년 전 | 2066 | ||
| 161 | 20년 전 | 2774 | ||
| 160 | 20년 전 | 1721 | ||
| 159 | 20년 전 | 1596 | ||
| 158 | 20년 전 | 2546 | ||
| 157 | 20년 전 | 1476 | ||
| 156 | 20년 전 | 1715 | ||
| 155 | 20년 전 | 3223 | ||
| 154 | 20년 전 | 1878 | ||
| 153 | 20년 전 | 1610 | ||
| 152 | 20년 전 | 4937 | ||
| 151 | 20년 전 | 4563 | ||
| 150 | 20년 전 | 3503 | ||
| 149 | 20년 전 | 3772 | ||
| 148 | 20년 전 | 7055 | ||
| 147 | 20년 전 | 3537 | ||
| 146 | 20년 전 | 2604 | ||
| 145 | 20년 전 | 2601 | ||
| 144 | 20년 전 | 7162 | ||
| 143 | 20년 전 | 4584 | ||
| 142 | 20년 전 | 1890 | ||
| 141 | 20년 전 | 3247 | ||
| 140 | 20년 전 | 1950 | ||
| 139 | 20년 전 | 1544 | ||
| 138 | 20년 전 | 2289 | ||
| 137 | 20년 전 | 1769 | ||
| 136 | 20년 전 | 1447 | ||
| 135 | 20년 전 | 1781 | ||
| 134 | 20년 전 | 2959 | ||
| 133 | 20년 전 | 2433 | ||
| 132 | 20년 전 | 1703 | ||
| 131 | 20년 전 | 1631 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기