<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ks_c_5601-1987">
<title>새 페이지 1</title>
</head>
<body>
<style>
.btn{width:40}
.btn2{width:88}
</style>
<script>
flag=0;//연산식이 입력되는 횟수플래그
str=0;//입력수치
opr="";//연산자
old=new Array();
//초기화
function init(){
document.f.t.value=""; //입력필드 소거
str=0;flag=0;opr=""; //입력수치, 횟수플래그, 연산자
}
//숫자를 입력받아 추가한다
function putn(v){
if(flag==0){ //연산식이 입력되기 전에는
document.f.t.value+=v; //텍스트 필드를 추가한다.
}else {
if(flag==1){ //연산식이 처음 입력되면,
document.f.t.value=v; //입력필드를 갱신
} else {
document.f.t.value+=v; //텍스트 필드를 추가한다.
}
flag++; //플래그 증가
}
}
//연산식이 입력되면, 기존 스트링과, 연산자를 변수에 담고, 플래그를 증가한다.
function cal(h){
old[old.length]=str;
str=document.f.t.value;
odr=opr;
opr=h;
flag=1;
if(h=='%'){
document.f.t.value=eval(old[old.length-1]*str/100);
} else {
if(odr=='%'){
document.f.t.value=eval(old[old.length-2]+opr+str);
}
}
}
function cal2(h){ //제곱,제곱근,로그등의 특수연산을 담당하는 함수
str=document.f.t.value;
switch(h){
case "pow":
document.f.t.value=eval("Math."+h+"(str,2)");
break;
case "sqrt":
document.f.t.value=eval("Math."+h+"(str,2)");
break;
case "log":
document.f.t.value=eval("Math."+h+"(str)");
break;
}
}
//= 이 입력되면,
function sol(){
//텍스트 필드에, 기존입력수치와 신규입력값의 연산결과를 출력한다.
document.f.t.value=eval(str+opr+document.f.t.value);
}
//Clear등의 처리
function adj(a){
switch(a){ //매개변수에 따라 처리
case 0: //clear이면,
init(); //초기화
break;
}
}
</script>
<body onload=init();>
<table border=6 width="247"><tr>
<form name=f>
<td colspan=5>
<input type=text name=t value="" size=30>
</td></tr>
<tr><td width="40">
<input type=button onclick=putn(7) value=7 class=btn></td><td width="40">
<input type=button onclick=putn(8) value=8 class=btn></td><td width="41">
<input type=button onclick=putn(9) value=9 class=btn></td><td width="40">
<input type=button onclick=cal("/") value="/" class=btn></td><td width="40">
<input type=button onclick=cal2("pow") value="pow" class=btn></td></tr>
<tr><Td width="40" height="25">
<input type=button onclick=putn(4) value=4 class=btn></td>
<td width="40" height="25">
<input type=button onclick=putn(5) value=5 class=btn></td>
<td width="41" height="25">
<input type=button onclick=putn(6) value=6 class=btn></td>
<td width="40" height="25">
<input type=button onclick=cal("*") value="*" class=btn></td>
<td width="40" height="25">
<input type=button onclick=cal2("sqrt") value="sqrt" class=btn></td></tr>
<tr><td width="40">
<input type=button onclick=putn(1) value=1 class=btn></td><td width="40">
<input type=button onclick=putn(2) value=2 class=btn></td><td width="41">
<input type=button onclick=putn(3) value=3 class=btn></td><td width="40">
<input type=button onclick=cal("-") value="-" class=btn></td><td width="40">
<input type=button onclick=cal2("log") value="log" class=btn></td></tr>
<tr><td width="40">
<input type=button onclick=putn(0) value=0 class=btn></td><td width="40">
<input type=button onclick=putn('.') value=. class=btn></td><td width="41">
<input type=button onclick=cal("+") value="+" class=btn></td><td width="40">
<input type=button onclick=cal("%") value="%" class=btn></td>
</td></tr>
<tr>
<td colspan=2>
<input type=button onclick=adj(0) value="지우기" class=btn2></td><td colspan=3>
<input type=button onclick=sol() value="계산하기" class=btn2></td>
</form>
</table>
</body>
</html>