日期:2014-05-20  浏览次数:20759 次

关于表达式求值的,求帮忙
废话不多说了,请各位先看代码
Java code

import javax.swing.JButton;

public class C24 {
    // 定义一个栈数组
        int stackArr[];
        // 定义栈的大小
        int maxsize;
        // 定义栈顶
        int top;
        // 初始化一个大小为size的栈
        char ch[]={'+','-','*','/','(',')','#'};//把符号转换成一个字符数组
        int f1[]={3,3,5,5,1,6,0};//栈内元素优先级
        int f2[]={2,2,4,4,6,1,0};//栈外的元素优先级
        int n=0;
        public C24()
        {
            maxsize = 100;
            stackArr = new int[maxsize];
            top =0;
        }
        public void push(int value) {
            stackArr[++top] = value;
        }

    // 出栈操作
        public int pop() {
            return stackArr[top--];
        }

        // 取栈顶元素
        public int peek() {
            return stackArr[top];
        }

        // 判断栈是否为空
        public boolean isEmpty() {
            return top == 0;
        }

        // 判断栈是否已满
        public boolean isFull() {
            return top == maxsize - 1;
        }
    public int cton(char c)
        {
           switch(c)
           {
               case '+':
                  return 0;
               case '-':
                  return 1;
               case '*':
                  return 2;
               case '/':
                  return 3;
               case '(':
                  return 4;
               case ')':
                  return 5;
               default:
                  return 6;
           }
        }


    public     char Compare(char c1,char c2)
        {
             int i1=cton(c1);
             int i2=cton(c2);//把字符变成数字
             if(f1[i1]>f2[i2])//通过原来设定找到优先级
                return '>';
             else if(f1[i1]<f2[i2])
                     return '<';
                  else
                      return '=';
        }
    public     int Operate(int a,int t,int b)
        {
            int sum;
            switch(t)
            {
                 case 0:
                    sum=a+b;
                    break;
                 case 1:
                    sum=a-b;
                    break;
                 case 2:
                    sum=a*b;
                    break;
                 default:
                    sum=a/b;
            }
           return sum;
        }

        public int EvaluateExpression(String s)
        {
           char c;
           int count=0;
           int i=0,sum=0;
           int k=1,j=1;//设置了开关变量
           int x,t,a ,b ;
           C24 OPTR=new C24();
           C24 OPND=new C24();
          OPTR.push(cton('#'));//0压入栈
         
           c=s.charAt(count++);
           while(c!='#'||ch[OPTR.peek()]!='#')
           {
              if(Character.isDigit(c))              //判断c是否为数字
              {
                  sum=0;
                  while(Character.isDigit(c))
                  {
                      if(j==0)              //是否进行数字串的转换判断,为0转换
                      {
                          sum=sum*10-(c-'0');//字符c转换成了对应的数字
                      }
                      else sum=sum*10+(c-'0');
                      c=s.charAt(count++);
                  }
                  OPND.push(sum);//当前c不为数字,则把之前的把数字串转化成十进制数字再压栈
                  j=1;                    //将数字串转化后压入栈后将其置1
              }
              else if(k!=0)
              {
                  switch(Compare(ch[OPTR.peek()],c))
                  {
                  case'<': OPTR.push(cton(c));//把它们整型化
                     
                      c=s.charAt(count++);
                    
                      break;
                  case'=': x=OPTR.pop();
                
                      c=s.charAt(count++);
                     
                      break;
                  case'>': t=OPTR.pop();
                     
                    b= OPND.peek();
                   
                     a= OPND.peek();//注意这里是谁先出栈
                     
                      OPND.push(Operate(a,t,b));
                  
                      break;
                  }
              }
          }
          return(OPND.peek());
        }
        
        public static void main(String args[])
        {
            System.out.print(new C24().EvaluateExpression("1+22*6*7-9#"));
            
        }

}