<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년 전 | 2087 | ||
| 229 | 20년 전 | 3146 | ||
| 228 | 20년 전 | 3339 | ||
| 227 | 20년 전 | 2416 | ||
| 226 | 20년 전 | 5497 | ||
| 225 | 20년 전 | 2534 | ||
| 224 | 20년 전 | 2980 | ||
| 223 | 20년 전 | 4212 | ||
| 222 | 20년 전 | 2612 | ||
| 221 | 20년 전 | 2302 | ||
| 220 | 20년 전 | 3690 | ||
| 219 | 20년 전 | 2083 | ||
| 218 | 20년 전 | 3584 | ||
| 217 | 20년 전 | 2495 | ||
| 216 | 20년 전 | 2932 | ||
| 215 | 20년 전 | 2241 | ||
| 214 | 20년 전 | 3350 | ||
| 213 | 20년 전 | 2930 | ||
| 212 | 20년 전 | 3072 | ||
| 211 | 20년 전 | 2156 | ||
| 210 | 20년 전 | 1928 | ||
| 209 | 20년 전 | 2374 | ||
| 208 | 20년 전 | 2002 | ||
| 207 | 20년 전 | 1690 | ||
| 206 | 20년 전 | 1865 | ||
| 205 | 20년 전 | 3958 | ||
| 204 | 20년 전 | 1684 | ||
| 203 | 20년 전 | 2023 | ||
| 202 | 20년 전 | 2366 | ||
| 201 | 20년 전 | 1839 | ||
| 200 | 20년 전 | 2969 | ||
| 199 | 20년 전 | 2012 | ||
| 198 | 20년 전 | 2096 | ||
| 197 | 20년 전 | 3667 | ||
| 196 | 20년 전 | 3006 | ||
| 195 | 20년 전 | 2117 | ||
| 194 | 20년 전 | 10267 | ||
| 193 | 20년 전 | 2262 | ||
| 192 | 20년 전 | 1615 | ||
| 191 | 20년 전 | 2678 | ||
| 190 | 20년 전 | 2299 | ||
| 189 | 20년 전 | 1701 | ||
| 188 | 20년 전 | 1503 | ||
| 187 | 20년 전 | 1923 | ||
| 186 | 20년 전 | 1748 | ||
| 185 | 20년 전 | 1793 | ||
| 184 | 20년 전 | 2378 | ||
| 183 | 20년 전 | 1588 | ||
| 182 | 20년 전 | 1496 | ||
| 181 | 20년 전 | 1638 | ||
| 180 | 20년 전 | 2737 | ||
| 179 | 20년 전 | 1822 | ||
| 178 | 20년 전 | 1873 | ||
| 177 | 20년 전 | 2002 | ||
| 176 | 20년 전 | 1811 | ||
| 175 | 20년 전 | 1878 | ||
| 174 | 20년 전 | 1707 | ||
| 173 | 20년 전 | 2067 | ||
| 172 | 20년 전 | 1793 | ||
| 171 | 20년 전 | 2574 | ||
| 170 | 20년 전 | 2297 | ||
| 169 | 20년 전 | 2572 | ||
| 168 | 20년 전 | 1507 | ||
| 167 | 20년 전 | 1598 | ||
| 166 | 20년 전 | 2170 | ||
| 165 | 20년 전 | 1652 | ||
| 164 | 20년 전 | 3788 | ||
| 163 | 20년 전 | 2687 | ||
| 162 | 20년 전 | 2104 | ||
| 161 | 20년 전 | 2816 | ||
| 160 | 20년 전 | 1753 | ||
| 159 | 20년 전 | 1631 | ||
| 158 | 20년 전 | 2581 | ||
| 157 | 20년 전 | 1506 | ||
| 156 | 20년 전 | 1769 | ||
| 155 | 20년 전 | 3243 | ||
| 154 | 20년 전 | 1919 | ||
| 153 | 20년 전 | 1636 | ||
| 152 | 20년 전 | 4973 | ||
| 151 | 20년 전 | 4592 | ||
| 150 | 20년 전 | 3533 | ||
| 149 | 20년 전 | 3816 | ||
| 148 | 20년 전 | 7099 | ||
| 147 | 20년 전 | 3568 | ||
| 146 | 20년 전 | 2644 | ||
| 145 | 20년 전 | 2636 | ||
| 144 | 20년 전 | 7177 | ||
| 143 | 20년 전 | 4622 | ||
| 142 | 20년 전 | 1909 | ||
| 141 | 20년 전 | 3258 | ||
| 140 | 20년 전 | 1979 | ||
| 139 | 20년 전 | 1612 | ||
| 138 | 20년 전 | 2326 | ||
| 137 | 20년 전 | 1798 | ||
| 136 | 20년 전 | 1468 | ||
| 135 | 20년 전 | 1806 | ||
| 134 | 20년 전 | 2982 | ||
| 133 | 20년 전 | 2470 | ||
| 132 | 20년 전 | 1714 | ||
| 131 | 20년 전 | 1666 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기