java - 如何将 '^' 运算符添加到此中缀到前缀转换、中缀到后缀转换中

标签 java

您能解释一下如何添加吗?

我的中缀是:1^2+3/3

前缀表达式为:+1^2/33(这是错误的)

后缀表达式为:1^233/+(这是错误的)

前缀为:+^12/33

后缀将为:12^33/+

我的代码:

import java.io.*;
class Stack
{
    private char[] a;
    private int top,m;
    public Stack(int max)
    {
        m=max;
        a=new char[m];
        top=-1;
    }
    public void push(char key)
    {
        a[++top]=key;
    }
    public char pop()
    {
        return(a[top--]);
    }
    public char peek()
    {
        return(a[top]);
    }
    public boolean isEmpty()
    {
        return (top==-1);
    }
}

class Evaluation
{
    private Stack s;
    private String input;
    private String output="";
    public Evaluation(String str)
    {
        input=str;
        s=new Stack(str.length());
    }

    public String inToPre()
    {
        for(int i=input.length()-1;i>=0;i--)
        {
            char ch=input.charAt(i);
            **switch(ch)
            {
            case '+':
            case '-':gotOperator(ch,1,')');
            break;
            case '*':
            case '/':gotOperator(ch,2,')');
            break;
            case ')':s.push(ch);
            break;
            case '(':gotParenthesis(')');
            break;
            default:output=ch+output;
            }
        }
        while(!s.isEmpty())
            output=s.pop()+output;
        return output;
    } // End to inToPre

    public String inToPost()
    {
        for(int i=0;i<input.length();i++)
        {
            char ch=input.charAt(i);
            switch(ch)
            {
            case '+':
            case '-':gotOperator(ch,1,'(');
            break;
            case '*':
            case '/':gotOperator(ch,2,'(');
            break;
            case '(':s.push(ch);
            break;
            case ')':gotParenthesis('(');
            break;
            default:output=output+ch;
            }
        }
        while(!s.isEmpty())
            output=output+s.pop();
        return output;
    } // End inToPost**

    private void gotOperator(char opThis,int prec1,char x)
    {
        while(!s.isEmpty())
        {
            char opTop=s.pop();
            if(opTop==x)
            {
                s.push(opTop);
                break;
            }
            else
            {
                int prec2;
                if(opTop=='+'||opTop=='-')
                    prec2=1;
                else
                    prec2=2;
                if(prec2<prec1&&x=='(')
                {
                    s.push(opTop);
                    break;
                }
                else if(prec2<=prec1&&x==')')
                {
                    s.push(opTop);
                    break;
                }
                else
                {
                    if(x==')')
                        output=opTop+output;
                    else
                        output=output+opTop;
                }
            }
        } // End While gotOperator
        s.push(opThis);
    } // End gotOperator

    private void gotParenthesis(char x)
    {
        while(!s.isEmpty())
        {
            char ch=s.pop();
            if(ch==x)
                break;
            else
            {
                if(x==')')
                    output=ch+output;
                else
                    output=output+ch;
            } // End Else
        } // End While gotParenthesis
    } // End gotParenthesis
} // End Class Evaluation

最佳答案

只要快速查看您的代码,我就可以看到在 InFix 分析中您没有考虑 ^ 运算符的新级别。

组应该是 +-、*/、^。由于 ^ 具有更高的优先级。

问候

关于java - 如何将 '^' 运算符添加到此中缀到前缀转换、中缀到后缀转换中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19118609/

相关文章:

java - Java中的乘法运算导致负值

java - 更改显示在 alertBox 中的 EditText(任何 View )的大小?

java - 逐行合并两个文件Java

java - Android Studio错误 "Cannot resolve method for method .add"

java - 如何从列表中隐藏 jcombobox 项目,但仍使用它从数据库表中获取数据

java - TreeMap 为某些对象键应该存在的值返回 null

java - 如何为使用 java.util.regex 的函数编写 JUnit

java - 使用 Byte Buddy 在运行时添加方法注释

java - 如何设置推断类型列表 getter 和 setter?

java - PdfBox 将多页 PDF 转换为一张 JPG 图像