计算器

package apq.Calculate;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculate extends JFrame
    implements ActionListener{
    
    private JTextField display=new JTextField();
    private JButton[] buttons=new JButton[16]; 
    private String[] keys={"7","8","9","/",
                           "4","5","6","*",
                           "1","2","3","-",
                           "0","C","=","+"};
    //to store the 2 inputs;
    private String numStr1="";
    private String numStr2="";
    //to store the result or mid-result;
    private String rstStr="";
    //to define the first input is finished;
    private boolean symb=false;
    //to store the op;
    private char op;
    
    public Calculate(){
        //Create the Frame;
        setTitle("My Calculate");
        setSize(230,200);
        
        //Get the pane;
        Container pane=this.getContentPane();
        pane.setLayout(null);
        
        //add the display;
        display.setSize(200,30);
        display.setLocation(10,10);
        pane.add(display);

        //add the buttons;
        int x=10,y=40;
        for(int i=0;i<16;i++){
            buttons[i]=new JButton(keys[i]);
            buttons[i].setSize(50,30);
            buttons[i].setLocation(x,y);
            buttons[i].addActionListener(this);
            pane.add(buttons[i]);
            if(i%4!=3){
                x+=50;
            }
            else{
                x=10;
                y+=30;
            }
        }
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    //implements the interface
    public void actionPerformed(ActionEvent e){
        String str=String.valueOf(e.getActionCommand());
        char ch=str.charAt(0);
        switch(ch){
            case '1':case '2':case '3':
            case '4':case '5':case '6':
            case '7':case '8':case '9':case '0':
                if(!symb){
                    numStr1+=ch;
                    display.setText(numStr1);
                }
                else{
                    numStr2+=ch;
                    display.setText(numStr2);
                }
                break;
            case '/':case '*':case '-':case '+':
                op=ch;
                symb=true;
                break;
            case 'C':
                display.setText("");
                numStr1=numStr2="";
                symb=false;
                break;
            case '=':
                rstStr=evaluate();
                display.setText(rstStr);
                try{
                    int temp=Integer.parseInt(rstStr);
                }
                catch(NumberFormatException e2){
                    numStr1="";
                    numStr2="";
                    symb=false;
                    break;
                }
                numStr1=rstStr;
                numStr2="";
                break;
        }
    }

    //used to evaluate the result;
    private String evaluate(){
        int result=0;
        try{
            int num1=Integer.parseInt(numStr1);
            int num2=Integer.parseInt(numStr2);
            switch (op){
                case '+':result=num1+num2;
                break;
                case '-':result=num1-num2;
                break;
                case '*':result=num1*num2;
                break;
                case '/':result=num1/num2;
                break;
            }
            return String.valueOf(result);
        }
        catch(ArithmeticException e){
            return "ERROR:"+e.getMessage();
        }
        catch(NumberFormatException e){
            if(numStr1.equals(""))
                return "ERROR:Invalid First Number";
            else
                return "ERROR:Invalid Second Number";
        }
        catch(Exception e){
            return "ERROR";
        }
    }
    public static void main(String args[]){
        Calculate myCalculate=new Calculate();
    }
}


你可能感兴趣的:(C++,c,swing,C#)