将前缀表达式转换为后缀表达式

发布于 2024-09-30 21:36:35 字数 1205 浏览 1 评论 0原文

我正在尝试实现一个程序,使用递归将前缀表达式更改为后缀表达式。

我已经写了我认为可行的内容,但我得到的是 aa/aa/*aa/aa/*- 而不是输出 ab/c*de+f*-反而。

当我尝试获取 String pre 的第一个字符或尝试删除 String pre 的第一个字符时,我认为我的代码被卡住了。有什么建议/意见吗?

  public class Prefix2Postfix {
        public static final String prefixInput ="-*/abc*+def";
        //desired postfix output is "ab/c*de+f*-"

        public static void main (String[] args){
            System.out.println(pre2Post(prefixInput));
        }

        public static String pre2Post(String pre){
            //find length of string
            int length = pre.length();

            //ch = first character of pre
            char ch = pre.charAt(0);

            //delete first character of pre
            pre = pre.substring(1,length);
            if(Character.isLetter(ch)){
                //base case: single identifier expression
                return (new Character(ch)).toString(ch);
            }else{ 
                //ch is an operator
                String postfix1 = pre2Post(pre);
                String postfix2 = pre2Post(pre);
                return postfix1 + postfix2 + ch;
            }
        }
    }

I'm trying to implement a program that changes a prefix expression to a postfix one using recursion.

I've written what I thought would work but instead of the output ab/c*de+f*- I get aa/aa/*aa/aa/*- instead.

I think my code is getting stuck when I try to get the first character of String pre or when I try to delete the first character of String pre. Any suggestions/comments?

  public class Prefix2Postfix {
        public static final String prefixInput ="-*/abc*+def";
        //desired postfix output is "ab/c*de+f*-"

        public static void main (String[] args){
            System.out.println(pre2Post(prefixInput));
        }

        public static String pre2Post(String pre){
            //find length of string
            int length = pre.length();

            //ch = first character of pre
            char ch = pre.charAt(0);

            //delete first character of pre
            pre = pre.substring(1,length);
            if(Character.isLetter(ch)){
                //base case: single identifier expression
                return (new Character(ch)).toString(ch);
            }else{ 
                //ch is an operator
                String postfix1 = pre2Post(pre);
                String postfix2 = pre2Post(pre);
                return postfix1 + postfix2 + ch;
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2024-10-07 21:36:35

因此,代码中的错误与计算 postfix1postfix2 的位置有关 - 请注意,您没有抵消 postfix2

要执行此递归,您需要了解几种情况:

  • 当您遇到运算符时,您需要递归并将运算符向右移动,然后处理字符串中尚未处理的任何剩余部分
  • 当您遇到字母和运算符,您应该只返回字母
  • 当您遇到两个字母时,您应该只返回这两个字母

这意味着当您遇到类似 +-abc 的内容时,您将执行以下步骤:

f("+-abc") => return f("-abc") + "+" + f(rem1)
 f("-abc") => return f("abc") + "-" + f(rem2)
  f("abc") => return "ab"
  rem2 = "c" (remainder of the string)
  f("c")   => return "c"
 rem1 = ""   (nothing left in the string to parse)

which constructs "ab-c+"

这应该有效:

public static String pre2post(String pre){
    if(pre.length() <= 1){
        return pre;
    }

    if(!Character.isLetter(pre.charAt(0))){
        String a = pre2post(pre.substring(1)) + pre.charAt(0);
        String b = pre2post(pre.substring(a.length()));
        return a + b;
    }else if(!Character.isLetter(pre.charAt(1))){
        return pre.substring(0,1);
    }else{
        return pre.substring(0,2);
    }

}

So the error in your code has to do with where you calculate postfix1 and postfix2 -- note that you're not offsetting postfix2.

To do this recursion you need to understand a few cases:

  • When you encounter an operator you need to recurse and move the operator to the right, and then process any remaining portion of the string that has not been processed
  • When you encounter a letter and an operator you should just return the letter
  • When you encounter two letters, you should just return those two letters

This means when you encounter something like +-abc you will do the following steps:

f("+-abc") => return f("-abc") + "+" + f(rem1)
 f("-abc") => return f("abc") + "-" + f(rem2)
  f("abc") => return "ab"
  rem2 = "c" (remainder of the string)
  f("c")   => return "c"
 rem1 = ""   (nothing left in the string to parse)

which constructs "ab-c+"

This should work:

public static String pre2post(String pre){
    if(pre.length() <= 1){
        return pre;
    }

    if(!Character.isLetter(pre.charAt(0))){
        String a = pre2post(pre.substring(1)) + pre.charAt(0);
        String b = pre2post(pre.substring(a.length()));
        return a + b;
    }else if(!Character.isLetter(pre.charAt(1))){
        return pre.substring(0,1);
    }else{
        return pre.substring(0,2);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文