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++ - 从并发析构函数停止 boost::asio::io_service::run()

c# - 为什么我的try catch在asp.net C#中不起作用

c++ - POCO - 我可以使用 SocketReactor 作为客户端吗?

c++ - 流程处理的封装会产生问题

c++ - 如果在 WinXP 上的调试版本中使用 boost (C++) 库,则无法执行程序

java - Java 支持结构吗?

c++ - 从 .txt 文件初始化对象 vector

java - Java 如何保存对堆栈的引用?

java - 由: java.lang.NoClassDefFoundError: com.inn.wireless.data.Users引起

git - smartgit:执行命令失败:主干已过时