Files
javalab/P9.java
Amneesh Singh 14898356a8 P6-P15: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2023-06-05 03:15:38 +05:30

155 lines
5.3 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class P9 extends JFrame {
private JTextField displayField;
public P9() {
setTitle("Scientific Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
displayField = new JTextField();
displayField.setPreferredSize(new Dimension(300, 30));
displayField.setEditable(false);
JPanel buttonPanel = new JPanel(new GridLayout(6, 4, 10, 10));
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"sin", "cos", "tan", "sqrt",
"e", "^", "pi", "clr"
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
button.addActionListener(new ButtonClickListener());
buttonPanel.add(button);
}
setLayout(new FlowLayout());
add(displayField);
add(buttonPanel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
String expression = displayField.getText();
if ("=".equals(command)) {
calculate();
} else if ("clr".equals(command)) {
displayField.setText("");
} else if ("sin".equals(command)) {
displayField.setText(String.valueOf(Math.sin(Double.parseDouble(expression))));
} else if ("cos".equals(command)) {
displayField.setText(String.valueOf(Math.cos(Double.parseDouble(expression))));
} else if ("tan".equals(command)) {
displayField.setText(String.valueOf(Math.tan(Double.parseDouble(expression))));
} else if ("sqrt".equals(command)) {
displayField.setText(String.valueOf(Math.sqrt(Double.parseDouble(expression))));
} else {
displayField.setText(displayField.getText() + command);
}
}
private void calculate() {
String expression = displayField.getText();
try {
double result = evaluateExpression(expression);
displayField.setText(String.valueOf(result));
} catch (NumberFormatException e) {
displayField.setText("Error");
}
}
private double evaluateExpression(String expression) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < expression.length()) ? expression.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < expression.length()) throw new RuntimeException("Unexpected character: " + (char) ch);
return x;
}
double parseExpression() {
double x = parseTerm();
while(true) {
if (eat('+')) x += parseTerm();
else if (eat('-')) x -= parseTerm();
else return x;
}
}
double parseTerm() {
double x = parseFactor();
while (true) {
if (eat('*')) x *= parseFactor();
else if (eat('/')) x /= parseFactor();
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor();
if (eat('-')) return -parseFactor();
double x;
int startPos = this.pos;
if (eat('(')) {
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') {
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(expression.substring(startPos, this.pos));
} else if (eat('e')) {
x = Math.E;
} else if (eat('p')) {
eat('i');
x = Math.PI;
} else {
throw new RuntimeException("Unexpected character: " + (char) ch);
}
if (eat('^')) x = Math.pow(x, parseFactor());
return x;
}
}.parse();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new P9();
}
});
}
}