package com.test1;import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;import com.sun.org.apache.xpath.internal.operations.Mult;import java.math.*;import sun.org.mozilla.javascript.internal.ast.Block;
/**
 * 计算器
 * @author 顾海生
 *
 */
@SuppressWarnings("serial")
public class Calculator extends JFrame implements ActionListener{
@SuppressWarnings({ "unchecked", "unused" })
private static final ArrayList ArrayList = null;
//定义组件
JButton[] btns = new JButton[16] ;
JTextField jtf ;
JPanel jp,jp1,jp2;
double num;
double sum=0;
String i;
JButton qcbtn;

ArrayList<Character> arrarylist;   //存放运算符
@SuppressWarnings("unchecked")
ArrayList<Double> numlist;    //存放数字
public static void main(String[] args) {
// TODO Auto-generated method stub Calculator cal = new Calculator();
}
public  Calculator(){
arrarylist = new ArrayList();
numlist=new ArrayList();
jtf=new JTextField();
jtf.setFont(new Font("隶书",Font.BOLD,26));
jp = new JPanel();
jp1 = new JPanel();
jp2= new JPanel();
qcbtn=new JButton("清除");
qcbtn.addActionListener(this);
jp.setLayout(new BorderLayout());
jp1.setLayout(new GridLayout(4,3));
jp2.setLayout(new GridLayout(4,1));
for(int i=1;i<=9;i++){
btns[i]=new JButton(i+"");
btns[i].setFont(new Font("隶书",Font.BOLD,26));
btns[i].addActionListener(this);
jp1.add(btns[i]);


}
btns[0]=new JButton(0+"");
btns[0].setFont(new Font("隶书",Font.BOLD,26));
btns[0].addActionListener(this);
jp1.add(btns[0]);

btns[10]=new JButton(".");
btns[10].setFont(new Font("隶书",Font.BOLD,26));
btns[10].addActionListener(this);
jp1.add(btns[10]);

btns[11]=new JButton("+");
btns[11].setFont(new Font("隶书",Font.BOLD,26));
btns[11].addActionListener(this);
jp2.add(btns[11]);

btns[12]=new JButton("-");
btns[12].setFont(new Font("隶书",Font.BOLD,26));
btns[12].addActionListener(this);
jp2.add(btns[12]);

btns[13]=new JButton("*");
btns[13].setFont(new Font("隶书",Font.BOLD,26));
btns[13].addActionListener(this);
jp2.add(btns[13]);

btns[14]=new JButton("/");
btns[14].setFont(new Font("隶书",Font.BOLD,26));
btns[14].addActionListener(this);
jp2.add(btns[14]);

btns[15]=new JButton("=");
btns[15].setFont(new Font("隶书",Font.BOLD,26));
btns[15].addActionListener(this);
jp1.add(btns[15]);
jp.add(jp1,BorderLayout.CENTER);
jp.add(jp2,BorderLayout.EAST);
jp.add(qcbtn,BorderLayout.SOUTH);
this.add(jtf,BorderLayout.NORTH);
this.add(jp);

//界面设置

this.setTitle("计算器");
this.setSize(250,250);
this.setLocation(200, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);


}
@SuppressWarnings({ "deprecation", "unchecked" })
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(int i=0;i<=14;i++){
if(e.getSource()==btns[i]){
this.jtf.setText(this.jtf.getText()+btns[i].getText());

}

}
if(e.getSource()==btns[11]){
arrarylist.add('+');
}else if(e.getSource()==btns[12]){
arrarylist.add('-');
}else if(e.getSource()==btns[13]){
arrarylist.add('*');
}else if(e.getSource()==btns[14]){
arrarylist.add('/');
}
if(e.getSource()==btns[15]){

String str = jtf.getText();
// System.out.print(this.jtf.getText());
//将全部符号改为'+',并以此分割得到数字的数组
String[] s=str.replace('-', '+').replace('*', '+').replace('/', '+').split("[+]");

//将得到的数组转到numlist中
for(int i=0;i<s.length;i++){
numlist.add(Double.parseDouble(s[i]));
 System.out.print(numlist.get(i)+"  ");
}

for(int i=0;i<arrarylist.size();i++){
    switch((Character)arrarylist.get(i)){
        
    case '*': 
     double a1=numlist.get(i)*numlist.get(i+1);
         arrarylist.remove(i);
      numlist.remove(i);
      numlist.remove(i);     
      numlist.add(i, a1);
       System.out.print("*");
      break;
    
    case '/':
     double b2=numlist.get(i)/numlist.get(i+1);
      arrarylist.remove(i);
      numlist.remove(i);
      numlist.remove(i);
      numlist.add(i, b2);
       System.out.print("/");
      break;

    }

    

}

     for(int i=0;i<arrarylist.size();i++){
      switch((Character)arrarylist.get(i)){
        
//     case '+': 
//      double a=numlist.get(i)+numlist.get(i+1);
//       arrarylist.remove(i);
//       numlist.remove(i);
//       numlist.remove(i+1);
//       numlist.add(i, a);
//       break;
//     
    case '-':
     double a=numlist.get(i+1)*(-1);
     arrarylist.remove(i);
      numlist.remove(i+1);
      numlist.add(i+1, a);
//      double b=numlist.get(i)-numlist.get(i+1);
//       arrarylist.remove(i);
//       numlist.remove(i);
//       numlist.remove(i+1);
//       numlist.add(i, b);
      System.out.print("-");
      break;
    }
      
      
     }
  
           for(int i=0;i<numlist.size();i++){
          
            sum+=numlist.get(i);
          
           }
jtf.setText(sum+"");

}
if(e.getSource()==qcbtn){

    String str2=jtf.getText();
    str2="";
    this.jtf.setText(str2);
    System.out.print(this.jtf.getText());
}
}
}

解决方案 »

  1.   

    你的结果不对是因为没有*和/的优先运算。
    我大概的想法是这样建一个stack,然后开始从前到后遍历你的算式:
    1. 看到数字,
        * 栈头是空的,直接入栈
        * 栈头是*或/,出栈两次,必然是一个符号一个数,做运算后入栈。否则报错
        * 栈头是+或-,入栈
    2. 看到+或-
        * 栈头是数字,出栈一次看栈头
              > 如果是空,放数字回栈,再将符号入栈
              > 如果不是空,接着出栈两次(手上有3原来的栈元素和一个读到的符号),对栈元素做运算,放回栈,再将+或-入栈
        * 栈头是操作符或空,报错
    3. 看到*或/
        * 栈头是数字,入栈
        * 栈头是操作符或空,报错要是不明白再讨论吧。这个算法的核心原理是,*和/是优先级最高的,所以看到就可以直接计算,+和-的话,在其后如果还是+和-,那前面那一个就可以直接计算。
      

  2.   

    没看到上面还有一段这个好解决,你不要用for而用while,自己控制i在什么时候++