C++ 中的后缀到中缀的转换。 "Process returned -1073740940 (0xC0000374) execution time : 5.538 s"。不知道为什么

Postfix to Infix conversion in C++. "Process returned -1073740940 (0xC0000374) execution time : 5.538 s". No clue why

问题是在没有堆栈库的情况下使用堆栈将后缀表达式转换为中缀表达式。

我在 运行 之后收到一条语句并输入我的后缀表达式:“处理 returned -1073740940 (0xC0000374) 执行时间:5.538 秒”

第二次我输入我的后缀表达式时,计算机冻结并占用了几秒钟,然后给出了语句。 它甚至没有 return 中缀表达式。

我完全不知道为什么。 有时也可能需要 30-40 秒才能弹出此错误。

编辑#1: 好吧,正如@churill 所说,放错地方的括号是个问题。我纠正了这一点。现在我立即收到此错误:“在抛出 'std::bad_alloc' 的实例后终止调用 什么():std::bad_alloc

我的代码:(编辑过一次)

#include <iostream>
#include <ctype.h>
const int MAX = 20;

using namespace std;

class Stack {
private:
    int top = -1;
    string arr[MAX];
public:
    bool isEmpty(){
        if (top == -1){
            return true;
        }
        else{
            return false;
        }
    }

    bool isFull(){
        if(top == MAX-1){
            return true;
        }
        else{
            return false;
        }
    }

    void push(string c){
        if(isFull()){ //Edited line 
            cout<<"Stack Overflow"<<endl;
        }
        else{
            top++;
            arr[top] = c;
        }
    }

    string pop(){
        string val = arr[top];
        arr[top] = '0';
        top--;
        return val;
    }

    string topele(){
        string topelement = arr[top];
        return topelement;
    }

    string display(){
        int i;
        for(i=top;i>=0;i--){
            cout<<arr[i]<<endl;
        }
    }

    bool isOperand(char c){
        if(c >= 'a' && c <= 'z'){
            return true;
        }
        else{
            return false;
        }
    }

    string posfixtoinfix(string postfix){
        Stack s;
        int i;
        int len = postfix.size();
        for(i=0;i<=len;i++){
            if(s.isOperand(postfix[i])){ //Edited line
                string op(1, postfix[i]);
                s.push(op);
            }
            else{
                string op1 = s.topele();
                s.pop();
                string op2 = s.topele();
                s.pop();
                string res = ("("+ (op2 + postfix[i] + op1) + ")");
                s.push(res);
            }
        }
        return s.topele();
    }

};

int main(){
    Stack s1;
    string postfix, infix;
    cout<<"Enter postfix expression: "<<endl;
    cin>>postfix;
    infix = s1.posfixtoinfix(postfix);
    cout<<"The infix expression is: "<<infix<<endl;

    return 0;

}

首先,您的程序中有 未定义的行为。这是因为当你写 string op1 = s.topele(); at:

时你越界了
else{
        string op1 = s.topele(); //this will call topele
//...
}

现在成员函数topele被调用,你有

string topelement = arr[top]; //this will result in(when top = -1) undefined behavior and may crash the program

但请注意,由于 top 默认保存值 -1,因此在上面的代码片段中,您实际上是在写

string topelement = arr[-1];

这会导致未定义的行为并可能导致崩溃。

请记住,对于 std::string,索引的有效范围是 0myString.length() - 1 包含 )。当您使用 -1 作为索引时,您会在 字符串之前得到字符 ,这是 越界 并导致未定义的行为。

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.


1有关未定义行为的更技术准确的定义,请参阅 this 其中提到:没有对程序行为的限制.