< html>
< head>
< title> calculate title>
< style type = " text/css" >
.bg {
width : 30%;
height : 450px;
background : rgb ( 168,168,168) ;
margin-left : 35%;
margin-right : 35%;
}
.uup {
width : 100%;
height : 9%;
text-align : "center" ;
}
.up {
width : 88%;
height : 12%;
border : solid 1px black;
margin : 3% 6% 1.5% 6%;
}
.down {
width : 88%;
height : 70%;
border : solid 1px black;
margin : 1.5% 6% 3% 6%;
}
.logo {
width : 36%;
height : 100%;
margin-top : 0px;
margin-left : 0px;
padding-left : 30px;
background-color : white;
font-size : 28px;
}
.show {
width : 100%;
height : 100%;
}
.numtable {
width : 90%;
height : 90%;
margin : 5%;
}
.numtable tr td {
width : 25%;
height : 20%;
}
.numtable input {
width : 80%;
height : 80%;
margin : 10%;
}
style>
< script type = " text/javascript" >
window. onload= initZero;
var str = new Array ( ) ;
var numStr = new Array ( ) ;
var charStr = new Array ( ) ;
var flag = new Array ( ) ;
var buttonId = new Array ( "reset" , "plus" , "minus" , "del" , "num7" , "num8" , "num9" , "multiply" , "num4" , "num5" , "num6" , "divide" , "num1" , "num2" , "num3" , "point" , "leftbracket" , "num0" , "rightbracket" , "equal" ) ;
function $ ( id) {
return document. getElementById ( id) ;
}
function initZero ( ) {
$ ( "inputframe" ) . value= 0 ;
initCE ( ) ;
addListener ( ) ;
}
function initCE ( ) {
while ( str. length!= 0 ) {
str. pop ( ) ;
}
while ( numStr. length!= 0 ) {
numStr. shift ( ) ;
}
while ( charStr. length!= 0 ) {
charStr. shift ( ) ;
}
while ( flag. length!= 0 ) {
flag. shift ( ) ;
}
charStr[ 0 ] = "0" ;
flag[ 0 ] = false ;
}
function addListener ( ) {
for ( var i= 0 ; i< buttonId. length; i++ ) {
$ ( buttonId[ i] ) . addEventListener ( "click" , function ( ) {
if ( this . id == "equal" ) {
var result = getResult ( ) ;
if ( str. length== 0 ) {
$ ( "inputframe" ) . value = 0 ;
} else {
$ ( "inputframe" ) . value = showStr ( ) + "=" + result;
initCE ( ) ;
}
} else if ( this . id == "reset" ) {
initCE ( ) ;
$ ( "inputframe" ) . value = "0" ;
} else if ( this . id == "del" ) {
str. pop ( ) ;
$ ( "inputframe" ) . value = showStr ( ) ;
} else {
str. push ( this . value) ;
$ ( "inputframe" ) . value = showStr ( ) ;
}
} ) ;
}
}
function mul ( num1, num2) {
var pointNum1 = 0 ;
var pointNum2 = 0 ;
var numStr1 = num1. toString ( ) ;
var numStr2 = num2. toString ( ) ;
if ( numStr1. indexOf ( "." , 0 ) != - 1 ) {
pointNum1 = numStr1. length- numStr1. indexOf ( "." , 0 ) - 1 ;
num1 = Number ( numStr1. replace ( "." , "" ) ) ;
}
if ( numStr2. indexOf ( "." , 0 ) != - 1 ) {
pointNum2 = numStr2. length- numStr2. indexOf ( "." , 0 ) - 1 ;
num2 = Number ( numStr2. replace ( "." , "" ) ) ;
}
var pointNum = pointNum1+ pointNum2;
var resStr = ( num1* num2) . toString ( ) ;
if ( pointNum!= 0 ) {
if ( resStr. length> pointNum) {
var num = resStr. length- pointNum;
var sstr = resStr. split ( "" ) ;
resStr= "" ;
for ( var i= 0 ; i< num; i++ ) {
resStr = resStr. concat ( sstr[ i] ) ;
}
resStr = resStr. concat ( "." ) ;
for ( var i= num; i< sstr. length; i++ ) {
resStr = resStr. concat ( sstr[ i] ) ;
}
} else {
var zero = "0" ;
var point = "." ;
var num = pointNum- resStr. length+ 2 ;
for ( var i= 0 ; i< num; i++ ) {
if ( i== num- 2 ) {
resStr = point. concat ( resStr) ;
} else {
resStr = zero. concat ( resStr) ;
}
}
}
}
return Number ( resStr) ;
}
function div ( num1, num2) {
var pointNum1 = 0 ;
var pointNum2 = 0 ;
var numStr1 = num1. toString ( ) ;
var numStr2 = num2. toString ( ) ;
if ( numStr1. indexOf ( "." , 0 ) != - 1 ) {
pointNum1 = numStr1. length- numStr1. indexOf ( "." , 0 ) - 1 ;
num1 = Number ( numStr1. replace ( "." , "" ) ) ;
}
if ( numStr2. indexOf ( "." , 0 ) != - 1 ) {
pointNum2 = numStr2. length- numStr2. indexOf ( "." , 0 ) - 1 ;
num2 = Number ( numStr2. replace ( "." , "" ) ) ;
}
var resStr = num2/ num1;
var pointNum = pointNum1> pointNum2? pointNum1- pointNum2: pointNum2- pointNum1;
var pointSum = 1 ;
if ( pointNum!= 0 ) {
for ( var i = 0 ; i< pointNum; i++ ) {
pointSum = pointSum* 10 ;
}
resStr = resStr/ pointSum;
}
return resStr;
}
function charRes ( num1, num2, ch) {
var res = 0 ;
switch ( ch) {
case "+" : res = num2+ num1; break ;
case "-" : res = num2- num1; break ;
case "*" : res = mul ( num1, num2) ; break ;
case "/" : res = div ( num1, num2) ; break ;
}
return res;
}
function charNum ( str) {
var num = 0 ;
switch ( str) {
case "+" : num = 0 ; break ;
case "-" : num = 1 ; break ;
case "*" : num = 2 ; break ;
case "/" : num = 3 ; break ;
case "(" : num = 4 ; break ;
case ")" : num = 5 ; break ;
case "0" : num = 5 ; break ;
}
return num;
}
function judgePriority ( str1, str2) {
var operator = new Array ( 6 ) ;
operator[ 0 ] = new Array ( - 1 , - 1 , 1 , 1 , 1 , 0 ) ;
operator[ 1 ] = new Array ( - 1 , - 1 , 1 , 1 , 1 , 0 ) ;
operator[ 2 ] = new Array ( - 1 , - 1 , - 1 , - 1 , 1 , 0 ) ;
operator[ 3 ] = new Array ( - 1 , - 1 , - 1 , - 1 , 1 , 0 ) ;
operator[ 4 ] = new Array ( 1 , 1 , 1 , 1 , 1 , 0 ) ;
operator[ 5 ] = new Array ( 1 , 1 , 1 , 1 , 1 , 0 ) ;
var i = charNum ( str1) ;
var j = charNum ( str2) ;
return operator[ i] [ j] ;
}
function getResult ( ) {
var j = 0 ;
var finalNum = "" ;
var fflag;
for ( var i= 0 ; i< str. length; i++ ) {
finalNum = "" ;
fflag = false ;
if ( ! isNaN ( str[ i] ) ) {
j = i+ 1 ;
while ( j< str. length) {
if ( ! isNaN ( str[ j] ) ) {
j++ ;
} else if ( str[ j] == "." ) {
j++ ;
} else {
break ;
}
}
for ( m= i; m< j; m++ ) {
finalNum += str[ m] ;
}
numStr. unshift ( Number ( finalNum) ) ;
i = j- 1 ;
}
else {
if ( str[ i] == "-" ) {
if ( i== 0 ) {
fflag= true ;
} else {
if ( str[ i- 1 ] == "(" ) {
fflag= true ;
} else {
fflag= false ;
}
}
}
if ( judgePriority ( charStr[ 0 ] , str[ i] ) > 0 ) {
charStr. unshift ( str[ i] ) ;
flag. unshift ( fflag) ;
} else {
var ch = charStr. shift ( ) ;
if ( judgePriority ( charStr[ 0 ] , str[ i] ) == 0 ) {
while ( ch!= "(" ) {
var num1 = numStr. shift ( ) ;
if ( ch== "-" && flag[ 0 ] == true ) {
numStr. unshift ( 0 ) ;
}
flag. shift ( ) ;
var num2 = numStr. shift ( ) ;
numStr. unshift ( charRes ( num1, num2, ch) ) ;
ch = charStr. shift ( ) ;
}
flag. shift ( ) ;
i++ ;
} else {
var num1 = numStr. shift ( ) ;
if ( ch== "-" && flag[ 0 ] == true ) {
numStr. unshift ( 0 ) ;
}
flag. shift ( ) ;
var num2 = numStr. shift ( ) ;
numStr. unshift ( charRes ( num1, num2, ch) ) ;
}
i-- ;
}
}
}
while ( charStr. length!= 1 ) {
var ch = charStr. shift ( ) ;
var num1 = numStr. shift ( ) ;
if ( ch== "-" && flag[ 0 ] == true ) {
numStr. unshift ( 0 ) ;
}
flag. shift ( ) ;
var num2 = numStr. shift ( ) ;
numStr. unshift ( charRes ( num1, num2, ch) ) ;
}
return numStr[ 0 ] ;
}
function showStr ( ) {
var equalStr = "" ;
if ( str. length== 0 ) {
equalStr= "0" ;
}
for ( var i= 0 ; i< str. length; i++ ) {
equalStr += str[ i] ;
}
return equalStr;
}
script>
head>
< body>
< div class = " bg" >
< div class = " uup" >
< p class = " logo" > CASIO. p>
div>
< div class = " up" >
div>
< div class = " down" >
< table class = " numtable" >
< tr>
< td> < input type = " button" value = " CE" id = " reset" /> td>
< td> < input type = " button" value = " +" id = " plus" /> td>
< td> < input type = " button" value = " -" id = " minus" /> td>
< td> < input type = " button" value = " DEL" id = " del" /> td>
tr>
< tr>
< td> < input type = " button" value = " 7" id = " num7" /> td>
< td> < input type = " button" value = " 8" id = " num8" /> td>
< td> < input type = " button" value = " 9" id = " num9" /> td>
< td> < input type = " button" value = " *" id = " multiply" /> td>
tr>
< tr>
< td> < input type = " button" value = " 4" id = " num4" /> td>
< td> < input type = " button" value = " 5" id = " num5" /> td>
< td> < input type = " button" value = " 6" id = " num6" /> td>
< td> < input type = " button" value = " /" id = " divide" /> td>
tr>
< tr>
< td> < input type = " button" value = " 1" id = " num1" /> td>
< td> < input type = " button" value = " 2" id = " num2" /> td>
< td> < input type = " button" value = " 3" id = " num3" /> td>
< td> < input type = " button" value = " ." id = " point" /> td>
tr>
< tr>
< td> < input type = " button" value = " (" id = " leftbracket" /> td>
< td> < input type = " button" value = " 0" id = " num0" /> td>
< td> < input type = " button" value = " )" id = " rightbracket" /> td>
< td> < input type = " button" value = " =" id = " equal" /> td>
tr>
table>
div>
div>
body>
html>
效果图: