按等号按钮说app停止工作

时间:2014-01-27 17:35:22

标签: android android-intent

我的按钮转到InfixtoPostfix课程,然后应该显示结果。但是,它显示应用程序已停止。

public class InfixtoPostfix extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        TextView tv=(TextView) findViewById(R.id.textview_display);
        String s = null;
         String p;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader br=new BufferedReader(new OutputStreamReader(System.out));
        Stack b=new Stack();

        try {
            s=br.readLine();

        //  s=tv.getText().toString();
        } catch (IOException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("inn");
        p=b.postfix(s);       

        Evaluation e=new Evaluation(); 
        tv.setText("Result:- "+e.calculate(p)); 

    }

    class Stack1  
    {  
       private int[] a;  
       private int top,m;  
       public Stack1(int max)  
       {  
         m=max;  
         a=new int[m];  
         top=-1;  
       }  
       public void push(int key)  
       {  
         a[++top]=key;  
       }  
       public int pop()  
       {  
         return(a[top--]);  
       }  
    }  
    class Evaluation{  
           public int calculate(String s)  
           {  
             int n,r=0;  
             n=s.length();  
             Stack1 a=new Stack1(n);  
             for(int i=0;i<n;i++)  
             {  
               char ch=s.charAt(i);  
               if(ch>='0'&&ch<='9')  
                 a.push((int)(ch-'0'));  
               else  
               {  
                 int x=a.pop();  
                 int y=a.pop();  
                 switch(ch)  
                 {  
                   case '+':r=x+y;  
                      break;  
                   case '-':r=y-x;  
                      break;  
                   case '*':r=x*y;  
                      break;  
                   case '/':r=y/x;  
                      break;  
                   default:r=0;  
                 }  
                 a.push(r);  
               }  
             }  
             r=a.pop();  
             return(r);  
           }  
        }  

}
package com.example.calculator;

public class Stack {

     char stack1[]=new char[20];
        int top;
        void push(char ch)
        {
            top++;
            stack1[top]=ch;
        }
        char pop()
        {
            char ch;
            ch=stack1[top];
            top--;
            return ch;
        }
        int pre(char ch)
        {
            switch(ch)
            {
                case '-':return 1;
                case '+':return 1;
                case '*':return 2;
                case '/':return 2;
            }
            return 0;
        }
        boolean operator(char ch)
        {
            if(ch=='/'||ch=='*'||ch=='+'||ch=='-')
                return true;
            else
                return false;
        }
        boolean isAlpha(char ch)
        {
            if(ch>='a'&&ch<='z'||ch>='0'&&ch=='9')
                return true;
            else
                return false;
        }
        String postfix(String str)
        {
            char output[]=new char[str.length()];
            char ch;
            int p=0,i;
            for(i=0;i<str.length();i++)
            {
                ch=str.charAt(i);   
                if(ch=='(')
                {
                    push(ch);
                }
                else if(isAlpha(ch))
                {
                    output[p++]=ch;
                }
                else if(operator(ch))
                {
                    if(stack1[top]==0||(pre(ch)>pre(stack1[top]))||stack1[top]=='(')
                {
                    push(ch);
                }
                }
                else if(pre(ch)<=pre(stack1[top]))
                {
                    output[p++]=pop();
                    push(ch);
                }
                else if(ch=='(')
                {
                    while((ch=pop())!='(')
                    {
                        output[p++]=ch;
                    }
                }
            }

            while(top!=0)
            {
                output[p++]=pop();
            }
            return output.toString();


        }

}

0 个答案:

没有答案