<!DOCTYPE html>
<html>
<head>
<title>简易计算器</title>
<style>
body {
text-align: center;
}
#calculator {
width: 200px;
margin: 0 auto;
font-size: 200%;
font-family: Arial, sans-serif;
}
input[type="text"] {
width: 90%;
margin-bottom: 10px;
padding: 5px;
}
input[type="button"] {
width: 40px;
margin: 5px;
padding: 5px;
}
</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 {
addDigit(button);
}
}
function addDigit(digit) {
var input = display.value;
if (waitingForOperand) {
input = "";
waitingForOperand = false;
}
display.value
评论已关闭