java实现简易整数计算器

用java实现的简单整数计算器

##em…这个又是老师布置的作业,话不多说,直接上图片和代码!
java实现简易整数计算器_第1张图片

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Java_494_9 extends JFrame {
    private JPanel jPanel1,jPanel2;
    private JTextField field;
    private JButton btns[] = new JButton[16];
    private String names[] = {"7","8","9","/","4","5","6","*","1","2","3","-","0","C","=","+"};
    private String str = new String();
    private char op[][] = {
            {'>','>','<','<'},
            {'>','>','<','<'},
            {'>','>','>','>'},
            {'>','>','>','>'}
    };
    public Java_494_9(){
        super("Calculator");
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        field = new JTextField(25);
        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        jPanel1.setLayout(new BorderLayout(1,1));
        jPanel1.add(field);
        jPanel2.setLayout(new GridLayout(4,4));
        for (int i = 0; i < 16; i++) {
            btns[i] = new JButton(names[i]);
            int finalI = i;
            if (i!=13&&i!=14) {
                btns[i].addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        str += names[finalI];
                        field.setText(str);
                    }
                });
            }
            else if (i==13){
                btns[i].addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        str = "";
                        field.setText(str);
                    }
                });
            }
            else if (i==14){
                btns[i].addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        char input[] = str.toCharArray();
                        int i = 0;
                        sequenceStack OPTR = new sequenceStack();
                        sequenceStack OPND = new sequenceStack();
                        while (i': c = OPTR.pop();
                                                  b = OPND.pop();
                                                  a = OPND.pop();
                                                  OPND.push(Operate(a,b,c));
                                                  break;
                                    }

                                }
                            }
                        }
                        int result = OPND.getHead();
                        str = ""+result;
                        field.setText(str);
                    }
                });
            }
            jPanel2.add(btns[i]);
        }
        container.add(jPanel1,BorderLayout.NORTH);
        container.add(jPanel2);
        setSize(300,300);
        setVisible(true);
    }
    public boolean isOperator(char c){
        switch (c){
            case '+':return true;
            case '-':return true;
            case '*':return true;
            case '/':return true;
            default:return false;
        }
    }
    public int getOpIndex(char c){
        int flag = 1;
        switch (c){
            case '+':flag = 0;break;
            case '-':flag = 1;break;
            case '*':flag = 2;break;
            case '/':flag = 3;break;
        }
        return flag;
    }
    public int Operate(int a,int b,char c){
        int result = 0;
        switch (c){
            case '+':result = a+b;break;
            case '-':result = a-b;break;
            case '*':result = a*b;break;
            case '/':result = a/b;break;
        }
        return result;
    }

    public static void main(String[] args) {
        Java_494_9 java_494_9 = new Java_494_9();
        java_494_9.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

其中用到了栈结构,下面附上sequenceStack类的代码:

public class sequenceStack {
    final int MaxSize = 10;
    private int top;
    private T[] stackArray;
    public sequenceStack(){
        top = -1;
        stackArray = (T[]) new Object[MaxSize];
    }
    public sequenceStack(int n){
        top = -1;
        if (n<=0){
            System.exit(1);
        }
        stackArray = (T[]) new Object[n];
    }
    public void push(T t){
        if (top<=stackArray.length-1){
            top++;
            stackArray[top] = t;
        }
    }
    public T pop(){
        if (top==-1){            
            return null;
        }
        else {
            return stackArray[top--];
        }
    }
    public T getHead(){
        if (top==-1){
            return null;
        }
        return stackArray[top];
    }
    public boolean isEmpty(){
        return top==-1;
    }
    public int size(){
        return top+1;
    }
    public void nextOrder(){
        for (int i = top; i <=0 ; i--) {
            System.out.println(stackArray[i]);
        }
    }
    public void  clear(){
        top=-1;
    }

}

大家一起努力哦~~

你可能感兴趣的:(java实现简易整数计算器)