Compare commits

...

No commits in common. 'master' and 'main' have entirely different histories.
master ... main

@ -1,98 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private double num1, num2, result;
private String operator;
public Calculator() {
// 设置窗口基本属性
setTitle("计算器");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 创建文本框
textField = new JTextField();
textField.setFont(new Font("Arial", Font.PLAIN, 24));
add(textField, BorderLayout.NORTH);
// 创建按钮面板
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4, 10, 10));
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+",
"%", "(", ")", ""
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
button.setFont(new Font("Arial", Font.PLAIN, 24));
button.addActionListener(this);
panel.add(button);
}
add(panel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "C":
textField.setText("");
break;
case "=":
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0) {
textField.setText("错误: 除以零");
return;
}
result = num1 / num2;
break;
case "%":
result = num1 % num2;
break;
}
textField.setText(String.valueOf(result));
break;
case "+":
case "-":
case "*":
case "/":
case "%":
num1 = Double.parseDouble(textField.getText());
operator = command;
textField.setText("");
break;
default:
textField.setText(textField.getText() + command);
break;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Calculator calculator = new Calculator();
calculator.setVisible(true);
});
}
}

@ -0,0 +1,2 @@
# Textcpt

@ -0,0 +1,213 @@
<!DOCTYPE html>
<style>
/* styles.css */
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
.calculator {
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
background-color: #fff;
padding: 20px;
}
.calculator-screen {
width: 100%;
height: 80px;
border: none;
background-color: #eee;
text-align: right;
font-size: 2em;
padding: 20px;
box-sizing: border-box;
border-radius: 5px;
margin-bottom: 10px;
}
.calculator-keys {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
height: 60px;
font-size: 1.5em;
border: none;
border-radius: 5px;
background-color: #eee;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #aeaeae;
}
button.operator {
background-color: #007BFF;
color: #fff;
}
button.operator:hover {
background-color: #0056b3;
}
button.all-clear, button.equal-sign {
grid-column: span 2;
background-color: #28a745;
color: #fff;
}
button.all-clear:hover, button.equal-sign:hover {
background-color: #218838;
}
</style>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
</head>
<body>
<div class="calculator">
<input type="text" class="calculator-screen" value="" disabled />
<div class="calculator-keys">
<button type="button" class="operator" value="+">+</button>
<button type="button" class="operator" value="-">-</button>
<button type="button" class="operator" value="*">&times;</button>
<button type="button" class="operator" value="/">&divide;</button>
<button type="button" class="operator" value="%">%</button>
<button type="button" value="7">7</button>
<button type="button" value="8">8</button>
<button type="button" value="9">9</button>
<button type="button" value="4">4</button>
<button type="button" value="5">5</button>
<button type="button" value="6">6</button>
<button type="button" value="1">1</button>
<button type="button" value="2">2</button>
<button type="button" value="3">3</button>
<button type="button" value="0">0</button>
<button type="button" class="decimal" value=".">.</button>
<button type="button" class="all-clear" value="all-clear">AC</button>
<button type="button" class="equal-sign operator" value="=">=</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
<script>
// script.js
document.addEventListener('DOMContentLoaded', (event) => {
const calculator = {
displayValue: '0',
firstOperand: null,
waitingForSecondOperand: false,
operator: null,
};
function updateDisplay() {
const display = document.querySelector('.calculator-screen');
display.value = calculator.displayValue;
}
const inputKeys = document.querySelector('.calculator-keys');
inputKeys.addEventListener('click', (event) => {
const { target } = event;
const { value } = target;
if (!target.matches('button')) {
return;
}
switch (value) {
case '+':
case '-':
case '*':
case '/':
case '%':
if (calculator.waitingForSecondOperand) {
calculate(calculator.firstOperand, calculator.displayValue, calculator.operator);
} else {
calculator.firstOperand = parseFloat(calculator.displayValue);
calculator.waitingForSecondOperand = true;
calculator.operator = value;
}
calculator.displayValue = '';
break;
case '=':
if (calculator.waitingForSecondOperand) {
calculate(calculator.firstOperand, calculator.displayValue, calculator.operator);
}
break;
case '.':
if (!calculator.displayValue.includes('.')) {
calculator.displayValue += value;
}
break;
case 'all-clear':
calculator.displayValue = '0';
calculator.firstOperand = null;
calculator.waitingForSecondOperand = false;
calculator.operator = null;
break;
default:
if (Number.isInteger(parseFloat(value))) {
if (calculator.displayValue === '0' && value !== '0') {
calculator.displayValue = value;
} else {
calculator.displayValue += value;
}
}
}
updateDisplay();
});
function calculate(firstOperand, secondOperand, operator) {
let result;
const secondOperandNumber = parseFloat(secondOperand);
switch (operator) {
case '+':
result = firstOperand + secondOperandNumber;
break;
case '-':
result = firstOperand - secondOperandNumber;
break;
case '*':
result = firstOperand * secondOperandNumber;
break;
case '/':
result = firstOperand / secondOperandNumber;
break;
case '%':
result = firstOperand % secondOperandNumber;
break;
default:
return;
}
calculator.displayValue = result;
calculator.firstOperand = result;
calculator.waitingForSecondOperand = false;
calculator.operator = null;
}
updateDisplay();
});
</script>
Loading…
Cancel
Save