繁体   English   中英

后缀程序的中缀

[英]infix to postfix program

我已经将以下中缀写入 postfix 程序,但它不起作用。 我的程序接受输入但不显示任何结果。 任何人都可以帮助找到我程序中的问题。 如果您知道我的将中缀转换为后缀的算法是否正确,这也将是一个很大的帮助。

using namespace std;

class Stack
{
private:
    int top;
    char s[mx];
public:
    Stack()
    {
        top=-1;
    }

    void push(char c)
    {
        if(!stackFull())
        s[++top]=c;
    }

    void pop()
    {
        if(!stackEmpty())
        top--;
        else cout<<"Stack is empty"<<endl;
    }

    char topShow()
    {
        if(!stackEmpty())
        return s[top];
    }

    bool stackEmpty()
    {
        if(top==-1)
            return 1;
        else return 0;
    }

    bool stackFull()
    {
        if(top == (mx-1))
            return 1;
        else return 0;
    }
};

class Expression
{
private:
    char entry2;
    int precedence;
    char infix[mx];
    char postfix[mx];
public:

int prec(char symbol)
{
    switch(symbol)
    {
        case '(':return 0; break;
        case '-':return 1; break;
        case '+':return 2; break;
        case '*':return 3; break;
        case '/':return 4; break;
    }
}

void Read()
{
    cout<<"Enter the infix expression: ";cin>>infix;
    for(int i=0;infix[i]!='\0';i++)
    {
        convertToPostfix(infix[i]);
    }
}

void ShowResult()
{
    cout<<"Postfix expression"<<endl;
    for(int j=0;postfix[j]!='\0';j++)
    {
        cout<<postfix[j];
    }
}

void convertToPostfix(char c)
{
    int p=0;
    Stack myStack;
    precedence=prec(c);
    entry2=myStack.topShow();
    if(isdigit(c))
    {
        postfix[++p]=c;
    }

    if(precedence>prec(entry2))
    {
        myStack.push(c);
    }

    if(precedence<prec(entry2))
    {
        switch(c)
        {
            case '(': myStack.push(c); break;
            case ')': while(myStack.topShow()!= '(')
                        {
                            postfix[++p]=myStack.topShow();
                            myStack.pop();
                        };myStack.pop();break;
            case '+':
            case '-':
            case '*':
            case '/': while(prec(myStack.topShow())>=precedence)
                        {
                            postfix[++p]=myStack.topShow();
                            myStack.pop();
                        };break;
        }
    }

}

};

int main()
{
    Expression myExp;
    myExp.Read();
    myExp.ShowResult();
    return 0;
}

以下是我发现的一些问题:

布尔函数返回 true 或 false 将返回类型与返回值匹配。 数字 1 和 0 不是布尔值。

优先级表
加法和减法具有相同的优先级。
乘法和除法具有相同的优先级。
乘法和除法的优先级高于加法和减法。

堆栈消失
由于堆栈在函数中被声明为局部变量,因此在进入函数时会重新创建并在退出函数前销毁。

解决方案:将其作为类成员移至类中或将其声明为static

每行多条语句效率不高
空行和换行不会影响性能,并且为构建增加的时间可以忽略不计。
但是,它们使您的程序更具可读性,这有助于检查或调试。 使用它们。

与操作符前后的空格类似。
现在就养成这个习惯,而不是在找到工作后改正。

调用一次函数并存储值
你调用prec(entry2)两次,这是浪费时间。 调用一次并将值保存在变量中。 stack.TopShow()类似。

使用 std::vector 不是数组
std::vector将根据需要增长并减少缓冲区溢出的机会。
对于数组,您必须检查索引是否始终在范围内。 此外,阵列容量不会改变; 您必须声明一个新实例并复制数据。

未声明变量 mx
编译器应该捕获这个。 您使用mx作为数组的容量并比较full 但是,它从未被声明、定义或初始化。 更喜欢std::vector ,您将不必处理这些问题。

输入未验证
您输入了一个字母,但不验证它。
试试这些字符:空格、#、@、A、B 等。

缺少开关的默认值
最大限度地提高编译器警告。 您的switch语句需要default s。 数字字符 ('0'..'9') 有什么优先级?
(您检查数字字符的优先级。)

检查通过您的函数和程序的所有路径。 使用调试器(见下文)或笔和纸,通过函数检查程序流。 包括边界值和不在边界内的值。

Case 语句:break 或 return
您不需要在return语句后break 想想看。 程序能否在return语句后的那一行继续执行?

使用调试器或打印语句
您可以在程序的不同点打印变量。 当调试器不可用时,这是一种古老的技术。
学习使用调试器。 大多数 IDE 都带有它们。 您可以单步执行每个语句并打印出变量值。 非常,非常,有用。

