웹마스터 팁
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
이것이 젝아 만둔 예제 입니다..
제목 | 글쓴이 | 날짜 |
---|---|---|
1 분마다 배경이 빠뀌는 소스...입니다. [1] | q333 | 2003.01.12 |
IE6SP1 에서 작동하는 크롬리스윈도우!![ByKlein_Chromeless_Window1.0] [13] | 술도짱 | 2003.01.10 |
[1분짜리 팁!] 홈페이지 입장 여부 묻는 폼 띄우기! [2] | 찐군 | 2003.01.09 |
자바스크립트용 계산기 v1.0 [4] | 찐군 | 2003.01.09 |
링크가 걸려있는 모든 이미지에 마우스 올리면 서서히 밝아지게 하는 소스입니다. [3] | 정해식 | 2003.01.09 |
혹시 이런것도 될까-_-;;. 시노부 플레이어에서 랜덤 모드 사용자가 택하게 하기 | TuTy | 2003.01.09 |
홈페이지 패스워드(암호) 걸기 소스 [6] | keymove | 2003.01.09 |
select form 으로 새 창 띄우기 [4] | 당근당근 | 2003.01.07 |
[re] select form 으로 새 창 띄우기(간단버전)
[3]
![]() | RedEye | 2003.01.27 |
갤러리스킨에 쓰면 좋을 것 같은 미리보기(수정2) [10] | 행복한고니 | 2003.01.06 |
크롬리스에 응용할만한 소스;; [3] | MYMob.INT. | 2003.01.05 |
숫자를 한글로 변환하는 함수 [1] | 행복한고니 | 2003.01.03 |
mid 랜덤으로 듣기 ... [2] | 아벨라 | 2003.01.02 |
색다른 링크법(새창) [2] | BIRDY™ | 2002.12.30 |
텍스트 폼 / 전체선택 하기 자바스크립 | mnemosyne | 2002.12.24 |
div, Javascript 이용해서 섹션 테이블 만들기 [3] | mnemosyne | 2002.12.18 |
크롬리스 완벽해결! IE.2.0부터 Netscape까지 작동가능!! [13] | 김민호 | 2002.12.16 |
오른쪽 클릭 메뉴 [6] | 디아릭스 | 2002.12.16 |
select form 으로 새창띄우기(수정:2002-12-19) [3] | dolufy | 2002.12.11 |
홈페이지에 머무른 시간을 알림창으로 나타내기. [3] | 시즌오브드림 | 2002.12.10 |