找回密码
 注册
搜索
热搜: java php web
查看: 419|回复: 1

表达式求值算法

[复制链接]
发表于 2009-1-26 08:31:37 | 显示全部楼层 |阅读模式
谁有JAVA编的,表达式求值算法,拿来分享一下?下面是我参照资料搞的一个,存在不少问题?

import java.io.*;
class StackX
   {
   private int maxSize;
   private char[] stackArray;
   private int top;
   public StackX(int s)
     {
      maxSize = s;
      stackArray = new char[maxSize];
      top = -1;
     }
   public void push(char j)
      { stackArray[++top] = j; }

   public char pop()
      { return stackArray[top--]; }

   public boolean isEmpty()
      { return (top == -1); }

   public int size()
      { return top+1; }
   }
class  Tox
{
   private StackX theStack;
   private String input;
   private String output = "";
   public Tox(String in)
      {
      input = in;
      int stackSize = input.length();
      theStack = new StackX(stackSize);
      }
public boolean  check()     
     {     
      int stackSize = input.length();
        StackX theStack = new StackX(stackSize);
        for(int j=0; j<input.length(); j++)
         {     
           char ch = input.charAt(j);
           switch(ch)     
              {     
              case '{':
              case '[':     
              case '(':     
                 theStack.push(ch);
                 break;     
              case '}':
              case ']':     
              case ')':     

               if( !theStack.isEmpty() )
                    {     
                    char chx = theStack.pop();
                    if( (ch=='}' && chx!='{') ||     
                        (ch==']' && chx!='[') ||     
                        (ch==')' && chx!='(') )     
                       System.out.println("Error: "+ch+" at "+j);     
                    }     
                    else  
                    System.out.println("Error: "+ch+" at "+j);     
                 break;     
              default:
                 break;     
              }
           }   
        if( !theStack.isEmpty() )     
        System.out.println("Error: missing right delimiter,please check it.");
        //System.exit(0);   
        return false;
        }   

   public String Tr()
      {
  for(int j=0; j<input.length(); j++)
         {
         char ch = input.charAt(j);
         switch(ch)
            {
            case '+':
            case '-':
               gotOper(ch, 1);
               break;
            case '*':
            case '/':
               gotOper(ch, 2);
               break;
            case '(':
               theStack.push(ch);
               break;
           case ')':
               gotParen(ch);
               break;
            default:
               output = output + ch;
               break;
            }
         }
      while( !theStack.isEmpty() )
         {
        output = output + theStack.pop();
         }
      return output;
      }

public void gotOper(char opThis, int prec1)
      {
      while( !theStack.isEmpty() )
         {
         char opTop = theStack.pop();
         if( opTop == '(' )
            {
            theStack.push(opTop);
            break;
            }
         else
            {
            int prec2;
          if(opTop=='+' || opTop=='-')
               prec2 = 1;
            else
               prec2 = 2;
            if(prec2 < prec1)
               {
               theStack.push(opTop);
               break;
               }
            else
              output = output + opTop;
            }
         }
      theStack.push(opThis);
      }

public void gotParen(char ch)
      {
      while( !theStack.isEmpty() )
         {
         char chx = theStack.pop();
         if( chx == '(' ) break;
         else  output = output + chx;
         }
      }
   }
class Stacky
{
        private int maxSizey;
        private double[] ystackArray;
        private int top;

      public Stacky(int size)
   {
     maxSizey=size;
     ystackArray=new double[maxSizey];
     top=-1;
   }
     public void push(double j)
   {  ystackArray[++top]=j;}

     public double pop()
   { return ystackArray[top--];}
   
     public boolean isEmpty()
   {  return (top==-1);}

     public boolean isFull()
   { return (top==maxSizey-1);}

     public int size()
   { return top+1;  }
}

class ParsePost
{
    private Stacky theStacky;
    private String inputy;
   
    public ParsePost(String output)
    { inputy=output;}

     public double dOParse()
   {
                theStacky =new Stacky(20);
                char ch;
                int j;
                double num1,num2,interAns;
      for(j=0;j<inputy.length();j++)
    {
        ch=inputy.charAt(j);
        if(ch>='0'&&ch<='9')
           theStacky.push((double)(ch-'0'));
        else{
             num2=theStacky.pop();
             num1=theStacky.pop();
        switch(ch){
                case '+':
                        interAns=num1+num2;
                        break;
                case '-':
                        interAns=num1-num2;
                        break;
                case '*':
                        interAns=num1*num2;
                        break;
                case '/':
                               interAns=num1/num2;
                        break;
                         default :

                        interAns=0;
               }
               theStacky.push(interAns);
            }
          }
            interAns=theStacky.pop();
            return interAns;
       }
}
class ggg{
public static void main(String [] args) throws IOException
  {  String input, output;
     double outputy;   
     while(true)
    {           
      System.out.print("Enter biaodashi: ");
      System.out.flush();
      input = getString();
      if( input.equals("") );
      Tox fx = new Tox(input);
      if( fx.check()!=false)return;
      output = fx.Tr().toString();
      ParsePost aParser=new ParsePost(output);
      outputy=aParser.dOParse();
      System.out.println("yunsuanjiguo: " + outputy);
     }
  }
  public static String getString() throws IOException
{    InputStreamReader isr=new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      String s = br.readLine();
      return s;
  }
}
发表于 2009-1-26 10:29:35 | 显示全部楼层
这个???应该不是很复杂吧?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|软晨网(RuanChen.com)

GMT+8, 2024-9-20 15:18

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表