<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="" />
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 330 |
prosper
|
20년 전 | 2243 | |
| 329 |
prosper
|
20년 전 | 1854 | |
| 328 |
prosper
|
20년 전 | 1662 | |
| 327 | 20년 전 | 3742 | ||
| 326 | 20년 전 | 4819 | ||
| 325 |
hwatta
|
20년 전 | 2440 | |
| 324 |
|
20년 전 | 3119 | |
| 323 | 20년 전 | 5830 | ||
| 322 |
hwatta
|
20년 전 | 2818 | |
| 321 |
hwatta
|
20년 전 | 2296 | |
| 320 |
yesmoa
|
20년 전 | 4554 | |
| 319 | 20년 전 | 2651 | ||
| 318 | 20년 전 | 2225 | ||
| 317 |
kyodon
|
20년 전 | 2751 | |
| 316 | 20년 전 | 2555 | ||
| 315 |
|
20년 전 | 2868 | |
| 314 |
|
20년 전 | 3308 | |
| 313 |
|
20년 전 | 2624 | |
| 312 |
yesmoa
|
20년 전 | 4698 | |
| 311 | 20년 전 | 3229 | ||
| 310 |
홀로남은자
|
20년 전 | 4539 | |
| 309 | 20년 전 | 2993 | ||
| 308 | 20년 전 | 4107 | ||
| 307 | 20년 전 | 4341 | ||
| 306 | 20년 전 | 7010 | ||
| 305 | 20년 전 | 3844 | ||
| 304 | 20년 전 | 2742 | ||
| 303 |
크리스탈처럼
|
20년 전 | 4375 | |
| 302 | 20년 전 | 2188 | ||
| 301 |
|
20년 전 | 4311 | |
| 300 | 20년 전 | 3769 | ||
| 299 | 20년 전 | 2572 | ||
| 298 | 20년 전 | 4797 | ||
| 297 |
|
20년 전 | 2507 | |
| 296 | 20년 전 | 4500 | ||
| 295 | 20년 전 | 3552 | ||
| 294 | 20년 전 | 3570 | ||
| 293 | 20년 전 | 3806 | ||
| 292 | 20년 전 | 3189 | ||
| 291 |
yesmoa
|
20년 전 | 5902 | |
| 290 | 20년 전 | 2925 | ||
| 289 | 20년 전 | 5831 | ||
| 288 |
|
20년 전 | 2351 | |
| 287 |
|
20년 전 | 1762 | |
| 286 |
|
20년 전 | 2124 | |
| 285 |
|
20년 전 | 3521 | |
| 284 |
|
20년 전 | 2020 | |
| 283 |
|
20년 전 | 4386 | |
| 282 | 20년 전 | 3367 | ||
| 281 |
|
20년 전 | 2192 | |
| 280 |
|
20년 전 | 7785 | |
| 279 | 20년 전 | 5523 | ||
| 278 | 20년 전 | 2974 | ||
| 277 |
|
20년 전 | 5556 | |
| 276 | 20년 전 | 2345 | ||
| 275 | 20년 전 | 2582 | ||
| 274 | 20년 전 | 2350 | ||
| 273 | 20년 전 | 2199 | ||
| 272 | 20년 전 | 2120 | ||
| 271 | 20년 전 | 2589 | ||
| 270 | 20년 전 | 2584 | ||
| 269 | 20년 전 | 2459 | ||
| 268 | 20년 전 | 2665 | ||
| 267 | 20년 전 | 2349 | ||
| 266 | 20년 전 | 2552 | ||
| 265 | 20년 전 | 3483 | ||
| 264 |
|
20년 전 | 5340 | |
| 263 |
|
20년 전 | 3702 | |
| 262 | 20년 전 | 3157 | ||
| 261 |
허저비
|
20년 전 | 5909 | |
| 260 |
|
20년 전 | 5701 | |
| 259 | 20년 전 | 4123 | ||
| 258 | 20년 전 | 2365 | ||
| 257 | 20년 전 | 3168 | ||
| 256 | 20년 전 | 1889 | ||
| 255 | 20년 전 | 1549 | ||
| 254 | 20년 전 | 3125 | ||
| 253 | 20년 전 | 3535 | ||
| 252 | 20년 전 | 5090 | ||
| 251 | 20년 전 | 5794 | ||
| 250 | 20년 전 | 3659 | ||
| 249 | 20년 전 | 4992 | ||
| 248 | 20년 전 | 3271 | ||
| 247 | 20년 전 | 3620 | ||
| 246 |
|
20년 전 | 7930 | |
| 245 |
|
20년 전 | 5887 | |
| 244 | 20년 전 | 4467 | ||
| 243 |
|
20년 전 | 4024 | |
| 242 | 20년 전 | 2753 | ||
| 241 | 20년 전 | 2694 | ||
| 240 | 20년 전 | 2336 | ||
| 239 | 20년 전 | 1637 | ||
| 238 |
아우겐나이스
|
20년 전 | 2242 | |
| 237 |
email
|
20년 전 | 3664 | |
| 236 | 20년 전 | 4125 | ||
| 235 | 20년 전 | 10443 | ||
| 234 | 20년 전 | 5040 | ||
| 233 | 20년 전 | 3357 | ||
| 232 | 20년 전 | 3179 | ||
| 231 | 20년 전 | 3811 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기