为什么如果(2> 1){System.out.println(“hello”);}不起作用?

时间:2015-07-19 09:18:41

标签: java

中缀到后缀转换的问题

  

在获取值为' - '时,编译器不会输入else-if块(最后一种情况)

package com.conversion;

import java.util.Scanner;


public class mainclass {

    private static Scanner sc;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String s;

        sc = new Scanner(System.in);
        System.out.println("Enter a complete expresion");
        s= sc.next();

        stackop ob = new stackop(); 

        System.out.println("YOUR PROVIDED input expression is :"+s);
        //System.out.print(" Equivalent postfix notation is :  ");
        System.out.println("\n\n");
        ob.postfix(s);

    }

}


class stackop{

    char stack1[]=new char[50];
    int top;

    void push(char ch)
        {
        top++;
        stack1[top]=ch; 
        System.out.println(" element pushed is: "+ch);

        System.out.println("----- current value of top is after push overs :  "+top+"  -------");

        }
    char pop(){
        char ch;
        ch= stack1[top];
        //System.out.println("current value of top is :  "+top);
        System.out.println(" element popped is: "+ch);
        --top;
        System.out.println("----- current value of top is after pop overs :  "+top+"  -------");
        return ch; 
    }

    boolean isalpha(char a)
        {
            if((a>='a' && a<='z')||(a>=0 && a<=9))
                return true;
            else
                return false;
        }
    boolean operator(char ch)
    {
        if(ch=='/'||ch=='*'||ch=='+'||ch=='-')
            return true;
        else 
            return false;
    }
    int prech(char ch){
            switch(ch)
            {
                case '-': return 1;
                //break;
                case '+': return 1;
                //break;
                case '/': return 2;
                //break;
                case '*': return 2;
            }
            return 0;
    }

    void postfix(String str)
    {

        char otop=0;
        char[] output = new char[str.length()+1];

        for(int i=0 ; i<str.length();i++)
        {
            char ch=str.charAt(i);
            System.out.println("==========value fetched is  "+ch+"    ===========");

            if(ch=='(')
                push(ch);
            else if(isalpha(ch))
            {
                System.out.println("elemnt inserted in output list is :"+ch);
                output[++otop]=ch;
            }
            else if(ch == ')' )
            {   
                char temp=0;
                System.out.println(" a close bracket  is encounterd  ");
                while((temp=pop())!='(')
                {
                    output[++otop]=temp;
                    System.out.println("elemnt inserted in output list is :"+temp);
                }
            }
            else if(operator(ch))
            {
                if(prech(stack1[top])==0||(prech(stack1[top])<=prech(ch))||(prech(stack1[top])=='('))
                    push(ch);
            }
            else if(prech(stack1[top]) > prech(ch))
            {
                System.out.println(" hahah  here i come");
                output[++otop]=pop();
                push(ch);
            }

        }
        while(top>0)
        {
            output[++otop]=pop();
            System.out.println("elemnt inserted in output list is :"+output[otop]);
        }
        System.out.println("\n\nOUTPUT OF EXPRESSION  IS  :   " );
        for(int j=0;j<str.length();j++)
        {
            System.out.print(""+output[j]);
        }
    }

}

2 个答案:

答案 0 :(得分:1)

标题和问题没有任何共同点,您也应格式化代码。尽管如此,如果你写了一个合适的标题,我可能会问你这个问题的答案。

在编程中实际上是0!='0',因为'0'被转换为它的整数值,在ASCII中为48,比较在0到48之间。 所以你的isalpha方法应该是这样的:

boolean isalpha(char a)
{
    if((a>='a' && a<='z')||(a>='0' && a<='9'))
        return true;
    else
        return false;
}

此外,还有一些简洁的小方法,您无需检查字符的ASCII代码来比较它们。我个人更喜欢这种方法,因为它更简单和简洁。它看起来像这样:

boolean isalpha(char a)
{
    return Character.isDigit(a) || Character.isAlphabetic(a);
}

答案 1 :(得分:1)

在这段代码中:

else if(operator(ch))
{
    if(prech(stack1[top])==0||(prech(stack1[top])<=prech(ch))||(prech(stack1[top])=='('))
                push(ch);
}
else if(prech(stack1[top]) > prech(ch))

operator(ch)方法为您的运算符返回true,这意味着控制流永远不会到达第二个else if。您可能想要做的是将其移到第一个else if内,如下所示:

else if(operator(ch))
{
    if(prech(stack1[top])==0||(prech(stack1[top])<=prech(ch))||(prech(stack1[top])=='('))
    {
        push(ch);
    }
    else if(prech(stack1[top]) > prech(ch)) { ... }
}