5

简单计算器Java代码非法表达式问题问题,求大神指点

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

public class Jisuanqi extends JFrame implements ActionListener{
//计算器控件键

JFrame Jisuan = new JFrame("计算器");

private  final String KEYS[] = {"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","=",
    "Exp","pi","sin","x^y","log","cos","x^3","n!","tan","x^2"};          
private  final String M[] = {"MC","MR","MS","M+"};
private  final String COM[] = {"Backspace","CE","C"};
private JButton keys[] = new JButton(KEY.length);  
private JButton com[] = new JButton(COM.length);
private JButton m[] = new JButton(M.length);

private JTextField result;
MenuBar menubar=new MenuBar();  
Menu edit=new Menu("编辑");
Menu help=new Menu("帮助");
MenuItem copy=new MenuItem("复制");
MenuItem paste=new MenuItem("粘贴");
MenuItem about=new MenuItem("关于计算器(A)");  
private boolean firstDigit = true;   //判断第一个输入的字符是否是数字
private double resultNum = 0.0;      //中间结果
private boolean operateValidFlag = true;
private String operator = "=";
private String c,s;

//初始化界面
void init(){
    result = new JTextField("0");
    result.setHorizontalAlignment(JTextField.RIGHT); //文字右对齐
    result.setEditable(false);        //设置为不可编辑
    result.setBackground(Color.white);   //设置颜色为白色

    JPanel Kpanel = new JPanel();
    Kpanel.setLayout(new GridLayout(6,5,5,5));   //六行五列

    for(int i = 0;i < KEYS.length;i++)
    {
        keys[i] = new JButton(KEYS[i]);
        Kpanel.add(keys[i]);
        keys[i].setForeground(Color.blue);
    }

    keys[3].setForeground(Color.red);
    keys[8].setForeground(Color.red);
    keys[13].setForeground(Color.red);
    keys[18].setForeground(Color.red);
    for(int i=19;i<30;i++){
       keys[i].setForeground(Color.red);
    }

    JPanel Cpanel = new JPanel();
    Cpanel.setLayout(new GridLayout(1,3,5,5));
    for(int i = 0;i < COM.length;i++)
    {
        com[i] = new JButton(COM[i]);
        Cpanel.add(com[i]);
        com[i].setForeground(Color.red);
    }

    JPanel Mpanel = new JPanel();
    Mpanel.setLayout(new GridLayout(4,1,5,5));
    for(int i = 0;i < M.length;i++)
    {
        m[i] = new JButton(M[i]);
        Mpanel.add(m[i]);
        m[i].setForeground(Color.red);
    }       
}

//构造函数

public Jisuanqi(){
    //界面初始化
    init();     
    //添加界面按键监听
    for(int i = 0;i < KEYS.length;i++){
        keys[i].addActionListener(this);
    }       
    for(int i = 0;i < COM.length;i++){
        com[i].addActionListener(this);
    }       
    for(int i = 0;i < M.length;i++){
        m[i].addActionListener(this);
    }

    //复制事件
    copy.addActionListener(new ActionListener()
        {
             public void actionPerformed(ActionEvent e)
                {
                   int i;
                   i=s.length();
                   if(i!=0)
                   c=result.getText();
                   else
                   System.out.println("没有可复制的对象");
                }
        });
    //粘贴事件
    paste.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
                {
                    s=c;
                    result.setText(s);
                }
        });
    help.addActionListener(new ActionListener()
    {
        JOptionPane.showMessageDialog(this,"计算器","帮助",JOptionPane,INFORMATION_MESSAGE);
    }); 
    edit.add(copy);
    edit.add(paste);
    help.add(about);

    menubar.add(edit);
    menubar.addSeparator();
    menubar.add(help);


    //整体布局
    JPanel p1 = new JPanel();
    p1.setLayout(new BorderLayout());
    p1.add("North",Cpanel);
    p1.add("Center",Kpanel);
    JPanel p2 = new JPanel();
    p2.setLayout(new BorderLayout());
    p2.add("Center",result);
    Jisuan.setLayout(new BorderLayout(3,5));
    Jisuan.add("North",p2);
    Jisuan.add("Center",p1);
    Jisuan.add("West",Mpanel);
    Jisuan.setBackground(Color.LIGHT_GRAY);
    Jisuan.setLocation(200,200);
    Jisuan.setMenuBar(menubar);
    Jisuan.setVisible(true);
    Jisuan.setResizable(false);//不可最大化
    Jisuan.pack();      //紧凑
    Jisuan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
    String str = e.getActionCommand();//获取事件源的标签
    if(str.equals(COM[0]))//按Backspace
        Backspace();
    else if(str.equals(COM[1]))//按CE
        result.setText("0");
    else if(str.equals(COM[2]))//按C
        C();
    else if(str.equals(M[0]))//按MC
        MC();
    else if(str.equals(M[1]))//按MR
        MR();
    else if(str.equals(M[2]))//按MS
        MS();
    else if(str.equals(M[3]))//按M+
        M();
    else if("0123456789.".indexOf(str) >= 0)//按数字键或者小数点
        number(str);
    else//按运算符
        operate(str);
}

//处理Backspace
private void Backspace(){
    String text = new String(result.getText());
    int i = text.length();
    if( i > 0){
        text = text.substring(0,i - 1);
        if(text.length() == 0){
            result.setText("0");
            firstDigit = true;
            operator = "=";
        }
        else{
            result.setText(text);
        }
    }
}
//处理C
private void C(){
    result.setText("0");
    firstDigit = true;
    operator = "=";
}
//处理数字键
private void number(String key){
    if(firstDigit){
        result.setText(key);
    }
    else if((key.equals("."))&&(result.getText().indexOf(".")<0)){
        result.setText(result.getText() + ".");     
    }
    else if(!key.equals(".")){
        result.setText(result.getText() + key);
    }
    firstDigit = false;
}       
//处理MC,清除内存
public void MC(){
    resultNum = 0.0;
    result.setText("0");
    m[0].setText(" ");
    firstDigit = true;
    operator = "=";
}
//处理MR,重新调用存储的数据
public void MR()
{
    //有记忆数字
    if(result.getText() != "" ){
        result.setText("" + resultNum);
    }
}
//处理MS,存储显示的数据
public void MS(){           
    resultNum = Double.valueOf(result.getText()).doubleValue();
    m[0].setText("M");
    result.setText("0");
    firstDigit = true;
    operator = "=";
}
//处理M+,将显示的数字与已经存储的数据相加要查看新的数字单击MR
public void M(){
    resultNum = resultNum + Double.valueOf(result.getText()).doubleValue();
}
//处理运算符
private void operate(String key){
    if(operator.equals("/")){
        if(getNumberFromText() == 0.0){
            operateValidFlag = false;
            result.setText("除数不能是零");
        }
        else
            resultNum /= getNumberFromText();
    }
    else if(operator.equals("1/x")){
         if(resultNum == 0.0){
            operateValidFlag = false;
            result.setText("零没有倒数");
        }
        else
            resultNum = 1 / resultNum;
    }
    else if(operator.equals("+"))
        resultNum += getNumberFromText();
    else if(operator.equals("-"))
        resultNum -= getNumberFromText();
    else if(operator.equals("*"))
        resultNum *= getNumberFromText();
    else if(operator.equals("sqrt"))
        resultNum = Math.sqrt(resultNum);/////////
    else if(operator.equals("%"))
        resultNum = resultNum / 100;
    else if(operator.equals("+/-"))
        resultNum = resultNum * (-1);
    else if(operator.equals("=")){  
        resultNum = getNumberFromText();
    }
    else if(str.equals("pi")){
            str.setText(""+3.141592653);
    }
    else if(operate.equals("sin")){
        double i= getNumberFromText();
        result.setText(String.valueOf(Math.sin(i)));
    }
    else if(operator.equals("x^y")){
        if(getNumberFromText() == 0.0||getNumberFromText() == 1){
            resultNum = getNumberFromText();
        }
        else{
            for(int i=1;i<=getNumberFromText();i++){
            resultNum2 *=resultNum;
        }
        result.setText(String.valueOf(resultNum));
    }
    if(operator.equals("log")){
        double i= getNumberFromText();
        resultNum =Math.log(i);
        result.setText(String.valueOf(resultNum));
    }
    else if(operator.equals("cos")){
        double i= getNumberFromText();
        resultNum =Math.cos(i);
        result.setText(String.valueOf(resultNum));
    }
    else if(operator.equals("tan")){
        double i= getNumberFromText();
        resultNum =Math.tan(i);
        result.setText(String.valueOf(resultNum));
    }
    else if(operator.equals("Exp")){
        double i= getNumberFromText();
        resultNum =Math.exp(i);
        result.setText(String.valueOf(resultNum));
    }       
    else if(operator.equals("x^3")){
        if(getNumberFromText() == 0.0||getNumberFromText() == 1){
            resultNum= getNumberFromText();
        }
        else{
            resultNum= getNumberFromText()*getNumberFromText()*getNumberFromText();
        }
        result.setText(String.valueOf(resultNum));
    }
    else if(operator.equals("n!")){
        double i = getNumberFromText();
        //判断为整数放进行阶乘操作
        if((i%2==0)||(i%2==1)){
            int j = (int)i;  //强制类型转换
            int resultNum=1;
            for(int k=1;k<=j;k++)
                    resultNum *= k;
            result.setText(String.valueOf(resultNum));
        }
        else{
            result.setText("无法求阶乘");
        } 
    }
    else if(operator.equals("x^2")){
        if(getNumberFromText() == 0.0||getNumberFromText() == 1){
             resultNum = getNumberFromText();
        }
        else{
             resultNum = getNumberFromText()*getNumberFromText();
        }
        result.setText(String.valueOf(resultNum));
    }
    // 双精度浮点数的运算
    if(operateValidFlag){
        long t1;
        double t2;
        t1 = (long) resultNum;
        t2 = resultNum - t1;
        if(t2 == 0 ){
            result.setText(String.valueOf(t1));
        }
        else{
            result.setText(String.valueOf(resultNum));
        }
    }
    //运算符等于用户按的按钮
    operator = key;
    firstDigit = true;
    operateValidFlag = true;
}
//从结果文本框中获取数字
private double getNumberFromText(){
    double res = 0;
    try{
        res = Double.valueOf(result2.getText()).doubleValue();
    }
    catch(NumberFormatException e){}
    return res;
}
public static void main(String args[]){
    Jisuanqi c2=new Jisuanqi();
}

}

查看全部
qq_22188225
苏苏Ciel
2015/07/01 08:59
  • java
  • 点赞
  • 收藏
  • 回答
    私信

2个回复