C++ 写了一个C++;用于检查表达式是否有平衡括号且我的代码未运行的代码。我已经被困了一天了

C++ 写了一个C++;用于检查表达式是否有平衡括号且我的代码未运行的代码。我已经被困了一天了,c++,data-structures,error-handling,stack,C++,Data Structures,Error Handling,Stack,代码如下所示。没有语法错误,如果我只检查了()括号的匹配,但在添加了一些if.之后,代码运行得非常好。。其他的语句检查是否与其他括号匹配,我的代码中断。我无法找出我的错误。请帮忙!!我想我犯了一些愚蠢的错误,但不知道是什么 // structure of a stack struct stackStructure { // top pointer of the stack int top; // pointer to the array/stack char *

代码如下所示。没有语法错误,如果我只检查了()括号的匹配,但在添加了一些if.之后,代码运行得非常好。。其他的语句检查是否与其他括号匹配,我的代码中断。我无法找出我的错误。请帮忙!!我想我犯了一些愚蠢的错误,但不知道是什么

// 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;
}
//堆栈的结构
结构堆栈结构
{
//堆栈的顶部指针
int top;
//指向数组/堆栈的指针
字符*数组;
}堆叠;
//函数将元素推送到堆栈
无效推送(字符数据)
{
如果(stack.top==999)
{

cout您有两个bug。第一个bug是将输入字符串读入堆栈,这至少会让人非常困惑,最坏的情况下也不会起作用

第二个是,当检查匹配的标记时,检查开始括号是否与结束括号相同,这永远不会是真的,您需要检查开始括号是否与结束括号类型相同

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

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;
}
intmain()
{
//匹配变量以跟踪关闭括号和打开括号的顺序
字符匹配='\0';
bool isMatching=true;
//重置堆栈变量和属性
stack.top=-1;
stack.array=新字符[1000];
std::字符串输入;
cout>输入;
std::map opening={{}','('},{'}','{'},{']','['}};
//遍历字符数组和匹配的括号
for(字符通道:输入)
{
//如果字符是一个左括号
如果(ch='('| | ch='{'| | ch=='['))
推(ch);
//如果字符是右括号
else如果(ch=')'| | ch='}'| | ch=']'))
{
match=pop();
自动打开=打开。查找(ch);
如果(打开==打开.end()| |打开->秒!=匹配)
isMatching=false;
}
}
如果(isMatching==true)

cout@Beats2019这是一个指向字符数组的字符指针,在我为多括号匹配条件添加了did更改之前,它工作正常。您声明了int top并将其注释为指针
struct stackStructure{int top;
-您可能希望初始化该变量。@Beats2019将索引调用到数组中并不是不合理的,它是指针(这本质上就是指针,是全局内存数组中的索引)@JesperJuhl在main开始时,它被初始化为
-1
,我在字符数组中输入,然后根据它是开始括号还是结束括号,我将其推入或弹出。这是错误的写入方式吗?在我的代码中,我已经在检查
match=pop();if(stack.array[i]!=match)isMatching=false;
对于这种特殊情况,我认为对输入和堆栈使用相同的数组总是可行的,但是如果代码变得更复杂,那么使用堆栈推送覆盖输入可能会导致问题。主要问题是比较结束方括号和开始方括号,这将永远不会出现问题tch