This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Scientific_calculator
133 lines (115 loc) · 4.22 KB
/
Scientific_calculator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ScientificCalculator extends JFrame {
private JTextField displayField;
private StringBuilder inputBuffer;
public ScientificCalculator() {
setTitle("Scientific Calculator");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
inputBuffer = new StringBuilder();
displayField = new JTextField();
displayField.setEditable(false);
displayField.setHorizontalAlignment(JTextField.RIGHT);
displayField.setFont(new Font("Arial", Font.PLAIN, 24));
add(displayField, BorderLayout.NORTH);
add(createButtonPanel(), BorderLayout.CENTER);
}
private JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 4, 5, 5));
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"C", "sqrt", "pow", "sin"
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
button.addActionListener(new ButtonClickListener());
buttonPanel.add(button);
}
return buttonPanel;
}
private class ButtonClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
String command = source.getText();
switch (command) {
case "=":
evaluateExpression();
break;
case "C":
clearInput();
break;
case "sqrt":
calculateSquareRoot();
break;
case "pow":
inputBuffer.append("^");
break;
case "sin":
calculateSin();
break;
default:
inputBuffer.append(command);
break;
}
updateDisplay();
}
private void evaluateExpression() {
try {
String expression = inputBuffer.toString();
double result = evaluate(expression);
clearInput();
inputBuffer.append(result);
} catch (Exception ex) {
clearInput();
inputBuffer.append("Error");
}
}
private double evaluate(String expression) {
// Use a library or write a parser to evaluate the mathematical expression
// For simplicity, we'll just use the built-in JavaScript engine
// Note: Using JavaScript engine for evaluation is not recommended for production code
return (double) new ScriptEngineManager().getEngineByName("JavaScript").eval(expression);
}
private void clearInput() {
inputBuffer.setLength(0);
}
private void calculateSquareRoot() {
try {
double operand = Double.parseDouble(inputBuffer.toString());
clearInput();
inputBuffer.append(Math.sqrt(operand));
} catch (NumberFormatException ignored) {
clearInput();
inputBuffer.append("Error");
}
}
private void calculateSin() {
try {
double angle = Math.toRadians(Double.parseDouble(inputBuffer.toString()));
clearInput();
inputBuffer.append(Math.sin(angle));
} catch (NumberFormatException ignored) {
clearInput();
inputBuffer.append("Error");
}
}
private void updateDisplay() {
displayField.setText(inputBuffer.toString());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
ScientificCalculator calculator = new ScientificCalculator();
calculator.setVisible(true);
});
}
}