第二次作业

发布时间 2023-12-04 18:20:45作者: 木南希

1.登录

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计算器登录注册页面</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
width: 400px;
padding: 20px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
}
input {
display: block;
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
button {
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
padding: 10px;
width: 100%;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1>计算器登录注册页面</h1>
<form id="loginForm">
<input type="text" placeholder="用户名" required>
<input type="password" placeholder="密码" required>
<button type="submit">登录</button>
</form>
<p>还没有账号?<a href="#">立即注册</a></p>
</div>
<script>
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault();
var username = document.querySelector('input[type="text"]').value;
var password = document.querySelector('input[type="password"]').value;
if (username && password) {
alert('登录成功,欢迎 ' + username + '!');
} else {
alert('请输入用户名和密码!');
}
});
</script>
</body>
</html>

2.CSS样式

body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}

.calculator {
border: 1px solid #ccc;
border-radius: 5px;
background-color: #fff;
padding: 20px;
width: 300px;
}

input {
width: 100%;
margin-bottom: 10px;
padding: 5px;
font-size: 18px;
}

.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 5px;
}

button {
background-color: #f0f0f0;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 18px;
padding: 5px;
cursor: pointer;
}

button:hover {
background-color: #e0e0e0;
}

3.JavaScript逻辑

const display = document.getElementById('display');
const buttons = document.querySelectorAll('.buttons button');
let currentInput = '';
let operator = null;
let operand1 = null;
let operand2 = null;

function clearDisplay() {
display.value = '';
operator = null;
operand1 = null;
operand2 = null;
}

function appendNumber(number) {
currentInput += number;
display.value = currentInput;
}

function setOperator(op) {
if (operator !== null) {
calculate();
}
operator = op;
operand1 = parseFloat(currentInput);
currentInput = '';
}

function calculate() {
operand2 = parseFloat(currentInput);
switch (operator) {
case '+':
currentInput = operand1 + operand2;
break;
case '-':
currentInput = operand1 - operand2;
break;
case '*':
currentInput = operand1 * operand2;
break;
case '/':
currentInput = operand1 / operand2;
break;
}
display.value = currentInput;
operator = null;
operand1 = null;
operand2 = null;
}

function saveHistory() {
const history = JSON.parse(localStorage.getItem('history')) || [];
history.push({ input: currentInput, result: display.value });
localStorage.setItem('history', JSON.stringify(history));
}

buttons.forEach(button => {
button.addEventListener('click', () => {
if (!isNaN(button.textContent)) {
appendNumber(button.textContent);
} else if (button.textContent === 'C') {
clearDisplay();
} else if (button.textContent === '=') {
calculate();
saveHistory();
} else {
setOperator(button.textContent);
}
});
});