这学期java课的一个小作业,用java编一个小计算器。
个人认为要点在于:
1.计算机的布局,即按钮和输出框的布局需要知道怎么操作,按钮的大小和字体的大小颜色、不同布局中按钮的改变大小等等
2.用栈将中缀表达式转换为后缀表达式
3.用栈将后缀表达式计算出来
其他问题应该不大,因为只是涉及到简单的加减乘除运算。
我的这个计算器有以下功能:
1.支持加减乘除运算,支持括号功能,小数点功能。
2.支持删除一位的操作。
3.支持计算结果继续进行计算的功能。
4.点击=后原表达式消失,只出现结果,一般保留一位小数。
目前发现的缺陷:
1.负数开头无法实现
2.输入错误表达式后,必须要再输入一次正确表达式后,才能正常开始运算,找不着bug,挺奇怪的,希望老师看不到
等等
但是我都通过try{}catch{}将异常捕获,统一在界面输出操作“非法输入,请按两次AC后重新操作”。
以下是界面展示:
下面直接上代码了
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.*;public class Computerhomework extends JFrame implements ActionListener{JTextField textField = new JTextField("");String input = "";JButton b[] = new JButton[20];JPanel panel = new JPanel();JPanel panel1 = new JPanel();String name [] = {"9","8","7","D","AC","6","5","4","-","(","3","2","1","*",")","0",".","=","/","+"};Stack<String> houzhui = new Stack<>();public Computerhomework() {this.setTitle("Computer");this.setSize(600, 700);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public void board() {textField.setPreferredSize(new Dimension(600,80));textField.setFont(new Font("宋体",Font.PLAIN,30));textField.setEditable(false); panel1.add(textField);panel.setLayout(new GridLayout(4,4,5,5));panel1.setLayout(new FlowLayout(3));this.add(panel, BorderLayout.CENTER);this.add(panel1, BorderLayout.NORTH);for(int i=0;i<name.length;i++)//添加按钮{b[i] = new JButton(name[i]);b[i].setBackground(new Color(162,192,142));b[i].setForeground(Color.BLACK); //设置按键颜色b[i].setFont(new Font("宋体",Font.PLAIN,50));//设置字体格式panel.add(b[i]);//将按键添加到界面b[i].addActionListener(this);}}@Overridepublic void actionPerformed(ActionEvent e){int flag_feifa = 0;try {for(int i = 0; i<name.length; i++) {if(e.getSource() == b[i] && name[i] == "AC") {textField.setText("");input = "";}else if(e.getSource() == b[i] && name[i] == "D") {String t = textField.getText();String flag = t.substring(t.length()-1);if(flag.equals("+") || flag.equals("-") || flag.equals("*") || flag.equals("/") || flag.equals("(") || flag.equals(")")) {textField.setText(t.substring(0, t.length()-1));input = input.substring(0,input.length() - 3);}else {textField.setText(t.substring(0, t.length()-1));input = input.substring(0,input.length() - 1);}}else if(e.getSource() == b[i] && name[i] =="(") {textField.setText(textField.getText() + e.getActionCommand() );input +=e.getActionCommand() + " ";}else if(e.getSource() == b[i] && name[i] == ")") {textField.setText(textField.getText() + e.getActionCommand() );input += " "+e.getActionCommand();}else if(e.getSource() == b[i] && name[i] == "=") {System.out.println(input);String r = result(to_houzhui(input));input = r;textField.setText(r);}else if(e.getSource() == b[i] && (name[i] == "+" || name[i] == "-" || name[i]=="*" || name[i] =="/")) {textField.setText(textField.getText() + e.getActionCommand() );input += " " + e.getActionCommand() + " ";}else if(e.getSource() == b[i]) {textField.setText(textField.getText() + e.getActionCommand() );input += e.getActionCommand();}}}catch(Exception x) {textField.setText("非法输入,请按两次AC后重新操作");input = "";flag_feifa = 1;}}public String to_houzhui(String in) {String hou = "";String s[] = in.split(" ");//System.out.println(s.length);//for(String t:s) {//System.out.println(t);//}for(int i=0; i<s.length; i++) {//.getClass().toString()获取数据类型 1*1*1*(11+1-44*21/11)char flag = s[i].charAt(0); //取出字符串的第一个字符作为判断if(flag >= '0' && flag <= '9' && i==0) {hou += s[i]; //数字加入后缀表达式}else if(flag >= '0' && flag <= '9') {hou += " "+s[i]; //数字加入后缀表达式}else if(houzhui.empty() == true) {houzhui.push(s[i]); //栈为空直接入栈}else if(flag == '(') houzhui.push(s[i]);else if(houzhui.peek().equals("(")) {houzhui.push(s[i]); //栈顶为左括号,入栈}else if(flag == ')') {while(houzhui.peek().equals("(") != true) {hou += " "+houzhui.peek();houzhui.pop();}houzhui.pop(); //将(也pop掉}else if((flag == '+' || flag == '-') && (houzhui.peek().equals("*") || houzhui.peek().equals("/") || houzhui.peek().equals("+") || houzhui.peek().equals("-"))) {//低优先级遇到高优先级,高优先级出栈,低优先级入栈,平级也是先出再入String f = houzhui.peek();while((f.equals("+") || f.equals("-")||f.equals("*")||f.equals("/"))&&!houzhui.empty()) {hou += " " + f;houzhui.pop();if(!houzhui.empty())f = houzhui.peek();}houzhui.push(s[i]);}else if((flag == '*' || flag == '/' )&& (houzhui.peek().equals("+")|| houzhui.peek().equals("-"))) {//*/为这里最高阶的运算符,遇到低级压入栈12*(12+12-14*13)houzhui.push(s[i]);}else{//最高级遇到同级,先出后进String f = houzhui.peek();while((f.equals("*") || f.equals("/"))&&!houzhui.empty() ) {hou += " " + f;houzhui.pop();if(!houzhui.empty())f = houzhui.peek();}houzhui.push(s[i]);}}while(!houzhui.empty()) {hou += " "+houzhui.peek();houzhui.pop();}return hou;}public String result(String s) {double sum = 0;String item[] = s.split(" ");Stack<String> res = new Stack<>();for(int i=0; i<item.length; i++) {if(item[i].charAt(0)>'0' && item[i].charAt(0)<'9') {res.push(item[i]);}else if(item[i].equals("+")) {double x = Double.parseDouble(res.peek());res.pop();double y = Double.parseDouble(res.peek());res.pop();String z = String.valueOf(y+x);res.push(z);}else if(item[i].equals("-")) {double x = Double.parseDouble(res.peek());res.pop();double y = Double.parseDouble(res.peek());res.pop();String z = String.valueOf(y-x);res.push(z);}else if(item[i].equals("*")) {double x = Double.parseDouble(res.peek());res.pop();double y = Double.parseDouble(res.peek());res.pop();String z = String.valueOf(y*x);res.push(z);}else if(item[i].equals("/")) {double x = Double.parseDouble(res.peek());res.pop();double y = Double.parseDouble(res.peek());res.pop();String z = String.valueOf(y/x);res.push(z);}}return res.peek();}public static void main(String[] args) {new Computerhomework().board();}}