回 帖 发 新 帖 刷新版面

主题:解析算术表达示` 两位变复杂了`

用栈和解析算术表达示`  实现算出算术表达示`   下面以经实现了1位数的表达示的结算`  但是如果操作数是两位的话` 就复杂了`` 

import java.io.*;

 class read{
    private int maxsize;
    private char[] stackarry;
    private int pop;

    public read(int s){
       this.maxsize=s;
       stackarry = new char[maxsize];
       this.pop= -1;
   
    }
    
    public void push(char j)
    {
         stackarry[++pop]=j;
    }
    
    public char pop()
    {
        return stackarry[pop--];
    }
    
    public char peek()
    {
        return stackarry[pop];
    }
    public boolean isempty()
    {
        return (pop==-1);
    }
    public boolean isfull()
    {
         return (pop==maxsize-1);
    }
    public int size()
    {
        return this.pop +1;
    }
    public char peekn(int n)
    {
        return this.stackarry[n];
    }
    public void display(String s)
    {
        System.out.print(s);
        System.out.print("read(bottom->TOP): ");
        for(int j=0;j<this.size();j++){
            System.out.print(this.peekn(j));
            System.out.print(' ');
        }    
        System.out.println(" ");
    }
}

回复列表 (共5个回复)

沙发

class InTo
{
    private read  one;
    private String input;
    private String output="";
    
    public InTo(String in)
    {
        input =in;
        int size = input.length();
        one = new read(size);
    }
    
    public String doTrans()
    {
        for(int j=0;j<input.length();j++)
        {
            char ch = input.charAt(j);
            //one.display("FOR "+ch+" ");
            switch(ch)
            {
            case '+':
            case '-':
                 gotOper(ch,1);
                 break;
            case '*':
            case '/':
                 gotOper(ch,2);
                 break;
            case '(':
                one.push(ch);
                break;
            case ')':
                 gotParen(ch);   //转点
                 break;
            default:
                output = output+ ch;
                break;
            }
        }
        while(!one.isempty())
        {
            //one.display("while ");
            output = output +one.pop();
        }
        //one.display("END" );
        return output;
    } //end doTrands
    public void gotOper(char opThis, int prec1)
    {
        while(!one.isempty())
        {
            char opTop = one.pop();
            if(opTop=='(')
            {
                one.push(opTop);
                break;
            }else{
                int prec2;
                if (opTop == '+' || opTop=='-')
                    prec2=1;
                else
                    prec2=2;
                
                if(prec2<prec1)
                {
                    one.push(opTop);
                    break;
                }else{
                    output = output +opTop;
                }
            }
        }//End while
        one.push(opThis);
    }//End gotOper
    public void gotParen(char ch)
    {
        while(!one.isempty())
        {
            char cha= one.pop();
            if(cha =='(')
                break;
            else
                output = output +cha;
        }//END while
    }
}

板凳

class two
{
    private int maxsize;
    private int[] stackarry;
    private int pop;

    public two(int s){
       this.maxsize=s;
       stackarry = new int[maxsize];
       this.pop= -1;
   
    }
    
    public void push(int j)
    {
         stackarry[++pop]=j;
    }
    
    public int pop()
    {
        return stackarry[pop--];
    }
    
    public int peek()
    {
        return stackarry[pop];
    }
    public boolean isempty()
    {
        return (pop==-1);
    }
    public boolean isfull()
    {
         return (pop==maxsize-1);
    }
    public int size()
    {
        return this.pop +1;
    }
    public int peekn(int n)
    {
        return this.stackarry[n];
    }
    public void display(String s)
    {
        System.out.print(s);
        System.out.print("read(bottom->TOP): ");
        for(int j=0;j<this.size();j++){
            System.out.print(this.peekn(j));
            System.out.print(' ');
        }    
        System.out.println(" ");
    }
}
class Post
{
    private two Potwo;
    private String input;
    
    public Post(String s)
    {
        input = s;
    }
    public int doParse()   //求值
    {
        Potwo = new two(20);
        char ch;
        int j;
        int num1,num2,temp;
        
        for(j=0;j<input.length();j++)
        {
            ch = input.charAt(j);
            Potwo.display(""+ch+" ");

            if(ch >='0' && ch<='9')
            {
                Potwo.push((int)(ch-'0'));
            }
            else{
                num2=Potwo.pop();
                num1=Potwo.pop();
                    switch(ch)
                    {
                        case '+':
                            temp = num1+num2;
                            break;
                        case '-':
                            temp = num1-num2;
                            break;
                        case '*':
                            temp = num1*num2;
                            break;
                        case '/':
                            temp = num1/num2;
                            break;
                        default:
                            temp=0;
                    }
                Potwo.push(temp);
            }
        }
        temp= Potwo.pop();    
        return temp;    
    }
}

3 楼

class stack
{
 
    public static String getString()throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s;
    
    }
    
    public static void main(String[] args) throws IOException {
        String input = "" ;
        String temp = ""; 
        int temptwo=0;    
            System.out.println("input String :");
            System.out.flush();
            input = getString();
            
        InTo myInTo = new InTo(input);
        temp = myInTo.doTrans();
        
        System.out.println("OUTPOU STRING :"+temp);
        
        Post myPost = new Post(temp);
        temptwo = myPost.doParse();
        
        System.out.println("temp STRING :" + temptwo);
       }
}

4 楼

给你个思路,我们知道编译器有词法分析,句法分析。对于一个算术表达式的计算。至少应该分2步做。第一步应该将字符串分解成运算符和数(数是一个整体,最好将表示数的串转化为一个数),第二步才是表达式计算呢。不要把1-2步一块做,那样的话,如果数的格式稍微复杂一些。那么你的代码就要推倒从来了。如这个表达
式 10+-.56687e-1 在c语言中是完全符合规则的表达式,他表示 1+(-0.056768)=1-0.0566768,你怎么解析他?是不是太复杂了。

  我的建议: 
   step1,表达式检查,看看表达式是否合乎语法规则。如果是执行是,执行step2
   step2, 词法分析,将字符串分解成 运算符 和 数串,对数串将其转化为数。运算符号和数可以用1个链表表示,也可以分别用1个链表表示,也可以用一个2叉数来表
示。
   step3:表达式计算(句法分析),其中最重要的是考虑优先级。

5 楼

谢谢你的建设`  的确上面的代码存在很多的漏洞和BUG `  我看是要重新设计思路`

我来回复

您尚未登录,请登录后再回复。点此登录或注册