<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网页计算器</title>
<style type="text/css">
* {font-family:微软雅黑; font-size:14px; }
#Counter{width:250px;height:322px; margin:100px auto; border:2px solid #5e799a; background:url(images/Box1_bg.gif) repeat-x; }
#writeBox{ height:54px; width:222px; border:1px solid #8e9cad; margin:10px auto; background-color:White; background:url(images/Box2_bg.gif) repeat-x;}
.writeBox-txtBox1{ margin-left:2px; text-align:right; line-height:18px; font-size:15px; border:0; background-color:transparent;}
.writeBox-txtBox2{ margin-left:2px; text-align:right; line-height:28px; font-size:25px; font-weight:bold; border:0; background-color:transparent;}
#Counter p{ margin:0px auto; width:222px; height:40px; line-height:40px;}
#Counter input{ width:41px; height:35px;}
#btnBox{width:222px; margin:0px auto;}
#btnBox-l{ float:left; width:178px;}
#btnBox-r{ float:right; width:42px; text-align:left;}
#btnBox p{ width:178px; margin:0px;}
</style>
<script type="text/javascript">
var txtResult; //输出的结果
var txtCourse; //输出的过程
var Result=0; //结果
var falg=false;
var operationJoin="no";
//清除文本框
function clearTxt() {
document.count.txtCourse.value = "";
document.count.txtResult.value = "0";
//重置对象
Result = 0;
falg = false;
operationJoin = "no";
}
//CE按钮,清除结果文本框
function clearCE() {
document.count.txtResult.value = "0";
}
//数字键
function inputkey(key) {
var index = key;
if (falg == true) { //已经有运算结果
document.count.txtResult.value = key;
falg = false;
}
else {
txtResult = document.count.txtResult.value;
if (txtResult == null || txtResult == "0" || txtResult == "") {
document.count.txtResult.value = key;
}
else {
if (txtResult.length > 13) {
//最多输入14个字符
return;s
}
else {
document.count.txtResult.value = txtResult + key;
}
}
}
}
//小数点
function dots() {
if (falg) { //已经有运算结果
document.count.txtResult.value = "0.";
falg = false;
}
else {
txtResult = document.count.txtResult.value;
var indexDot = txtResult.indexOf('.');
if (indexDot < 1) {
document.count.txtResult.value = txtResult + document.count.btn_NumberDot.value;
}
}
}
//退格键
function Backspace() {
txtResult = document.count.txtResult.value;
if (txtResult.length > 1) {
document.count.txtResult.value = txtResult.substring(0, txtResult.length - 1);
}
else {
document.count.txtResult.value = "0";
}
}
//正负号
function plusMinus() {
txtResult = document.count.txtResult.value;
txtCourse=document.count.txtCourse.value;
if (txtResult != 0) {
if (falg) { //已经有运算结果
//显示过程
if (txtCourse.length > 0) {
document.count.txtCourse.value = "nengate(" + txtCourse + ")";
}
else {
document.count.txtCourse.value = "nengate(" + Result + ")";
}
//显示输出
if (document.count.txtResult.value.substring(0, 1) == "-") {
document.count.txtResult.value = txtResult.substring(1, txtResult.length);
}
else {
document.count.txtResult.value = "-" + txtResult;
}
}
else { //没有有运算结果
if (document.count.txtResult.value.substring(0, 1) == "-") {
document.count.txtResult.value = txtResult.substring(1, txtResult.length);
}
else {
document.count.txtResult.value = "-" + txtResult;
}
}
}
}
//求倒数
function Reciproc() {
txtResult = document.count.txtResult.value;
txtCourse = document.count.txtCourse.value;
if (txtResult != 0) {
if (falg == true && operationJoin != "no") { //已经有运算结果
var temp = 1 / parseFloat(txtResult);
Count(operationJoin, temp); //计算结果,写入结果
document.count.txtCourse.value = Result + operationJoin + "reciproc(" + txtResult + ")"; //写入过程
}
else { //没有运算过程
document.count.txtCourse.value = "reciproc(" + txtResult + ")"; //写入过程
document.count.txtResult.value = 1 / parseFloat(txtResult); //计算结果,写入结果
}
}
}
//开平方
function sqrt() {
txtResult = document.count.txtResult.value;
document.count.txtCourse.value = "sqrt(" + txtResult + ")"; //写入过程
document.count.txtResult.value = Math.sqrt(parseFloat(txtResult)); //计算结果,写入结果
}
//运算符
function operation(join) {
falg = true;
txtResult = document.count.txtResult.value;
txtCourse = document.count.txtCourse.value;
//截取字符串
if (txtCourse.length > 0) {
if (txtResult != "0") {
txtCourse = txtCourse + txtResult + join;
}
else {
txtCourse = txtCourse.substring(0, txtCourse.length - 1) + join;
}
}
else {
txtCourse = txtResult + join;
}
//运算结果
Count(operationJoin, txtResult);
//写入过程
if (txtCourse.length > 24) {
document.count.txtCourse.value = ">> " + txtCourse.substring(txtCourse.length - 22);
}
else {
document.count.txtCourse.value = txtCourse;
}
operationJoin = join;
}
//运算结果
function Count(operationJoin,txtResult) {
switch (operationJoin) {
case "no":
if (document.count.txtResult.value != "0") {
Result = parseFloat(document.count.txtResult.value);
}
else {
return;
}
break;
case "+":
var jg = parseFloat(Result) + parseFloat(txtResult);
document.count.txtResult.value = jg; //显示结果
Result = jg; //保存结果
break;
case "-":
var jg = parseFloat(Result) - parseFloat(txtResult);
document.count.txtResult.value = jg; //显示结果
Result = jg; //保存结果
break;
case "*":
var jg = parseFloat(Result) * parseFloat(txtResult);
document.count.txtResult.value = jg; //显示结果