Mini Calculator(JavaScript)

原创 2007年09月23日 16:19:00
<html>
    
<head>
        
<title>Calculator</title>
        
<script language="javascript">
            
            
//数字验证
            function ValidateIsNum(textbox)
            
{
                
//如果为空,则默认为0
                if(textbox.value == "")
                
{
                    textbox.value 
= 0;
                    
return true;
                }

                
//输入为非数字则清空并提示
                if(isNaN(textbox.value))
                
{
                    textbox.value 
= "";
                    alert(
"请输入数字");
                    textbox.focus();
                    
return false;
                }

                
return true;
            }

            
            
//改变提示
            function ChangeTitle(radio)
            
{
                
var title1 = document.getElementById("tdTitle1");
                
var title2 = document.getElementById("tdTitle2");
                
if(radio.id =="plus")
                
{                    
                    title1.innerText 
= "请输入加数1:";
                    title2.innerText 
= "请输入加数2:"
                }

                
if(radio.id == "minus")
                
{                    
                    title1.innerText 
= "请输入被减数:";
                    title2.innerText 
= "请输入减数:"
                }

                
if(radio.id == "multiply")
                
{                    
                    title1.innerText 
= "请输入被乘数:";
                    title2.innerText 
= "请输入乘数:"
                }

                
if(radio.id == "divide")
                
{
                    title1.innerText 
= "请输入被除数:";
                    title2.innerText 
= "请输入除数:"
                }

                
//有输入框为空,则Focus,不为空则计算
                var text1 = document.getElementById("input1");
                
var text2 = document.getElementById("input2");
                
if(text1.value == "")
                    text1.focus();
                
else if(text2.valuse == "")
                    text2.focus();
                    
else
                        Calc(text1,text2);
            }

            
            
function Calc(textbox1,textbox2)
            
{
                
//验证输入是否合法
                if(!ValidateIsNum(textbox1) || !ValidateIsNum(textbox2))
                
{
                    
return;
                }

                
var operand1 = parseFloat(textbox1.value);
                
var operand2 = parseFloat(textbox2.value);
                
var result = 0;
                
if(document.getElementById("plus").checked == true)
                
{
                    result 
= operand1 + operand2;
                }

                
if(document.getElementById("minus").checked == true)
                
{
                    result 
= operand1 - operand2;
                }

                
if(document.getElementById("multiply").checked == true)
                
{
                    result 
= operand1 * operand2;
                }

                
if(document.getElementById("divide").checked == true)
                
{
                    
if(operand2 != 0)
                    
{
                        result 
= operand1 / operand2;
                    }

                    
else
                    
{
                        alert(
"被除数不可为零");
                        textbox2.value 
= "";
                        textbox2.focus();
                    }

                }

                
//输出
                document.getElementById("inputResult").value = result;
            }

            
            
//当focus时如果内容为0,则清空
            function ClearWhenFocus(textbox)
            
{
                
if(textbox.value == 0)
                
{
                    textbox.value 
= "";
                }

            }

        
</script>
    
</head>
    
<body>
        
<table border="2">
            
<tr>
                
<td>请选择运算符:</td>
                
<td>
                    
<input type="radio" checked="true" id="plus" name="operator" onclick="ChangeTitle(this)"></input>+
                    
<input type="radio" id="minus" name="operator"  onclick="ChangeTitle(this)"></input>-
                    
<input type="radio" id="multiply" name="operator"  onclick="ChangeTitle(this)"></input>*
                    
<input type="radio" id="divide" name="operator"  onclick="ChangeTitle(this)"></input>/
                
</td>
            
</tr>
            
<tr>
                
<td id="tdTitle1">请输入加数1</td>
                
<td><input type="text" id="input1" style="display:block" onkeyup="Calc(this,input2)" onfocus="ClearWhenFocus(this)"></input></td>
            
</tr>
            
<tr>
                
<td id="tdTitle2">请输入加数2</td>
                
<td><input type="text" id="input2" style="display:block" onkeyup="Calc(input1,this)" onfocus="ClearWhenFocus(this)"></input></td>
            
</tr>
            
<tr>
                
<td>计算结果为:</td>
                
<td><input type="text" id="inputResult" style="display:block"></input></td>
            
</tr>
        
</table>
    
</body>
</html>
 

相关文章推荐

JavaScript权威指南 Loan Calculator示例说明

    今天开始学习JavaScript权威指南的第一章节,首先学会里JavaScript的代码如何嵌套在HTML代码中,哈哈哈哈。。白痴都知道。。我是新手哈。。然后手打了一次Table of Fac...

LeetCode Basic Calculator(用栈计算表达式的值)

题意:给出一个计算表达式,只包含 +,-,(,),求计算结果 思路:用栈来实现  代码如下: public class Solution { private int cal(int nu...

224-m-Basic Calculator

有括号的运算,只有加减运算。很明显本题只考察括号匹配没有再加入乘除,否则就真是半个计算器的工程了。由于我是先写了计算器二式,所以本题也想用一次扫描就出结果。但由于有括号要考虑出栈,要考虑单个括号括的数...

[Code] 《JS权威指南》示例程序 -- Loan Calculator

都说前端最重要的技能要看JS 开始看犀牛书 照着开头的示例程序编写了一下 工具: Editplus 浏览器: 百度浏览器 抄写完程序以后,运行提示有脚本错误,如下:   对抄写!!!...

calculator.java

package calculator.test; import android.app.Activity; import android.os.Bundle; import android.view...
  • asicer
  • asicer
  • 2012年02月23日 13:29
  • 180

hdu2424Gary's Calculator

Gary's Calculator Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others...

FOJ Playing with Calculator (模运算)

Problem Description Pete is playing a game. He types on a calculator a natural number K and then ...

LightOJ 1013 - Love Calculator(DP)

题意:给你两个字符串str1,str2,要求求以这两个字符串为不下降子序列的字符串det最小长度,并且求出该字符串det的组合方式有多少种。 这个算是一道简单的DP题。第一个问题的关键是求两个字符串s...

湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator

Interesting Calculator Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 163  Solved: 49 Descri...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Mini Calculator(JavaScript)
举报原因:
原因补充:

(最多只允许输入30个字)