好久没有更新了。
通过原生js实现计算机
这是html文档
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>calculator</title>
<link rel="stylesheet" type="text/css" href="calc.css"/>
<script src="index.js"></script>
</head>
<body>
<div id="a">
<div id="b">
<input type="text" name="" id="answer" readonly="readonly"/><br>
<input type="text" name="" id="answer2" readonly="readonly"/>
</div>
<div id="c">
<input type="button" name="" id="fs1" value="C" onclick="clean()"/>
<input type="button" name="" id="fs2" value="<" onclick="del()"/>
<input type="button" name="" id="fs8" value="+/-" onclick="jj()"/>
<input type="button" name="" id="fs6" value="÷" onclick="chu()"/>
</div>
<div id="d">
<input type="button" value="7" id="bs1" onclick="press(7)">
<input type="button" value="8" id="bs2" onclick="press(8)">
<input type="button" value="9" id="bs3" onclick="press(9)">
<input type="button" name="" id="fs5" value="x" onclick="cheng()"/>
</div>
<div id="e">
<input type="button" name="" id="bs4" value="4" onclick="press(4)"/>
<input type="button" name="" id="bs5" value="5" onclick="press(5)"/>
<input type="button" name="" id="bs6" value="6" onclick="press(6)"/>
<input type="button" name="" id="fs4" value="-" onclick="jian()"/>
</div>
<div id="f">
<input type="button" name="" id="bs7" value="1" onclick="press(1)"/>
<input type="button" name="" id="bs8" value="2" onclick="press(2)"/>
<input type="button" name="" id="bs9" value="3" onclick="press(3)"/>
<input type="button" name="" id="fs3" value="+" onclick="jia()"/>
</div>
<div id="g">
<input type="button" name="" id="fs7" value="%" onclick="pressbf()"/>
<input type="button" name="" id="bs10" value="0" onclick="presszero()"/>
<input type="button" name="" id="fs9" value="." onclick="pressdian()"/>
<input type="button" name="" id="fs10" value="=" onclick="dy()"/>
</div>
</div>
</body>
</html>
这是样式文档
body{
margin: 0;
padding: 0;
}
#a{
width: 200px;
background-color: #000;
margin: 20px auto;
text-align: center;
border: #000 1px solid;
color: #fff;
}
#b #answer,#answer2{
margin-top: 10px;
font-size: 20px;
background: transparent;
height: 30px;
width: 190px;
border: #000 solid 1px;
outline: none;
color: #fff;
}
#c input{
padding: 10px;
margin: 3px;
width: 40px;
height: 40px;
outline: none;
}
#d input{
padding: 10px;
margin: 3px;
width: 40px;
height: 40px;
outline: none;
}
#e input{
padding: 10px;
margin: 3px;
width: 40px;
height: 40px;
outline: none;
}
#f input{
padding: 10px;
margin: 3px;
width: 40px;
height: 40px;
outline: none;
}
#g input{
padding: 10px;
margin: 3px;
width: 40px;
height: 40px;
margin-bottom: 5px;
outline: none;
}
#bs1,#bs2,#bs3,#bs4,#bs5,#bs6,#bs7,#bs8,#bs9,#bs10{
font-size: 14px;
font-weight: bold;
background: #333;
color: #fff;
border: #000 solid 1px;
border-radius: 90px;
}
#fs1,#fs2,#fs7,#fs8{
font-size: 16px;
color: #000;
font-weight: bold;
background: #ccc;
border: #ccc solid 1px;
border-radius: 90px;
}
#fs3,#fs4,#fs5,#fs6,#fs10{
color: #fff;
font-size: 16px;
font-weight: bold;
background: orange;
border: orange solid 1px;
border-radius: 90px;
}
#fs9{
color: #fff;
font-size: 16px;
font-weight: bold;
background: #333;
border: #000 solid 1px;
border-radius: 90px;
}
这里是js文档
// str 存储的为运算表达式 answer显示的为运算式或者点击等号后的最终结果 answer2为实时运算结果
function dy() {
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/") {
str=str.substring(0,str.length-1);
answer2.value=Number(eval(str));
str=answer2.value;
answer.value = str;
answer2.value="";
}
else{
str=answer2.value;
answer.value = str;
answer2.value="";
}
// 函数为点击等号 第一行显示运算结果第二行清空
}
function clean() {
// 点击c清空存储用字符串以及第一第二行显示内容
str="";
answer.value=str;
answer2.value = str;
}
function jj(){
if(answer2.value!=""){
if(answer2.value>0)
answer2.value="-"+eval(str);
else{
str=answer2.value;
str=str.substr(1,str.length-1);
answer2.value=str;
}
}
}
var str="";
// 用于存放要计算的表达式
function chu() {
if (str === "") {
answer2.value ="";
} else {
// 使用截取表达式最后一位如果为运算符那么再次点击运算符时用新的运算符将其替换
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/") {
str = str.substr(0, str.length - 1) + "/";
answer.value = str;
} else {
// 否则正常输入
str = str + "/"
answer.value = str;
}
}
}
function cheng() {
// 与同理chu
if (str === "") {
answer2.value = "";
} else {
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/") {
str = str.substr(0, str.length - 1) + "*";
answer.value = str;
} else {
str = str + "*"
answer.value = str;
}
}
}
function jia() {
// 第一位是+-时有意义 允许输入
if (str === "") {
str += "+";
answer.value = str;
} else {
// 与同理chu
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/") {
str = str.substr(0, str.length - 1) + "+";
answer.value = str;
} else {
str = str + "+"
answer.value = str;
}
}
}
function jian() {
// 与jia同理
if (str === "") {
str += "-";
answer.value = str;
} else {
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/") {
str = str.substr(0, str.length - 1) + "-";
answer.value = str;
} else {
str = str + "-"
answer.value = str;
}
}
}
var str1;
function del() {
// 点击删除键时删除表达式最后一位刷线显示内容
str = str.substring(0,str.length-1);
answer.value = str;
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/") {
str1=str.substring(0,str.length-1);
answer2.value=Number(eval(str1));
}
else
answer2.value=Number(eval(str));
}
function press(y) {
// 点击数字在表达上添加数字 刷新显示内容
str += y;
answer.value = str;
// 下面为判断表达式里面有没有运算符如果有点击数字时(也就是改变表达式时)实时在第二行显示运算结果 当字符串中没有运算符号时第二行不进行操作
//if (str.indexOf("+") !== -1 || str.indexOf("-") !== -1 || str.indexOf("*") !== -1 || str.indexOf("/") !== -1) {
answer2.value = Number(eval(str));
//}
}
var e="",f="",g="",h="",j="",k="";
// 用于存储下方运算符位置
function pressdian() {
if(str==""){
// 当表达式为空时用户点击“点” 那么帮用户添加一个0 形成"0."的效果
str += "0"+".";
answer.value = str;
} else {
// 表达式不为空时则需要进行判断
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/") {
//当表达式最后一位是运算符时那么 用户点击“点” 那么帮用户添加一个0 形成"0."的效果
str += "0" + ".";
answer.value = str;
}else{
if (str.substr(str.length - 1, 1)=="."){
// 当最后一位是点时不允许再输入一个点此时无操作
}else{
h = str.lastIndexOf("+");
e = str.lastIndexOf("-");
f = str.lastIndexOf("*");
g = str.lastIndexOf("/");
j = Math.max(h, e, f, g);
// 搜索表达式返回各个运算符号的位置取最大的一个也就是最右边的一个
k = str.lastIndexOf(".");
// 搜索上一个点的位置
if (k=="-1"){
// 此时整个表达式没有出现点 允许输入点
str += ".";
answer.value = str;
} else {
if(k>j){
}else{
str += ".";
answer.value = str;
}
}
}
}
}
}
function presszero() {
if(str===""){
answer.value = 0;
// 为用户显示一个0
}else{
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/")
{
answer.value = str+"0";
// 仅显示不存储(不修改str)
}else{
// 允许正常输入
str += 0;
answer.value =str;
answer2.value = Number(eval(str));
}
}
}
// ----------------------------------------------------------------------------
var a = "", b = "", c = "", d = "", i = "";
function pressbf() {
// (1) 如果表达式为数字 那么表达式除100并记录
if (!isNaN(str)) {
str = ""+str / "100";
answer.value = str;
answer2.value = str;
}
else {
// (2)如果表达式最后一位是运算符这时是没有意义的我们不进行任何操作
if (str.substr(str.length - 1, 1) == "+" || str.substr(str.length - 1, 1) == "*" || str.substr(str.length - 1, 1) == "-" || str.substr(str.length - 1, 1) == "/") {
} else {
// (3)如果接下来实现第二个功能99+6*999变为 99+6*9.99 我们需要知道最后一个运算符的位置才可以继续
a = str.lastIndexOf("+");
b = str.lastIndexOf("-");
c = str.lastIndexOf("*");
d = str.lastIndexOf("/");
i = Math.max(a, b, c, d);
// 取最大值即为最右面的运算符位置
str = str.substr(0, i + 1) + (str.substr(i + 1, str.length - i) / 100);
// 截取表达式第一位到运算符那一位连接上"运算符后面的数字/100" 存入新表达式
answer.value = str;
// 显示结果
answer2.value = eval(str);
// 功能成功实现
}
}
}
运行结果: