<!DOCTYPE html>
<html>
<head>
<title>简易计算器</title>
<style>
body {
text-align: center;
}
#calculator {
width: 200px;
margin: 20px auto;
border: 1px solid #ccc;
padding: 20px;
}
input[type="text"] {
width: 90%;
margin-bottom: 10px;
padding: 8px;
}
input[type="button"] {
width: 40px;
margin: 5px;
padding: 8px;
}
</style>
</head>
<body>
<div id="calculator">
<input type="text" id="display" disabled>
<input type="button" value="1" onclick="press('1')">
<input type="button" value="2" onclick="press('2')">
<input type="button" value="3" onclick="press('3')">
<input type="button" value="+" onclick="press('+')">
<input type="button" value="4" onclick="press('4')">
<input type="button" value="5" onclick="press('5')">
<input type="button" value="6" onclick="press('6')">
<input type="button" value="-" onclick="press('-')">
<input type="button" value="7" onclick="press('7')">
<input type="button" value="8" onclick="press('8')">
<input type="button" value="9" onclick="press('9')">
<input type="button" value="*" onclick="press('*')">
<input type="button" value="0" onclick="press('0')">
<input type="button" value="." onclick="press('.')">
<input type="button" value="/" onclick="press('/')">
<input type="button" value="=" onclick="press('=')">
</div>
<script>
var display = document.getElementById('display');
var operator = null;
var firstNumber = 0;
var waitingForOperand = true;
function press(button) {
if (button === "=") {
calculateResult();
} else if (button === "C") {
clear();
} else {
if (waitingForOperand) {
display.value = "";
waitingForOperand = false;
}
评论已关闭