主要():

char inp[] = "(A+B)/(C*D))";

Infix i;
cout << "In main: " << inp  /* + ')' */  << endl << endl;

这是Infix构造函数:
Infix() {
        push('(');
        cout << "Element In Stack: " << *returnElement(returnTop()) << endl;
        outputString = "";
        strcpy(operatorArray, "/*-+%");
        cout << "Operator Array: " << operatorArray << endl;

    }

Infix从类'Stack'继承:
class Stack{
int top = -1;
char arr[100];

public:
    bool push(char);
    char pop();
    char peek();
    bool isEmpty();
    void displayAll();
    char returnTop() { return top;}
    char* returnElement(int i) {
        if(i > 98){
            cout << "StackOutOfIndex";
            return nullptr;
        }
        return &arr[i];
    }

};

当我在main中运行代码时,它显示异常输出:
Element In Stack: (
Operator Array: /*-+%
In main: +%


Stack Object Destroyed!

但是,在main中时,如果在注释行中声明“Infix”对象声明,则代码可以正常运行:
In main: (A+B)/(C*D))

编辑:


#include<iostream>

using namespace std;

class Stack{
    int top = -1;
    char arr[100];

    public:
        bool push(char);
        char pop();
        char peek();
        bool isEmpty();
        void displayAll();
        char returnTop() { return top;}
        char* returnElement(int i) {
            if(i > 98){
                cout << "StackOutOfIndex";
                return nullptr;
            }
            return &arr[i];
        }

};

bool Stack:: push(char elementToPush) {
    if(top > 98) {
        cout << "\nStack Overflow!!";
        return false;
    } else {
        arr[++top] = elementToPush;
        return true;
    }
}

char Stack:: pop() {
    if(top <= -1) {
        cout << "\nStack Underflow!!";
        return ' ';
    } else {
        return (arr[top--]);
    }
}

char Stack:: peek() {
    if(top > 98) {
        cout << "\nStack Overflow!!";
        return ' ';
    } else {
        return arr[top];
    }
}

bool Stack:: isEmpty() {
    return (top <= 0);
}

void Stack:: displayAll() {
    if(top <= -1) {
        cout << "null";
        return;
    }
    int i = top;
    while (i >= 0) {
        cout << arr[i] << " ";
        --i;
    }
    cout << "\n";
}


#include<iostream>
#include<cstring>
#include<D:\Programs\11Stack.cpp>

using namespace std;

class Infix : public Stack {
    string outputString;
    char operatorArray[];
    public:
        Infix() {
            push('(');
            cout << "Element In Stack: " << *returnElement(returnTop()) << endl;
            outputString = "";
            strcpy(operatorArray, "/*-+%");
            cout << "Operator Array: " << operatorArray << endl;

        }

        string infixToPostfix(char *, int);
        bool manupulateOperator(char, int);
        int checkPrecedence(char);

        ~Infix() {
            cout << "\nStack Object Destroyed!" << endl;
        }
};

string Infix:: infixToPostfix(char *str, int size) {
    cout << "\nGiven String: " << str << endl;
    int x;
    for(int i = 0; i < size; ++size) {
        x = str[i];
        if(x != ' ') {
            if(x == ')') {
                while(returnTop() != '(') {
                    cout << pop() << " popped!\n";
                }

                    cout << pop() << " popped!\n";
            } else if(isalpha(x)) {
                cout << x;
        } /* else{ // scanned character is an operator
            if(manupulateOperator(x, i)) {

            } else {
                return " ";
                }
            } */
        }
    }

    return outputString;
}

bool Infix::manupulateOperator(char c, int position) {
    try {
        char topElement = *returnElement(returnTop());
        if(checkPrecedence(c) == -1) {
            cout << "\nErr\n";
        }else if((checkPrecedence(c) > checkPrecedence(topElement)) || returnTop() == 0) {
            push(c);
            cout << c << " pushed!\n";
        }
    } catch(std::exception e) {
        std::cerr << e.what() << '\n';
        return false;
    } catch (char* Ce) {
        cout << Ce << endl;
    }

    return true;
}

int Infix::checkPrecedence(char c) {
    /*
        + -> 1
        - -> 1
        * -> 2
        / -> 2
        % -> 2
    */
    switch(c) {
        case '+':
            return 1;

        case '-':
            return 1;

        case '*':
            return 2;

        case '/':
            return 2;

        case '%':
            return 2;

        default:
            // throw "Illegal Operator Detected!";
            cout << "Illegal Operator Detected: " << c << endl;
            return -1;

    }
}

int main() {
    cout << endl;
    int x = 1;

    char inp[] = "(A+B)/(C*D))";

    //Infix i;
    cout << "In main: " << inp  /* + ')' */  << endl << endl;

    // cout << i.infixToPostfix(input + ')', sizeof(input));
/*     for(int i = 0; i < strlen(inp); ++i) {
        cout << inp[i];
    }
 */
    return 0;
}

最佳答案

您将operatorArray声明为char的数组,但您是而不是为其分配任何内存!因此,当您在strcpy(operatorArray, "/*-+%");构造函数中调用Infix时,通过尝试将给定的字符串常量复制到尚未分配的内存中,会导致 undefined 行为,这似乎覆盖了inp[]中声明的main数组。

为了解决这个问题,我建议给您的operatorArray成员一个特定的大小,该大小将足以容纳您要复制到它的任何字符串-8个字符将在您提供的示例代码中起作用:

class Infix : public Stack {
    string outputString;
    char operatorArray[8]; // Make this member a REAL array of characters.
    //..

关于c++ - 对象初始化后在C++中显示char数组时出现异常行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60093645/

10-11 07:49