赞
踩
代码运行如图
- package 其他;
- //计算器,可加减乘除混合运算
- //2020.11.25
- //18计科2 张阵涛
-
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Stack;
-
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.border.Border;
-
- public class MyCalc {
- public static void main(String[] args) {
- new myFrame("Calculator");
- }
-
- }
-
- class myFrame extends JFrame {
-
- JTextField text = new JTextField();
-
- JButton[] bt1 = new JButton[16];
- JButton[] bt2 = new JButton[3];
- String key2[] = new String[]{ "BackSpace","Clear", "C"};
- String key1[] = new String[] { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*",
- "0", ".", "=", "/" };
- JPanel key1Panel = new JPanel();
- JPanel key2Panel = new JPanel();
-
- boolean finish = false;
- double ans=0;
-
- public myFrame(String title) {
- super(title);
- init();
- }
-
- public void init() {
- text.setHorizontalAlignment(JTextField.RIGHT);
- text.setPreferredSize(new Dimension(0, 40));
- text.setFont(new Font("微软雅黑", Font.BOLD, 20));
- for (int i = 0; i < bt1.length; i++) {
- bt1[i] = new JButton(key1[i]);
- }
- for (int i = 0; i < bt2.length; i++) {
- bt2[i] = new JButton(key2[i]);
- }
- Container pane = this.getContentPane();
- this.add(text, BorderLayout.NORTH);
- key1Panel.setLayout(new GridLayout(5, 4, 3, 3));
- this.add(key2Panel, BorderLayout.CENTER);
- this.add(key1Panel, BorderLayout.SOUTH);
- for (int i = 0; i < bt1.length; i++) {
- key1Panel.add(bt1[i]);
- bt1[i].addActionListener(new ActionListener() {
-
- public void actionPerformed(ActionEvent e) {
-
- if (finish == true) {
- text.setText("");
- finish = false;
- }
- String command = e.getActionCommand();
- String s = text.getText();
- if ("0123456789.+-*/".indexOf(command) >= 0)
- text.setText(text.getText() + command);
- else if (command.equals("Clear"))
- text.setText(s.substring(0, s.length() - 1));
- else if (command.equals("C") || command.equals("BackSpace"))
- text.setText("");
-
- else if (command.equals("=")) {
- ans=new calc(s).result;
- text.setText(text.getText() + "="+ans);
- finish = true;
- }
-
- }
- });
- }
- for (int i = 0; i < bt2.length; i++) {
- key2Panel.add(bt2[i]);
- bt2[i].addActionListener(new ActionListener() {
-
- public void actionPerformed(ActionEvent e) {
-
- if (finish == true) {
- text.setText("");
- finish = false;
- }
- String command = e.getActionCommand();
- String s = text.getText();
- if ("0123456789.+-*/".indexOf(command) >= 0)
- text.setText(text.getText() + command);
- else if (command.equals("Clear"))
- text.setText(s.substring(0, s.length() - 1));
- else if (command.equals("C") || command.equals("BackSpace"))
- text.setText("");
-
- else if (command.equals("=")) {
- ans=new calc(s).result;
- text.setText(text.getText() + "="+ans);
- finish = true;
- }
-
- }
- });
- }
- this.setVisible(true);
- this.setLocation(500, 200);
- this.setSize(500, 400);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- }
-
- }
- class calc {
- Stack<String> ops = new Stack<String>();
- Stack<Double> num = new Stack<Double>();
- String exp;
- double result;
-
- public calc(String s) {
- exp = s;
- calctor();
- }
-
- public void calctor() {
- String s[] = tranform(exp).split(" ");
- for (String string : s) {
- if (string.matches("\\d+") || string.matches("\\d+.\\d+")) {
- double n = Double.parseDouble(string);
- num.push(n);
- } else if (string.equals("+") || string.equals("-") || string.equals("/") || string.equals("*")) {
- double op1 = num.pop(), op2 = num.pop();
- switch (string) {
- case "+":
- num.push(op1 + op2);
- break;
- case "-":
- num.push(op2 - op1);
- break;
- case "*":
- num.push(op2 * op1);
- break;
- case "/":
- num.push(op2 / op1);
- break;
- default:
- break;
- }
-
- } else {
-
- }
- }
- result = num.peek();
- // System.out.println("ans= " + result);
-
- }
-
- public String tranform(String s) {
- s = s.replace("+", " + ");
- s = s.replace("-", " - ");
- s = s.replace("*", " * ");
- s = s.replace("/", " / ");
- String str[] = s.split(" ");
- String s1 = "";
- for (String string : str) {
- if (string.matches("\\d+") || string.matches("\\d+.\\d+")) {
- s1 = s1 + " " + string;
- } else {
- if (ops.isEmpty()) {
- ops.push(string);
- continue;// 跳过下面
- }
- String temp = ops.peek();
- while (priority(temp) >= priority(string)) {// 栈顶元素优先级高于或等于进栈元素,不断弹出栈顶元素
- s1 = s1 + " " + temp;
- ops.pop();
- if (ops.isEmpty())
- break;
- temp = ops.peek();
- }
- ops.push(string);
-
- }
- }
- while (!ops.isEmpty())
- s1 = s1 + " " + ops.pop();
- // System.out.println(s1);
- return s1;
-
- }
-
- public int priority(String s) {
- switch (s) {
- case "+":
- case "-":
- return 1;
- case "*":
- case "/":
- return 2;
-
- default:
- break;
- }
- return 0;
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。