웹마스터 팁
page_full_width" class="col-xs-12" |cond="$__Context->page_full_width">
자바스크립트용 계산기 v1.0
2003.01.09 22:44
<HTML>
<HEAD>
<TITLE>자바스크립트 - 계산기 예제</TITLE>
</HEAD>
<body>
<center>
<script language="JavaScript">
<!-- Hide all this from non javascript browsers
NaN=0; // if someone typed characters and not digits we assume the value 0
var stack=0; // used to save the first value
var op='+'; // used to save the operator
var assume_equals=false; // used to calculate sum when didn't click '='
var nnum=true; // the first number is a new number
function digitpress(val) { // pressed a digit
if (nnum==false) { // append digit to digits currently on the screen
document.calc.screen.value = document.calc.screen.value+val;
}
else { // starting a new number
stack = parseFloat(document.calc.screen.value); // save prev number
document.calc.screen.value = val; // show new digit
nnum=false; // subsequent numbers get appended
}
return(true);
}
function pushop(s) { // pressed an operator (one of +, -, *, / or =)
if (s=='=' || assume_equals==true) { // we should update total on the screen
// grab the number on the screen now
v = parseFloat(document.calc.screen.value);
// catch division by zero
if (!(op == '/' && v == 0)) {
// work out the sum and put the result on the screen
document.calc.screen.value = eval((stack) + op + v);
if (s != '='+ ') { // save the operator and the current total
op = s;
stack = parseFloat(document.calc.screen.value);
}
else {
assume_equals = false; // this was an '=' so don't total on next op
}
}
else { // division by zero so display error message
document.calc.screen.value = "Error";
assume_equals = false; // this was an error so don't total on next op
}
nnum = true;
}
else { // otherwise save the current value and the op
assume_equals = true; // give total on next op
stack = parseFloat(document.calc.screen.value); // save current total
nnum=true; // next number to be entered is a new one
op=s; // remember the operator just typed
}
return(true);
}
// End of all the hidden stuff -->
</script>
<FORM name=calc METHOD="POST">
<TABLE border=1 align=center>
<tr align=center><td colspan=5 align=center><INPUT TYPE="float" NAME="screen" VALUE="0" SIZE=20,1 MAXLENGTH=20>
<tr align=center>
<TD> <INPUT TYPE="button" NAME="one" VALUE=" 1 " onClick="digitpress(1)">
<TD> <INPUT TYPE="button" NAME="two" VALUE=" 2 " onClick="digitpress(2)">
<TD> <INPUT TYPE="button" NAME="three" VALUE=" 3 " onClick="digitpress(3)">
<TD> <input type="button" name="plus" value=" + " onClick="pushop('+')">
<TD> <input type="button" name="clear" value=" 계산! " onClick="document.calc.screen.value=0;if (nnum==true) nnum = false;">
<TR align=center>
<td><INPUT TYPE="button" NAME="four" VALUE=" 4 " onClick="digitpress(4)">
<td><INPUT TYPE="button" NAME="five" VALUE=" 5 " onClick="digitpress(5)">
<td><INPUT TYPE="button" NAME="six" VALUE=" 6 " onClick="digitpress(6)">
<td><input type="button" name="minus" value=" - " onClick="pushop('-')">
<td></td>
<tr align=center>
<td><INPUT TYPE="button" NAME="seven" VALUE=" 7 " onClick="digitpress(7)">
<td><INPUT TYPE="button" NAME="eight" VALUE=" 8 " onClick="digitpress(8)">
<td><INPUT TYPE="button" NAME="nine" VALUE=" 9 " onClick="digitpress(9)">
<td><input type="button" name="times" value=" * " onClick="pushop('*')">
<td></td>
<tr align=center>
<td><INPUT TYPE="button" NAME="zero" VALUE=" 0 " onClick="digitpress(0)">
<td><INPUT TYPE="button" NAME="dot" VALUE=" . " onClick="digitpress('.'+ ')">
<TD><input type="button" name="equal" value=" = " onClick="pushop('=')">
<td><input type="button" name="divide" value=" / " onClick="pushop('/')">
<td></td>
</TABLE>
</FORM>
</body>
</html>
<HEAD>
<TITLE>자바스크립트 - 계산기 예제</TITLE>
</HEAD>
<body>
<center>
<script language="JavaScript">
<!-- Hide all this from non javascript browsers
NaN=0; // if someone typed characters and not digits we assume the value 0
var stack=0; // used to save the first value
var op='+'; // used to save the operator
var assume_equals=false; // used to calculate sum when didn't click '='
var nnum=true; // the first number is a new number
function digitpress(val) { // pressed a digit
if (nnum==false) { // append digit to digits currently on the screen
document.calc.screen.value = document.calc.screen.value+val;
}
else { // starting a new number
stack = parseFloat(document.calc.screen.value); // save prev number
document.calc.screen.value = val; // show new digit
nnum=false; // subsequent numbers get appended
}
return(true);
}
function pushop(s) { // pressed an operator (one of +, -, *, / or =)
if (s=='=' || assume_equals==true) { // we should update total on the screen
// grab the number on the screen now
v = parseFloat(document.calc.screen.value);
// catch division by zero
if (!(op == '/' && v == 0)) {
// work out the sum and put the result on the screen
document.calc.screen.value = eval((stack) + op + v);
if (s != '='+ ') { // save the operator and the current total
op = s;
stack = parseFloat(document.calc.screen.value);
}
else {
assume_equals = false; // this was an '=' so don't total on next op
}
}
else { // division by zero so display error message
document.calc.screen.value = "Error";
assume_equals = false; // this was an error so don't total on next op
}
nnum = true;
}
else { // otherwise save the current value and the op
assume_equals = true; // give total on next op
stack = parseFloat(document.calc.screen.value); // save current total
nnum=true; // next number to be entered is a new one
op=s; // remember the operator just typed
}
return(true);
}
// End of all the hidden stuff -->
</script>
<FORM name=calc METHOD="POST">
<TABLE border=1 align=center>
<tr align=center><td colspan=5 align=center><INPUT TYPE="float" NAME="screen" VALUE="0" SIZE=20,1 MAXLENGTH=20>
<tr align=center>
<TD> <INPUT TYPE="button" NAME="one" VALUE=" 1 " onClick="digitpress(1)">
<TD> <INPUT TYPE="button" NAME="two" VALUE=" 2 " onClick="digitpress(2)">
<TD> <INPUT TYPE="button" NAME="three" VALUE=" 3 " onClick="digitpress(3)">
<TD> <input type="button" name="plus" value=" + " onClick="pushop('+')">
<TD> <input type="button" name="clear" value=" 계산! " onClick="document.calc.screen.value=0;if (nnum==true) nnum = false;">
<TR align=center>
<td><INPUT TYPE="button" NAME="four" VALUE=" 4 " onClick="digitpress(4)">
<td><INPUT TYPE="button" NAME="five" VALUE=" 5 " onClick="digitpress(5)">
<td><INPUT TYPE="button" NAME="six" VALUE=" 6 " onClick="digitpress(6)">
<td><input type="button" name="minus" value=" - " onClick="pushop('-')">
<td></td>
<tr align=center>
<td><INPUT TYPE="button" NAME="seven" VALUE=" 7 " onClick="digitpress(7)">
<td><INPUT TYPE="button" NAME="eight" VALUE=" 8 " onClick="digitpress(8)">
<td><INPUT TYPE="button" NAME="nine" VALUE=" 9 " onClick="digitpress(9)">
<td><input type="button" name="times" value=" * " onClick="pushop('*')">
<td></td>
<tr align=center>
<td><INPUT TYPE="button" NAME="zero" VALUE=" 0 " onClick="digitpress(0)">
<td><INPUT TYPE="button" NAME="dot" VALUE=" . " onClick="digitpress('.'+ ')">
<TD><input type="button" name="equal" value=" = " onClick="pushop('=')">
<td><input type="button" name="divide" value=" / " onClick="pushop('/')">
<td></td>
</TABLE>
</FORM>
</body>
</html>
댓글 4
-
바코드™
2003.01.10 09:51
http://budapia.nayana.org/Calculater.html -
찐군
2003.01.10 13:26
음.. 예제 감사합니다.. 제 홈페이지가 아직 강좌란이 마련되지가 않아서^^; -
메트론
2003.01.29 21:47
10*2-1=10 ?????
계산이 정확이 이루어지지 않네요...
그리고 엔터치면 무조건 0으로 되구요... 흑흑... -
라엘
2003.02.10 22:16
ㅡㅡ?..; 아마 님이 잘못 이해를 하시고 계신듯..;
젝아 찐군님의 계산기중에 사용자분들이 이해가 잘 안될것 같은부분인 '계산'<= 이버튼을 약간 수정해서 젝아 예제를 만두렀습니다.. 이 예제는 그냥 '계산!' 이 글씨를 '새로운계산'으로 수정한것 뿐입니다..
http://user.chollian.net/~ckk21/webpia/javageasangi.htm
이것이 젝아 만둔 예제 입니다..
제목 | 글쓴이 | 날짜 |
---|---|---|
Mysql 4.0 버전에서 xe 설치해서 사용하기 [3] | amamamam.myid.net | 2007.08.13 |
(파일 첨부) 파일명 안보일 때 ... 없애기 [9] | Simulz | 2007.09.10 |
apmsetup5를 이용한 zbxe 설치 및 복구 [2] | 무도사 | 2007.11.02 |
UTF-8과 Euc-kr 동시에 사용하기 [10] | 써니a | 2007.11.05 |
제로보드에서 로그인후 테크노트에서 로그인정보 사용하기 [4] | JinHoHan | 2007.12.26 |
HTTP 406 Error 가 나오면서 업로드 안될때 [16] | plruto | 2008.02.08 |
[펌] Apache rewrite Module [8] | ☜ TeRy ☞ | 2008.02.22 |
자료 첨부 문제 | 단풍534 | 2008.07.15 |
[초간단]회원팝업메뉴에 메뉴넣기 [1] | Pw-NET | 2008.11.30 |
XE DB 백업한거 복원 하기 [2] | 라르게덴 | 2009.03.17 |
폴더에 올린 mp3를 자동으로 podcast로 만들어주는 소스... ^^ [2] | 하얀마법 | 2010.09.11 |
Microsoft 에이전트를 이용해서 멋나게 꾸며보자 -_-)/ [7] | 티르-_-)/ | 2002.02.23 |
textarea 입력받는 글자수 제한하는 스크립트 [5] | 오기 | 2002.01.17 |
▩찾기 기능을 내 홈페이지에 넣어보자 [1] | ▩윤미 | 2002.02.26 |
윈도우 할아버지 나타나게 하기 [11] | 행복한고니 | 2002.11.14 |
[re] 아래 행복한고니님의 할아버지 소스의 업글(?) 버젼입니다. [2] | Mahican | 2002.12.02 |
아래 행복한고니님의 할아버지 소스의 업글(?) 버젼입니다. [7] | (づ_-) 커터칼 | 2002.11.18 |
자바스크립트용 계산기 v1.0 [4] | 찐군 | 2003.01.09 |
메인 접속하면 할아버지 나오는... [6] | 공유 | 2003.08.28 |
[초간단 자바스크립트!] 자바로 플래시 따라하기! [3] | ∑Ztxy | 2003.10.15 |