c++ - 编写了一个C++代码以检查表达式是否具有平衡的括号并且我的代码未运行。我被困了一天

标签 c++ data-structures error-handling stack

代码如下。没有语法错误,并且如果我仅检查()括号是否匹配,则代码可以正常运行,但是在我添加了一些if .. else ..语句以检查其他括号的匹配之后,我的代码坏了。我无法弄清楚我的错误。请帮忙!!我认为我犯了一些愚蠢的错误,但无法弄清楚是什么。

// structure of a stack

struct stackStructure
{
    // top pointer of the stack
    int top;
    // pointer to the array/stack
    char *array;
}stack;

// function to push element to stack

void push(char data)
{
    if(stack.top == 999)
    {
        cout<<"Stack Overflow"<<endl;
        return;
    }
    else
    {
        // incrementing the top and then pushing data to the stack
        stack.top++;
        stack.array[stack.top]= data;
    }
} // end of push() function

// function to pop elements from the stack

char pop()
{
    if(stack.top == -1)
        return '\0';
    else
        // returning the popped value and then decrementing the top pointer
        return stack.array[stack.top--];
} // end of pop() function
int main()
{
    // match variable to keep track that closing bracket and opening brackets are in sequence
    char match= '\0';
    bool isMatching= true;
    // resetting the stack variables and attributes
    stack.top= -1;
    stack.array= new char[1000];
    cout<<"Enter the Expression to match the parenthesis: ";
    cin>>stack.array;

    // traversing through the character array and matching parenthesis
    for(int i=0; stack.array[i] != NULL; i++)
    {
        // if character is an opening bracket
        if(stack.array[i] == '(' || stack.array[i] == '{' || stack.array[i] == '[')
            push(stack.array[i]);
        // if character is a closing bracket
        else if(stack.array[i] == ')' || stack.array[i] == '}' || stack.array[i] == ']')
        {
            match= pop();
            if(stack.array[i] != match)
                isMatching= false;
        }
        // if character is anything else we do nothing
        else
            continue;
    }
    if(isMatching == true)
        cout<<"Parenthesis Matched"<<endl;
    else
        cout<<"Not matched"<<endl;
    return 0;
}

最佳答案

您有2个错误。首先是将输入字符串读入堆栈,这至少会造成困惑,最坏的情况是不起作用。

第二个问题是,在检查匹配的标签时,您要检查左括号与右括号相同,这永远都是不正确的,您需要检查左括号是否与右括号相同。

解决这两个错误的一种方法是:

int main()
{
    // match variable to keep track that closing bracket and opening brackets are in sequence
    char match = '\0';
    bool isMatching = true;
    // resetting the stack variables and attributes
    stack.top = -1;
    stack.array = new char[1000];
    std::string input;
    cout << "Enter the Expression to match the parenthesis: ";
    cin >> input;

    std::map<char, char> opening = { { ')', '(' }, { '}', '{' }, { ']', '[' } };

    // traversing through the character array and matching parenthesis
    for (char ch : input)
    {
        // if character is an opening bracket
        if (ch == '(' || ch == '{' || ch == '[')
            push(ch);
        // if character is a closing bracket
        else if (ch == ')' || ch == '}' || ch == ']')
        {
            match = pop();
            auto open = opening.find(ch);
            if (open == opening.end() || open->second != match )
                isMatching = false;
        }
    }
    if (isMatching == true)
        cout << "Parenthesis Matched" << endl;
    else
        cout << "Not matched" << endl;
    delete[] stack.array;
    return 0;
}

关于c++ - 编写了一个C++代码以检查表达式是否具有平衡的括号并且我的代码未运行。我被困了一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60019885/

相关文章:

c++ - 将函数传递给构造函数中的初始化列表有多明智?

C++ putback() 成员函数

algorithm - 排序问题 - n/k 间隔,每个间隔大小为 k

C# HttpWebRequest SEC_I_RENEGOTIATE 间歇性错误

c++ - 子类,赋值运算符重载?

c++ - gdb 从函数打印静态变量

Swift:将一组值 A 映射到 B,将 B 映射到 A

c# - 这里提供所需信息量的最简单的数据结构是什么?

error-handling - 什么是ANTLR3错误恢复方法?

sql - 如何检测哪个值发生错误?