<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년 전 | 2057 | ||
| 229 | 20년 전 | 3134 | ||
| 228 | 20년 전 | 3322 | ||
| 227 | 20년 전 | 2389 | ||
| 226 | 20년 전 | 5475 | ||
| 225 | 20년 전 | 2518 | ||
| 224 | 20년 전 | 2964 | ||
| 223 | 20년 전 | 4204 | ||
| 222 | 20년 전 | 2604 | ||
| 221 | 20년 전 | 2288 | ||
| 220 | 20년 전 | 3674 | ||
| 219 | 20년 전 | 2067 | ||
| 218 | 20년 전 | 3562 | ||
| 217 | 20년 전 | 2475 | ||
| 216 | 20년 전 | 2908 | ||
| 215 | 20년 전 | 2223 | ||
| 214 | 20년 전 | 3334 | ||
| 213 | 20년 전 | 2914 | ||
| 212 | 20년 전 | 3045 | ||
| 211 | 20년 전 | 2144 | ||
| 210 | 20년 전 | 1891 | ||
| 209 | 20년 전 | 2349 | ||
| 208 | 20년 전 | 1973 | ||
| 207 | 20년 전 | 1672 | ||
| 206 | 20년 전 | 1853 | ||
| 205 | 20년 전 | 3946 | ||
| 204 | 20년 전 | 1672 | ||
| 203 | 20년 전 | 2009 | ||
| 202 | 20년 전 | 2357 | ||
| 201 | 20년 전 | 1822 | ||
| 200 | 20년 전 | 2954 | ||
| 199 | 20년 전 | 2004 | ||
| 198 | 20년 전 | 2084 | ||
| 197 | 20년 전 | 3648 | ||
| 196 | 20년 전 | 2978 | ||
| 195 | 20년 전 | 2090 | ||
| 194 | 20년 전 | 10242 | ||
| 193 | 20년 전 | 2243 | ||
| 192 | 20년 전 | 1600 | ||
| 191 | 20년 전 | 2660 | ||
| 190 | 20년 전 | 2287 | ||
| 189 | 20년 전 | 1679 | ||
| 188 | 20년 전 | 1473 | ||
| 187 | 20년 전 | 1903 | ||
| 186 | 20년 전 | 1708 | ||
| 185 | 20년 전 | 1751 | ||
| 184 | 20년 전 | 2345 | ||
| 183 | 20년 전 | 1556 | ||
| 182 | 20년 전 | 1482 | ||
| 181 | 20년 전 | 1622 | ||
| 180 | 20년 전 | 2716 | ||
| 179 | 20년 전 | 1800 | ||
| 178 | 20년 전 | 1855 | ||
| 177 | 20년 전 | 1979 | ||
| 176 | 20년 전 | 1798 | ||
| 175 | 20년 전 | 1870 | ||
| 174 | 20년 전 | 1693 | ||
| 173 | 20년 전 | 2058 | ||
| 172 | 20년 전 | 1782 | ||
| 171 | 20년 전 | 2555 | ||
| 170 | 20년 전 | 2268 | ||
| 169 | 20년 전 | 2548 | ||
| 168 | 20년 전 | 1474 | ||
| 167 | 20년 전 | 1577 | ||
| 166 | 20년 전 | 2148 | ||
| 165 | 20년 전 | 1622 | ||
| 164 | 20년 전 | 3767 | ||
| 163 | 20년 전 | 2660 | ||
| 162 | 20년 전 | 2078 | ||
| 161 | 20년 전 | 2786 | ||
| 160 | 20년 전 | 1731 | ||
| 159 | 20년 전 | 1607 | ||
| 158 | 20년 전 | 2560 | ||
| 157 | 20년 전 | 1488 | ||
| 156 | 20년 전 | 1735 | ||
| 155 | 20년 전 | 3227 | ||
| 154 | 20년 전 | 1889 | ||
| 153 | 20년 전 | 1620 | ||
| 152 | 20년 전 | 4947 | ||
| 151 | 20년 전 | 4574 | ||
| 150 | 20년 전 | 3512 | ||
| 149 | 20년 전 | 3786 | ||
| 148 | 20년 전 | 7068 | ||
| 147 | 20년 전 | 3549 | ||
| 146 | 20년 전 | 2617 | ||
| 145 | 20년 전 | 2612 | ||
| 144 | 20년 전 | 7166 | ||
| 143 | 20년 전 | 4595 | ||
| 142 | 20년 전 | 1899 | ||
| 141 | 20년 전 | 3250 | ||
| 140 | 20년 전 | 1957 | ||
| 139 | 20년 전 | 1571 | ||
| 138 | 20년 전 | 2302 | ||
| 137 | 20년 전 | 1775 | ||
| 136 | 20년 전 | 1451 | ||
| 135 | 20년 전 | 1782 | ||
| 134 | 20년 전 | 2964 | ||
| 133 | 20년 전 | 2447 | ||
| 132 | 20년 전 | 1705 | ||
| 131 | 20년 전 | 1637 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기