Java课设 保存计算过程的计算器(附完整源码)

前言

这是一个保存计算过程的计算器,calculator.java中实现计算原理,MyForm.java中实现面板。效果如图:
在这里插入图片描述

calculator.java

import java.util.Stack;

public class Calculator {

    public String cal(String str) {
        str = str.replaceAll("(?<![0-9)}\\]])(?=-[0-9({\\[])", "0") + "#";
        //初始化栈
        Stack<Double> operandStack = new Stack<>();
        Stack<Character> operatorStack = new Stack<>();

        //分析计算
        int strLen = str.length();
        String temp = "";
        for (int i = 0; i < strLen; i++) {
            char x = str.charAt(i);
            //若为数字,记入temp中;否则转double入栈
            if (x >= '0' && x <= '9' || x == '.') {
                temp += x;
            } else {
                if (!temp.isEmpty()) {
                    double a = Double.parseDouble(temp);
                    operandStack.push(a);
                    temp = "";
                }

                if (x == '#') {
                    break;
                } else if (x == '+' || x == '-') {
                    if (operatorStack.empty() || operatorStack.peek() == '(') {
                        operatorStack.push(x);
                    } else {
                        while (!operatorStack.empty() && operatorStack.peek() != '(') {
                            //出栈计算
                            pop2Cal(operandStack, operatorStack);
                        }
                        operatorStack.push(x);

                    }
                } else if (x == '*' || x == '/') {
                    if (operatorStack.empty() || operatorStack.peek() == '(' || operatorStack.peek() == '+' || operatorStack.peek() == '-') {
                        operatorStack.push(x);
                    } else {
                        while (!operatorStack.empty() && operatorStack.peek() != '(' && operatorStack.peek() != '+' && operatorStack.peek() != '-') {
                            //出栈计算
                            pop2Cal(operandStack, operatorStack);
                        }
                        operatorStack.push(x);
                    }
                } else {
                    if (x == '(') {
                        operatorStack.push(x);
                    } else if (x == ')') {
                        while (operatorStack.peek() != '(') {
                            //出栈计算
                            pop2Cal(operandStack, operatorStack);
                        }
                        operatorStack.pop();
                    }
                }
            }
        }
        while (!operatorStack.empty()) {
            //出栈计算
            pop2Cal(operandStack, operatorStack);
        }
        //返回结果
        return operandStack.pop().toString();
    }

    //出栈计算
    private static void pop2Cal(Stack<Double> opStack, Stack<Character> otStack) {
        double op = 0;
        double op2 = opStack.pop();
        double op1 = opStack.pop();
        char ot = otStack.pop();
        switch (ot) {
            case '+':
                op = op1 + op2;
                break;
            case '-':
                op = op1 - op2;
                break;
            case '*':
                op = op1 * op2;
                break;
            case '/':
                op = op1 / op2;
                break;
        }
        opStack.push(op);
    }

}

MyForm.java

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;

public class MyForm {
    JPanel panel1;
    private JButton a7Button;
    private JButton a8Button;
    private JButton a9Button;
    private JButton mul;
    private JButton a5Button;
    private JButton a4Button;
    private JButton a6Button;
    private JButton sub;
    private JButton a3Button;
    private JButton a2Button;
    private JButton a1Button;
    private JButton add;
    private JButton sqrt;
    private JButton a0Button;
    private JButton point;
    private JButton cButton;
    private JButton div;
    private JButton equ;
    private JButton back;
    private JTextField textField1;
    private JButton saveButton;

    private Calculator calculator;

    private StringBuilder sb;
    private String str = "";
    private void saveCalculationToFile(String filePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
            String[] steps = sb.toString().split("[+\\-*/]");
            String[] operators = sb.toString().split("[0-9\\.]+");
            if (steps.length != operators.length) {
                throw new IllegalArgumentException("Invalid calculation format.");
            }

            StringBuilder calculation = new StringBuilder();
            calculation.append(steps[0]);
            for (int i = 1; i < steps.length; i++) {
                calculation.append(" ")
                        .append(operators[i])
                        .append(" ")
                        .append(steps[i]);
            }
            calculation.append(" = ").append(str).append("\n");

            writer.append(calculation.toString());
            writer.flush();
            JOptionPane.showMessageDialog(panel1, "Calculation saved successfully!");
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(panel1, "Error occurred while saving calculation.", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(panel1, "Invalid calculation format.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private JFileChooser fileChooser;
    private void playSound(URL soundURL) {
        try {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundURL);
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    public MyForm() {
        sb = new StringBuilder();
        calculator = new Calculator();
        // 获取音效文件的URL
        URL soundURL = getClass().getResource("music.wav");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int result = fileChooser.showSaveDialog(panel1);
                if (result == JFileChooser.APPROVE_OPTION) {
                    String filePath = fileChooser.getSelectedFile().getAbsolutePath();
                    saveCalculationToFile(filePath);
                }

            }
        });

        fileChooser = new JFileChooser();

        a0Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("0");
                textField1.setText(sb.toString());
                playSound(soundURL);
            }
        });
        a1Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("1");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        a2Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("2");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        a3Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("3");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        a4Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("4");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        a5Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("5");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        a6Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("6");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        a7Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("7");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        a8Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("8");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        a9Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("9");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        point.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sb.length() == 0) sb.append("0.");
                else if (sb.charAt(sb.length() - 1) != '.') {
                    sb.append(".");
                }
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        cButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.delete(0, sb.length());
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sb.length() > 0) {
                    sb.deleteCharAt(sb.length() - 1);
                    textField1.setText(sb.toString());
                }
                playSound(soundURL);

            }
        });
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sb.length() == 0) sb.append("0");
                else if (sb.charAt(sb.length() - 1) >= '0' && sb.charAt(sb.length() - 1) <= '9') {
                    sb.append("+");
                }
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        sub.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append("-");
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        mul.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sb.length() == 0) sb.append("0");
                else if (sb.charAt(sb.length() - 1) >= '0' && sb.charAt(sb.length() - 1) <= '9') {
                    sb.append("*");
                }
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        div.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sb.length() == 0) sb.append("0");
                else if (sb.charAt(sb.length() - 1) >= '0' && sb.charAt(sb.length() - 1) <= '9') {
                    sb.append("/");
                }
                textField1.setText(sb.toString());
                playSound(soundURL);

            }
        });
        panel1.addComponentListener(new ComponentAdapter() {
        });
        equ.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sb.length() == 0) sb.append("0");
                try {
                    str = sb.toString();
                    //sb.delete(0,sb.length());
                    str = calculator.cal(str);
                    sb.replace(0, sb.length(), str);
                    textField1.setText(str);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    textField1.setText("Error!");
                    sb.delete(0, sb.length());
                }
                playSound(soundURL);

            }
        });

        sqrt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sb.length() == 0) {
                    sb.append("0");
                }
                double num = Double.parseDouble(calculator.cal(sb.toString()));
                if (num >= 0) {
                    sb.delete(0, sb.length());
                    sb.append(Math.sqrt(num));
                    textField1.setText(sb.toString());
                } else {
                    textField1.setText("Error!");
                    sb.delete(0, sb.length());
                }
                playSound(soundURL);

            }
        });
    }
}

main.java

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("计算器");
        frame.setContentPane(new MyForm().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个努力学习编程的人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值