简单的计算器代码
作为初学者能写出一个简单的程序再开心不过了。下面小编给大家介绍一个简单的计算器代码程序。
#box{width: 295px; margin: 0 auto; text-align: justify; border: 1px solid #ddd; padding: 15px;}
.d_num{display: inline-block; margin: 10px; width: 33px; height: 30px; border: 1px solid #ddd; text-align: center; line-height: 30px; cursor: pointer;}
.sum{margin: 0 10px 10px; line-height: 30px; font-size: 20px;}
#sum{width: 205px; height: 30px; padding: 0 5px; margin-left: 5px; font-size: 20px;}
#process{width: 255px; height: 30px; padding: 0 5px; margin-left: 5px; font-size: 20px;}
SUM:
123<-C
456*/
789+-
000.%=
javascript">
var $box = document.getElementById('box');
var $sum = document.getElementById('sum');
var $process = document.getElementById('process');
var sum = 0, process = '', num1 = '', num2 = '', temp = '', statu = false;
$box.onclick = function(e){
var event = window.event || e;
var ele = event.srcElement || event.target;
var _className = ele.className;
if(_className == 'd_num'){
var num = ele.getAttribute('data-num');//点击的.按钮对应的值
var NotNum = isNaN(num);
if(!NotNum || num == '.'){ //点击了数字
if(!statu){ //还没点击过符号
if(num1 == '0'){
num1 = '';
}
num1 += num;
process = num1;
}else{ //已经点击过符号
if(num2 == '0'){
num2 = '';
}
num2 += num;
process = num1 + temp + num2;
}
$process.value = process;
}
else{ //点击了符号
if(num1 == ''){//非法操作
return false;
}
if(num == 'C'){//归零
num1 = '';
num2 = '';
process = '';
temp = '';
sum = '';
$process.value = '0';
$sum.value = '0';