묻고답하기
page_full_width" class="col-xs-12" |cond="$__Context->page_full_width">
계산기 소스입니다. 계산을 3번이상 한뒤에 =을 눌러야 답이 나옵니다.
2004.06.07 20:11
소스는 아래와 같습니다.
<html>
<head>
<style>
input {width:40}
</style>
<script>
var buffer = 0;
var new_value = 1;
var m;
var start = 1;
function back() {
var str = new String(form1.text1.value);
var leng = str.length;
if((form1.text1.value < 0) && (leng == 2)) {
str = 0;
}
else {
str = str.substr(0,leng-1);
if(str == "") str = 0;
}
form1.text1.value = str;
}
function ce() {
form1.text1.value = 0;
}
function c() {
form1.text1.value = 0;
this.buffer = 0;
this.m = "";
this.start = 1;
}
function sqrt() {
form1.text1.value = Math.sqrt(form1.text1.value);
}
function sub() {
form1.text1.value = 1 / parseInt(form1.text1.value);
}
function equal() {
math(this.m);
form1.text1.value = this.buffer;
}
function plus(i) {
if((form1.text1.value == "")||(form1.text1.value == 0)||(this.new_value == 1)) {
form1.text1.value = i;
this.new_value = 0;
}
else {
form1.text1.value = form1.text1.value + new String(i);
}
}
function toggle() {
form1.text1.value = parseInt(form1.text1.value) * -1;
}
function math(s) {
if(start) {
this.buffer = parseInt(form1.text1.value);
this.start = 0;
this.new_value = 1;
return;
}
if(s == "+") {
this.buffer = this.buffer + parseInt(form1.text1.value);
}
if(s == "*") {
this.buffer = this.buffer * parseInt(form1.text1.value);
}
if(s == "/") {
this.buffer = this.buffer / parseInt(form1.text1.value);
}
if(s == "-") {
this.buffer = this.buffer - parseInt(form1.text1.value);
}
form1.text1.value = this.buffer;
this.new_value = 1;
this.m = s
}
function percent() {
number = parseInt(form1.text1.value);
number = this.buffer / 100 * number;
form1.text1.value = number;
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="text1" value="0" style="width:220"><br>
<input type="button" value="BACK" onclick="back()"> <input type="button" value="C E" onclick="ce()" style="width:85"> <input type="button" value="C" onclick="c()" style="width:85"><br>
<input type="button" value="sqrt" onclick="sqrt()"> <input type="button" value="7" onclick="plus(7)"> <input type="button" value="8" onclick="plus(8)"> <input type="button" value="9" onclick="plus(9)"> <input type="button" value="/" onclick="math('/')"><br>
<input type="button" value="%" onclick="percent()"> <input type="button" value="4" onclick="plus(4)"> <input type="button" value="5" onclick="plus(5)"> <input type="button" value="6" onclick="plus(6)"> <input type="button" value="*" onclick="math('*')"><br>
<input type="button" value="1/x" onclick="sub()"> <input type="button" value="1" onclick="plus(1)"> <input type="button" value="2" onclick="plus(2)"> <input type="button" value="3" onclick="plus(3)"> <input type="button" value="-" onclick="math('-')"><br>
<input type="button" value="=" onclick="equal()"> <input type="button" value="0" onclick="plus(0)"> <input type="button" value="+-" onclick="toggle()"> <input type="button" value="." onclick="plus('.')"> <input type="button" value="+" onclick="math('+')">
</form>
</body>
</html>
여기서 +.-,*,/ 계산을 3번 이상 하고 =을 눌러야 답이 나오고 그 전에 =을 누르면 이전 값이 출력됩니다.
어디가 잘못되었는지 알려주세요.
<html>
<head>
<style>
input {width:40}
</style>
<script>
var buffer = 0;
var new_value = 1;
var m;
var start = 1;
function back() {
var str = new String(form1.text1.value);
var leng = str.length;
if((form1.text1.value < 0) && (leng == 2)) {
str = 0;
}
else {
str = str.substr(0,leng-1);
if(str == "") str = 0;
}
form1.text1.value = str;
}
function ce() {
form1.text1.value = 0;
}
function c() {
form1.text1.value = 0;
this.buffer = 0;
this.m = "";
this.start = 1;
}
function sqrt() {
form1.text1.value = Math.sqrt(form1.text1.value);
}
function sub() {
form1.text1.value = 1 / parseInt(form1.text1.value);
}
function equal() {
math(this.m);
form1.text1.value = this.buffer;
}
function plus(i) {
if((form1.text1.value == "")||(form1.text1.value == 0)||(this.new_value == 1)) {
form1.text1.value = i;
this.new_value = 0;
}
else {
form1.text1.value = form1.text1.value + new String(i);
}
}
function toggle() {
form1.text1.value = parseInt(form1.text1.value) * -1;
}
function math(s) {
if(start) {
this.buffer = parseInt(form1.text1.value);
this.start = 0;
this.new_value = 1;
return;
}
if(s == "+") {
this.buffer = this.buffer + parseInt(form1.text1.value);
}
if(s == "*") {
this.buffer = this.buffer * parseInt(form1.text1.value);
}
if(s == "/") {
this.buffer = this.buffer / parseInt(form1.text1.value);
}
if(s == "-") {
this.buffer = this.buffer - parseInt(form1.text1.value);
}
form1.text1.value = this.buffer;
this.new_value = 1;
this.m = s
}
function percent() {
number = parseInt(form1.text1.value);
number = this.buffer / 100 * number;
form1.text1.value = number;
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="text1" value="0" style="width:220"><br>
<input type="button" value="BACK" onclick="back()"> <input type="button" value="C E" onclick="ce()" style="width:85"> <input type="button" value="C" onclick="c()" style="width:85"><br>
<input type="button" value="sqrt" onclick="sqrt()"> <input type="button" value="7" onclick="plus(7)"> <input type="button" value="8" onclick="plus(8)"> <input type="button" value="9" onclick="plus(9)"> <input type="button" value="/" onclick="math('/')"><br>
<input type="button" value="%" onclick="percent()"> <input type="button" value="4" onclick="plus(4)"> <input type="button" value="5" onclick="plus(5)"> <input type="button" value="6" onclick="plus(6)"> <input type="button" value="*" onclick="math('*')"><br>
<input type="button" value="1/x" onclick="sub()"> <input type="button" value="1" onclick="plus(1)"> <input type="button" value="2" onclick="plus(2)"> <input type="button" value="3" onclick="plus(3)"> <input type="button" value="-" onclick="math('-')"><br>
<input type="button" value="=" onclick="equal()"> <input type="button" value="0" onclick="plus(0)"> <input type="button" value="+-" onclick="toggle()"> <input type="button" value="." onclick="plus('.')"> <input type="button" value="+" onclick="math('+')">
</form>
</body>
</html>
여기서 +.-,*,/ 계산을 3번 이상 하고 =을 눌러야 답이 나오고 그 전에 =을 누르면 이전 값이 출력됩니다.
어디가 잘못되었는지 알려주세요.