首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助将后缀转换为使用堆栈的Infix

需要帮助将后缀转换为使用堆栈的Infix
EN

Stack Overflow用户
提问于 2019-03-27 05:24:54
回答 1查看 192关注 0票数 0

作为家庭作业的一部分,我编写了代码将后缀转换为完全括号大小的infix,但这段代码只能转换带有个位数的infix表达式。我需要帮助,以转换2或更多数字的infix表达式。

代码语言:javascript
复制
//Here's my code. My class doesn't use collection in JAVA.
//Classes and Interfaces for stack, list, and tree are provided.
private static final String DIGITS = "0123456789";

public static String convertPostfixtoInfix(String toPostfix)
{
    LinkedStack<String> s = new LinkedStack<>();

    for(int i=0; i<toPostfix.length(); i++)
    {
        if(DIGITS.indexOf(toPostfix.charAt(i)) != -1)
        {
            s.push(toPostfix.charAt(i)+"");
        }
        else if(toPostfix.charAt(i) == " ");{}//do nothing for blank.
        else
        {
            String temp = "";
            temp += toPostfix.charAt(i);

            String num1 = s.top();
            s.pop();
            String num2 = s.top();
            s.pop();
            s.push("(" + num2 + temp + num1 + ")");
        }
    }

    return s.top();//top() is same as peek() method.
}

例如,有了这些代码,

输入:4 5-9 2 1+/* 输出:(4-5)*(9/(2+1)) 输入:40 5-9 20 1+/* 输出:(9*(2/(0+1)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-27 06:12:51

这是你怎么做的。

首先,注意一点。这一行代码是多余的:

代码语言:javascript
复制
private static final String DIGITS = "0123456789";

如果您想检查一个字符是否是一个数字,您可以简单地用

代码语言:javascript
复制
Character.isDigit();

但为了简单起见,我保留了这一行。

现在,回到你的代码。为了提供解析多个数字的功能,您所要做的就是在遇到一个数字时循环遍历输入字符串,直到第一个非数字字符。

我对您的代码做了一些修改,向您展示了它应该如何工作的基本想法:

代码语言:javascript
复制
private static final String DIGITS = "0123456789";

public static String convertPostfixtoInfix(String toPostfix)
{
    LinkedStack<String> s = new LinkedStack<>();
    StringBuilder digitBuffer = new StringBuilder();  

    /* I've changed the 'for' to 'while' loop, 
       because we have to increment i variable inside the loop, 
       which is considered as a bad practice if done inside 'for' loop
    */
    int i = 0;
    while(i < toPostfix.length()) 
    {
        if(DIGITS.indexOf(toPostfix.charAt(i)) != -1)
        {
            //when a digit is encountered, just loop through toPostfix while the first non-digit char is encountered ...
            while (DIGITS.indexOf(toPostfix.charAt(i)) != -1) {
                digitBuffer.append(toPostfix.charAt(i++)); //... and add it to the digitBuffer
            }
            s.push(digitBuffer.toString());
            digitBuffer.setLength(0); //erase the buffer
        }
        //this if-else can also be replace with only one "if (toPostfix.charAt(i) != ' ')"
        else if(toPostfix.charAt(i) == ' ');{}//do nothing for blank.
        else
        {
            String temp = "";
            temp += toPostfix.charAt(i);

            String num1 = s.top();
            s.pop();
            String num2 = s.top();
            s.pop();
            s.push("(" + num2 + temp + num1 + ")");
        }
        i++;
    }

    return s.top();//top() is same as peek() method.
}

输入:40 5-9 20 1+/* 输出:((40-5)*(9/(20+1)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55370323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档