Created
February 24, 2025 20:08
-
-
Save dragonhuntr/1d0f65c2eb5586159d77180973e494d5 to your computer and use it in GitHub Desktop.
Lab6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.BorderLayout; | |
| import java.awt.GridBagConstraints; | |
| import java.awt.GridBagLayout; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import javax.swing.*; | |
| import java.util.Stack; | |
| public class Calculator implements ActionListener { | |
| private static JTextField inputBox; | |
| // adding memory to save values | |
| private double memory = 0; | |
| JOptionPane optionPane = new JOptionPane("thank you for using java", JOptionPane.WARNING_MESSAGE); | |
| Calculator() { | |
| } | |
| public static void main(String[] args) { | |
| createWindow(); | |
| } | |
| private static void createWindow() { | |
| JFrame frame = new JFrame("Calculator"); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| createUI(frame); | |
| frame.setSize(400, 400); | |
| frame.setLocationRelativeTo(null); | |
| frame.setVisible(true); | |
| } | |
| private static void createUI(JFrame frame) { | |
| // separating textbox to resize without grid constraints | |
| JPanel textbox = new JPanel(new BorderLayout()); | |
| inputBox = new JTextField("", 30); | |
| inputBox.setEditable(false); | |
| JButton clearEntryButton = new JButton("CE"); | |
| clearEntryButton.addActionListener(new ActionListener() { | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| inputBox.setText(""); | |
| } | |
| }); | |
| textbox.add(inputBox, BorderLayout.CENTER); | |
| textbox.add(clearEntryButton, BorderLayout.EAST); | |
| frame.getContentPane().add(textbox, BorderLayout.NORTH); | |
| JPanel panel = new JPanel(); | |
| Calculator calculator = new Calculator(); | |
| GridBagLayout layout = new GridBagLayout(); | |
| GridBagConstraints gbc = new GridBagConstraints(); | |
| panel.setLayout(layout); | |
| String[] buttonLabels = { | |
| "mc", "mr", "m-", "m+", | |
| "AC", "√x", "%", "÷", | |
| "7", "8", "9", "x", | |
| "4", "5", "6", "-", | |
| "1", "2", "3", "+", | |
| "0", ".", "+/-", "=", | |
| "π", "xʸ", "R2", "R0", | |
| }; | |
| int gridx = 0; | |
| int gridy = 1; | |
| for (String label : buttonLabels) { | |
| JButton button = new JButton(label); | |
| button.addActionListener(calculator); | |
| gbc.gridx = gridx; | |
| gbc.gridy = gridy; | |
| // setting button size to fill frame | |
| gbc.fill = GridBagConstraints.BOTH; | |
| gbc.weightx = 1; | |
| gbc.weighty = 1; | |
| panel.add(button, gbc); | |
| gridx++; | |
| // setting grid layout to 4 columns only | |
| if (gridx > 3) { | |
| gridx = 0; | |
| gridy++; | |
| } | |
| } | |
| frame.getContentPane().add(panel, BorderLayout.CENTER); | |
| } | |
| /* | |
| * 1. put all expressions in textbox | |
| * 2. parse textbox and split by spaces | |
| * 3. push numbers to stack | |
| * 4. evaluate expression by popping numbers and applying operators | |
| * 5. return result and save to memory | |
| * 6. display result | |
| */ | |
| public void actionPerformed(ActionEvent e) { | |
| String command = e.getActionCommand(); | |
| // using switch case to handle special functions | |
| switch (command) { | |
| // calculate | |
| case "=": | |
| String result = evaluate(inputBox.getText()); | |
| inputBox.setText(result); | |
| // check if valid number | |
| try { | |
| memory = Double.parseDouble(result); | |
| } catch (NumberFormatException ex) { | |
| JOptionPane.showMessageDialog(null, "Textbox is empty"); | |
| } | |
| break; | |
| // plus/minus toggles pos/neg sign of displayed number | |
| case "+/-": | |
| inputBox.setText(String.valueOf(-Double.parseDouble(inputBox.getText()))); | |
| break; | |
| // memory functions | |
| // memory clear, clear memory to 0, dont affect display | |
| case "mc": | |
| memory = 0; | |
| break; | |
| // memory recall, display current memory value | |
| case "mr": | |
| inputBox.setText(String.valueOf(memory)); | |
| break; | |
| // memory minus, subtract display value from memory value | |
| case "m-": | |
| memory -= Double.parseDouble(inputBox.getText()); | |
| break; | |
| // memory plus, add display value to memory value | |
| case "m+": | |
| memory += Double.parseDouble(inputBox.getText()); | |
| break; | |
| // clear entry | |
| case "CE": | |
| inputBox.setText(""); | |
| break; | |
| // all clear, clear display and memory | |
| case "AC": | |
| inputBox.setText(""); | |
| memory = 0; | |
| break; | |
| // square root | |
| case "√x": | |
| inputBox.setText("√ " + inputBox.getText()); | |
| break; | |
| // exponent | |
| case "xʸ": | |
| inputBox.setText(inputBox.getText() + " ^ "); | |
| break; | |
| // round to 2 decimals (cents) | |
| case "R2": | |
| inputBox.setText(String.format("%.2f", Double.parseDouble(inputBox.getText()))); | |
| break; | |
| // round to 0 decimals (dollars) | |
| case "R0": | |
| inputBox.setText(String.format("%.0f", Double.parseDouble(inputBox.getText()))); | |
| break; | |
| default: | |
| // if command is a number or decimal, add to display | |
| if (command.matches("[0-9]+") || command.equals(".")) { | |
| inputBox.setText(inputBox.getText() + command); | |
| } else { | |
| inputBox.setText(inputBox.getText() + " " + command + " "); | |
| } | |
| break; | |
| } | |
| } | |
| public static String evaluate(String expression) { | |
| // using stack to evaluate expression, first in last out | |
| try { | |
| Stack<Double> numbers = new Stack<>(); | |
| Stack<String> operators = new Stack<>(); | |
| // splitting expression by spaces to iterate through each token | |
| System.out.println(expression); | |
| // first loop to extract numbers and operators | |
| for (String token : expression.split(" ")) { | |
| if (token.matches("[0-9]+") || token.matches("[0-9]*\\.[0-9]+")) { | |
| numbers.push(Double.parseDouble(token)); | |
| } else { | |
| operators.push(token); | |
| } | |
| } | |
| // process remaining operators | |
| while (!operators.isEmpty()) { | |
| applyOperator(numbers, operators.pop()); | |
| } | |
| // if result decimal is 0, return as integer | |
| // peek() - checking top of stack value without pop | |
| if (numbers.peek() % 1 == 0) { | |
| return String.valueOf(numbers.pop().intValue()); | |
| } | |
| return String.valueOf(numbers.pop()); | |
| } catch (Exception e) { | |
| return ""; | |
| } | |
| } | |
| private static void applyOperator(Stack<Double> numbers, String operator) { | |
| switch (operator) { | |
| case "+": | |
| numbers.push(numbers.pop() + numbers.pop()); | |
| break; | |
| case "-": | |
| double b = numbers.pop(); | |
| double a = numbers.pop(); | |
| numbers.push(a - b); | |
| break; | |
| case "x": | |
| numbers.push(numbers.pop() * numbers.pop()); | |
| break; | |
| case "÷": | |
| double divisor = numbers.pop(); | |
| numbers.push(numbers.pop() / divisor); | |
| break; | |
| case "^": | |
| double exponent = numbers.pop(); | |
| numbers.push(Math.pow(numbers.pop(), exponent)); | |
| break; | |
| case "√": | |
| numbers.push(Math.sqrt(numbers.pop())); | |
| break; | |
| case "%": | |
| numbers.push(numbers.pop() / 100); | |
| break; | |
| case "π": | |
| numbers.push(numbers.pop() * 3.1415926536); | |
| break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment