<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="" />
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8030 | 9년 전 | 356 | ||
| 8029 | 9년 전 | 288 | ||
| 8028 | 9년 전 | 255 | ||
| 8027 | 9년 전 | 267 | ||
| 8026 | 9년 전 | 313 | ||
| 8025 | 9년 전 | 367 | ||
| 8024 | 9년 전 | 333 | ||
| 8023 | 9년 전 | 380 | ||
| 8022 | 9년 전 | 310 | ||
| 8021 | 9년 전 | 315 | ||
| 8020 | 9년 전 | 309 | ||
| 8019 | 9년 전 | 332 | ||
| 8018 | 9년 전 | 438 | ||
| 8017 | 9년 전 | 531 | ||
| 8016 | 9년 전 | 317 | ||
| 8015 | 9년 전 | 382 | ||
| 8014 | 9년 전 | 315 | ||
| 8013 | 9년 전 | 229 | ||
| 8012 | 9년 전 | 238 | ||
| 8011 | 9년 전 | 436 | ||
| 8010 | 9년 전 | 290 | ||
| 8009 | 9년 전 | 274 | ||
| 8008 | 9년 전 | 279 | ||
| 8007 | 9년 전 | 421 | ||
| 8006 | 9년 전 | 455 | ||
| 8005 |
|
9년 전 | 959 | |
| 8004 | 9년 전 | 343 | ||
| 8003 | 9년 전 | 413 | ||
| 8002 | 9년 전 | 317 | ||
| 8001 |
|
9년 전 | 662 | |
| 8000 | 9년 전 | 406 | ||
| 7999 | 9년 전 | 367 | ||
| 7998 | 9년 전 | 418 | ||
| 7997 | 9년 전 | 304 | ||
| 7996 | 9년 전 | 530 | ||
| 7995 | 9년 전 | 445 | ||
| 7994 | 9년 전 | 299 | ||
| 7993 | 9년 전 | 367 | ||
| 7992 | 9년 전 | 499 | ||
| 7991 | 9년 전 | 243 | ||
| 7990 | 9년 전 | 263 | ||
| 7989 | 9년 전 | 294 | ||
| 7988 | 9년 전 | 719 | ||
| 7987 | 9년 전 | 413 | ||
| 7986 | 9년 전 | 404 | ||
| 7985 | 9년 전 | 502 | ||
| 7984 | 9년 전 | 424 | ||
| 7983 | 9년 전 | 647 | ||
| 7982 | 9년 전 | 505 | ||
| 7981 | 9년 전 | 455 | ||
| 7980 | 9년 전 | 482 | ||
| 7979 | 9년 전 | 453 | ||
| 7978 | 9년 전 | 434 | ||
| 7977 | 9년 전 | 375 | ||
| 7976 | 9년 전 | 844 | ||
| 7975 | 9년 전 | 354 | ||
| 7974 | 9년 전 | 384 | ||
| 7973 | 9년 전 | 586 | ||
| 7972 | 9년 전 | 371 | ||
| 7971 | 9년 전 | 451 | ||
| 7970 | 9년 전 | 285 | ||
| 7969 | 9년 전 | 535 | ||
| 7968 | 9년 전 | 374 | ||
| 7967 | 9년 전 | 360 | ||
| 7966 | 9년 전 | 362 | ||
| 7965 |
|
9년 전 | 1007 | |
| 7964 | 9년 전 | 392 | ||
| 7963 | 9년 전 | 382 | ||
| 7962 | 9년 전 | 362 | ||
| 7961 |
전갈자리남자
|
9년 전 | 499 | |
| 7960 | 9년 전 | 955 | ||
| 7959 | 9년 전 | 544 | ||
| 7958 | 9년 전 | 400 | ||
| 7957 | 9년 전 | 360 | ||
| 7956 | 9년 전 | 365 | ||
| 7955 | 9년 전 | 437 | ||
| 7954 | 9년 전 | 365 | ||
| 7953 | 9년 전 | 419 | ||
| 7952 | 9년 전 | 345 | ||
| 7951 | 9년 전 | 492 | ||
| 7950 | 9년 전 | 381 | ||
| 7949 | 9년 전 | 381 | ||
| 7948 | 9년 전 | 319 | ||
| 7947 | 9년 전 | 924 | ||
| 7946 | 9년 전 | 416 | ||
| 7945 | 9년 전 | 384 | ||
| 7944 | 9년 전 | 397 | ||
| 7943 | 9년 전 | 361 | ||
| 7942 | 9년 전 | 400 | ||
| 7941 | 9년 전 | 380 | ||
| 7940 | 9년 전 | 871 | ||
| 7939 | 9년 전 | 337 | ||
| 7938 | 9년 전 | 369 | ||
| 7937 | 9년 전 | 271 | ||
| 7936 | 9년 전 | 873 | ||
| 7935 | 9년 전 | 433 | ||
| 7934 | 9년 전 | 398 | ||
| 7933 | 9년 전 | 490 | ||
| 7932 | 9년 전 | 457 | ||
| 7931 | 9년 전 | 514 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기