class infixToPostfix{
    public static void postfix(String str){
        Stack<Character> stk = new Stack<Character>();
        for(Character c : str.toCharArray()){
            // If operands appears just print it
            if(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'){
                System.out.print(c);
            }else{ 
                // Open paranthesis push is 
                if(c == '('){
                    stk.push(c);
                //Close paranthesis pop until close paranthesis
                }else if( c == ')'){
                    while(stk.peek() != '(')
                        System.out.print(stk.pop());
                    stk.pop();
                // check the precedence of operator with the top of stack
                }else if(c == '+' || c == '-'){
                    if(!stk.isEmpty()){
                        char top = stk.peek();
                        if(top == '*' || top == '/' || top == '+' || top == '-'){
                            System.out.print(stk.pop());    
                        }
                    }   
                    stk.push(c);
                }else{
                    if(!stk.isEmpty()){
                        char top = stk.peek();
                        if(top == '/' || top == '*'){
                            System.out.print(stk.pop());
                        }
                    }   
                    stk.push(c);
                }
            }
        }
        //Print all the remaining operands
        while(!stk.isEmpty()) System.out.print(stk.pop());
        System.out.println();
    }
    public static void main(String args[]){
        String str = "A+B-(c+d*Z+t)/e";
        postfix(str);
    }
}

使用堆栈和映射你可以解决问题
1)创建一个以运算符为键和一些整数来设置优先级的映射。 具有相同优先级的运算符将具有相同的值,例如:

map<char,int>oprMap;
oprMap['^'] = 3;
oprMap['*'] = 2;
oprMap['/'] = 2;
oprMap['+'] = 1;
oprMap['-'] = 1;

2) 遍历给定的表达式调用这些检查
1) 如果当前元素
i) 是操作数,将其添加到结果中
ii) 非操作数做以下检查
一种。 而不是(堆栈为空,元素为左方括号,找到具有更高优先级的运算符。将堆栈顶部添加到结果和 pop()
将当前元素压入堆栈

iii) 如果左括号压入堆栈
iv) 如果闭合括号弹出直到得到闭合括号并将其添加到结果中

3) 当堆栈不为空时 pop() 并将顶部元素添加到结果中。

{       
        stack<char>S;
        for (int i = 0; i < n; i++)  {
            if(isOperand(exps[i])) {
                res = res + exps[i];
            } else if(isOperator(exps[i])){

                while(!(S.empty() && isOpenParanthesis(S.top()) && isHeigherPrecedence(S.top(),exps[i])){
                    res = res+S.top();
                    S.pop();
                }  
                S.push(exps[i]);
            } else if(isOpenParanthesis(exps[i])) {
                S.push(exps[i]);
            } else if(isClosingParanthesis(exps[i])) {
                while(!S.empty() && !isOpenParanthesis(S.top())) {
                    res = res+S.top();
                    S.pop();
                }
                S.pop();
            }
        }

        while(!S.empty()) {
            res = res + S.top();
            S.pop();
        }
    }
}
#include<bits/stdc++.h>
using namespace std;

// 这个 isHigher 函数检查字符 a 比 b 的优先级。

 bool isHigher(char a,char b)
{
if(a=='+' || a=='-')
return false;

else if((a=='*' && b=='*') || (a=='*' && b=='/') || (a=='/' && b=='*') ||          
 (a=='/' && b == '/')|| (a=='^' && b=='^')||(a=='*' && b=='^') || (a=='/' && 
  b=='^'))
    return false;

    return true;
}

int main(){

    string s;
    cin>>s;
    s = s + ")";
 //Vector postfix contains the postfix expression.
    vector<char>postfix;
    stack<char>mid;
    mid.push('(');
    for(int i=0;i<s.length();i++)
    {
        if(s[i] == '(')
            mid.push(s[i]);

        else if(s[i] == '+' || s[i] == '^' || s[i] == '-' || s[i] == '*'|| 
          s[i] == '/')
        {
            if(mid.top() == '(')
                mid.push(s[i]);

            else {

                if(isHigher(s[i],mid.top()))
                    mid.push(s[i]);

                else
                {

                 while(mid.top()!='(')
                 {

                    if(!isHigher(s[i],mid.top()))
                        {
                            postfix.push_back(mid.top());
                            mid.pop();  
                        }

                    else
                        break;

                 }

                 mid.push(s[i]);

                }
            }
        }
        else if(s[i] == ')')
        {

            while(mid.top() != '(')
            {
                            postfix.push_back(mid.top());
                            mid.pop();      
            }

            mid.pop();

        }

        else
            postfix.push_back(s[i]);

    }

    for(int i=0;i<postfix.size();i++)
        cout<<postfix[i];

 return 0;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM