小型计算器(JAVA)

package 作业;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Stack;//一开始就把所有的类写在一起,后面就不好拆分了(这不是我的锅,祭天比较好)
public class Calculator extends JFrame{//计算器类
    static Object []Option={"确定"};
    static boolean Flag=false;
    static boolean BABA=false;
    static String Nit=new String("");
    static JPanel Pa,Pb,Pc;
    static JMenuItem Mc1=new JMenuItem("文件"),Mc2=new JMenuItem("退出"),Mc3=new JMenuItem("关于");
    static JMenu Mb1=new JMenu("文件"),Mb2=new JMenu("帮助");
    static JMenuBar Ma1=new JMenuBar();
    static JButton []Lan=new JButton[22];
    static String []Name={"1","2","3","4","5","6","7","8","9","0","+","-","*","÷","+/-"
            , "1/x","sqrt","%","=",".","Back","C"};
    static int []Rank={20,21,6,7,8,13,3,4,5,12,0,1,2,11,9,14,19,10,15,17,16,18};
    static int []TLE=new int[22];
    static JTextField Wo=new JTextField(Nit);
    public Calculator() {
        //初始化
        super("计算器");
        for(int i=0;i<22;i++)TLE[Rank[i]]=i;
        for(int i=0;i<22;i++){Lan[i]=new JButton(Name[Rank[i]]);Lan[i].addActionListener(
                new Action());}
        //构建选项菜单
        Mb1.add(Mc1);
        Mb1.add(Mc2);
        Mb2.add(Mc3);
        Ma1.add(Mb1);Ma1.add(Mb2);
        this.setJMenuBar(Ma1);
        Mc1.addActionListener(new Action2());
        Mc2.addActionListener(new Action2());
        Mc3.addActionListener(new Action2());
        //设置面板布局
        Pa=new JPanel();Pa.setLayout(new GridLayout(1,1));
        Pb=new JPanel();Pb.setLayout(new GridLayout(1,2));
        Pc=new JPanel();Pc.setLayout(new GridLayout(5,4));
        Pa.add(Wo);
        Pb.add(Lan[0]);Pb.add(Lan[1]);
        for(int i=2;i<22;i++)Pc.add(Lan[i]);
        this.add(Pa,BorderLayout.NORTH);
        this.add(Pb,BorderLayout.CENTER);
        this.add(Pc,BorderLayout.SOUTH);
        //设置面板大小,位置和状态
        this.setSize(350,250);
        this.setFont(new Font("System", Font.PLAIN, 14));
        this.setLocation(20,100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    //按钮查找函数
    public JButton Find(String HK){
        for(int i=0;i<22;i++)
            if(HK.equals(Name[i]))return Lan[TLE[i]];
        return null;
    }
    //监听器1
    class Action implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(BABA)return;
            JButton TP=(JButton)e.getSource();
            if(Flag){
                for(int i=0;i<10;i++)if(TP==Find(Integer.toString(i))){Nit="";Flag=false;}
                Flag=false;
                }
            if(TP==Find("=")){Nit=Count(Nit);Wo.setText(Nit);Flag=true;}
            else if(TP==Find("C")){Nit="";Wo.setText(Nit);}
            else if(TP==Find("Back")){
                if(Nit.length()!=0){
                Nit=Nit.substring(0,Nit.length()-1);
                Wo.setText(Nit);
                }
            }
            else if(TP==Find("1/x")){
                double x=Double.valueOf(Count(Nit));
                if(x!=0.0){
                    String y=Double.toString(1.0/x);
                    Nit=y;
                    Wo.setText(y);
                    Flag=true;
                }
            }
            else if(TP==Find("sqrt")){
                double x=Double.valueOf(Count(Nit));
                String y=Double.toString(Math.sqrt(x));
                Nit=y;
                Wo.setText(y);
                Flag=true;
            }
            else if(TP==Find("+/-")){
                double x=Double.valueOf(Count(Nit));
                String y;
                if(x==(double)((int)x))y=Integer.toString((int)(-x));
                else y=Double.toString(-x);
                Nit=y;
                Wo.setText(y);
                Flag=true;
            }
            else for(int i=0;i<22;i++)if(TP==Find(Name[i])){
                Nit+=Name[i];Wo.setText(Nit);
            }
        }
    }
    //监听器2
    class Action2 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            JMenuItem TP=(JMenuItem)e.getSource();
            if(TP==Mc1){Nit="愚蠢的家伙,这是禁忌,你触碰了禁忌,重启吧";Wo.setText(Nit);BABA=true;}
            else if(TP==Mc2){System.exit(0);}
            else {
JOptionPane.showOptionDialog(null, "请查看帮助文档","帮助",JOptionPane.DEFAULT_OPTION
        , JOptionPane.WARNING_MESSAGE, null, Option, Option[0]);
            }
        }
    }
    //辅助计算函数
    double Cou1(double x,double y,char T){
        if(T=='*')return x*y;
        else if(T=='÷')if(x!=0)return y/x;else Wo.setText(Nit="Error");
        else {
            int xx=(int)x;int yy=(int)y;
            if(xx!=0)return yy%xx;
            else Wo.setText(Nit="Error");
        }
        return 0.0;
    }
    //计算函数
    String Count(String Init){
        Stack<Double>s1=new Stack<Double>();
        Stack<String>s2=new Stack<String>();
        boolean PP=false;
        boolean QQ=false;
        double ans=0.0;
        for(int i=0;i<Init.length();i++){
            if(Nit=="Error")return "Error";
            int leng=0;
            char T=Init.charAt(i);
            if(T=='*'||T=='÷'||T=='%'){
                s1.push(ans);ans=0.0;leng=0;PP=false;
                if(s1.size()!=s2.size()+1)return "Error";
                if(QQ)s1.push(Cou1(s1.pop(),s1.pop(),s2.pop().charAt(0)));
                s2.push(String.valueOf(T));
                QQ=true;
            }
            else if(T=='+'||T=='-'){
                s1.push(ans);ans=0.0;leng=0;PP=false;
                if(QQ){
                    if(s1.size()!=s2.size()+1)return "Error";
                    s1.push(Cou1(s1.pop(),s1.pop(),s2.pop().charAt(0)));
                    QQ=false;
                }
                s2.push(String.valueOf(T));
            }
            else if(T=='.')PP=true;
            else {
                double Change=(T-'0')*1.0;
                if(PP){
                    leng++;
                    for(int j=0;j<leng;j++)Change/=10.0;
                    ans+=Change;
                }
                else {
                    ans=ans*10.0+Change;
                }
                if(i==Init.length()-1){
                    s1.push(ans);ans=0.0;
                    if(QQ)s1.push(Cou1(s1.pop(),s1.pop(),s2.pop().charAt(0)));
                }
            }
        }
        if(s1.size()!=s2.size()+1)return "Error";
        if(s2.empty()){
        double beta=s1.pop();
        if(beta==(double)((int)beta))return Integer.toString((int)beta);
        else return Double.toString(beta);
        }
        char []TOK=new char[s2.size()];
        double []Num=new double[s1.size()];
        int FFT=s2.size();
        for(int j=s2.size()-1;j>=0;j--){TOK[j]=s2.pop().charAt(0);}
        for(int j=s1.size()-1;j>=0;j--){Num[j]=s1.pop();}
        ans=Num[0];for(int j=0;j<FFT;j++){
            if(TOK[j]=='+')ans+=Num[j+1];
            else ans-=Num[j+1];
        }
        if(ans==(double)((int)ans))return Integer.toString((int)ans);
        else return Double.toString(ans);
    }
    //主方法
    public static void main(String[] args) {
           Calculator C=new Calculator();       
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值