我应该使用地图,3D阵列还是坚持堆叠?

时间:2016-03-13 02:15:22

标签: c++ arrays dictionary stack

到目前为止,我可以完美地为每个案例工作,除非有更多的开括号而不是闭括号。每当堆栈仍然包含任何打开的括号时,我不知道如何找到特定的开括号。我想知道是否包含一个地图或更改我的堆栈以获取3D(?)数组值,以便它可以包含char的坐标?如果有一个更简单的方法,如果你们可以帮助引导我走向它会很好,请不要只告诉我答案,因为我不知道我应该如何使它工作。 ; - ;

非常感谢你!

    void Brackets::check_bracket()
        {
            for (int a = 0; a < line_num; a++)//loops through every line
            {
                for (int c = 0; c < lines[a].length(); c++)//loops through every char in string
                {
                    while (lines[a].find("//") != -1 || lines[a].find("cout") != -1) a++;//checks if there is a comment
                    if (lines[a][c] == '(' || lines[a][c] == '[' || lines[a][c] == '{')//checks if the brace is an open brace
                    {
                        //DBG::cout << "yes" << lines[a][c] << endl;
                        bracket.push(lines[a][c]);//if yes then it pushs the brace in
                    }
                    if (lines[a][c] == ')' || lines[a][c] == ']' || lines[a][c] == '}')//checks if the brace is a close brace
                    {
                        //DBG::cout << bracket.empty() << lines[a][c] << endl;
                        if (bracket.empty())
                        {//if empty then tells user the bracket does not match any open parenthesis
                            //DBG::cout << 1;
                            cout << "closed parenthesis " << lines[a][c] << " does not match any open parenthesis.";
                            return;
                        }
                        //else if the brackets do match an open brace then it pops it out
                        else if ((lines[a][c] == ')' && bracket.top() == '(') || (lines[a][c] == ']' && bracket.top() == '[') || (lines[a][c] == '}' && bracket.top() == '{'))
                        {
                            //DBG::cout << 2;
                            bracket.pop();
                        }//checks if bracket matches any open parenthesis
                        else
                        {
                            //DBG::cout << 3 << a << endl;
                            cout << "Line " << a+1 << ":" << " error at column " << c+1 << ":" << endl;
                            cout << lines[a] << endl;
                            for (int o = 0; o < c; o++)
                                cout << " ";
                            cout << "^" << endl;
                            cout << "closed parenthesis " << lines[a][c] << " does not match open parenthesis " << bracket.top() << " from row " << find_last(a, bracket.top());
                            return;
                        }
                    }
                }
            }
            if (!bracket.empty())//if bracket still contains something in the stack then it finds where the leftover open brace is
            {
                cout << "Unmatched open parenthesis " << bracket.top() << " at row "; //im stuck
            }
            else
            cout << "No parenthesis errors."<<endl;//otherwise no parenthesis errors
        }
        //finds the last problem brace
        string Brackets::find_last(int line, char chk)
        {
            string row_col = "";
            for (int a = line; a >= 0; a--)//loops through lines backwards
            {
                for (int b = lines[a].length() - 1; b >= 0; b--)//loops through length backwards
                {
                    if (lines[a][b] == chk)//checks if the char equals the problem brace
                    {
                        row_col = to_string(a+1) + " and column " + to_string(b+1)+"\n";//returns string of location
                        return row_col;
                    }
                }
            }
            return "";
        }

private:
    string lines[1000];//contains code lines
    int line_num = 0;//number of lines
    stack<char> bracket;//brackets

2 个答案:

答案 0 :(得分:2)

我只是在堆栈上存储一个简单的结构

struct Bracket {
  char type;
  int line;
  int col;
}
BTW:存储预期的右括号(而不是左括号)可能是有意义的:计算你推送的位置很简单,并简化目前由3x &&和2x {{1}组成的匹配}}

答案 1 :(得分:0)

您只是在寻找匹配的牙箍吗?

坚持使用堆栈但存储结构而不仅仅是char;像斯特凡建议的那样。由于括号本质上是堆栈式的,即更开放的括号,你在堆叠中更深,而关闭括号会弹出堆栈。理想情况下,当一切正常时,你应该拥有并清空堆栈。您使用结构而不仅仅是char的原因是您可以保留有关括号的更多信息。

3D阵列将使用过多的连续空间来实现您的目标。如果你解析一个大文件或多个文件,它会变得非常糟糕。

地图并不是那么糟糕,但并不理想。您只能拥有一个键和值,可能无法提供您想要的所有信息。管理结构的附加功能也会产生不必要的负担,只需简单的推送和弹出就可以解决这个问题。

所以,总结一下:  1)创建一个包含char,line和column的结构(由Stefan Haustein建议)  2)当你找到你的大括号时,用相关信息推送结构  3)如果你达到EOF并且堆栈不是空的,则弹出堆栈的其余部分并打印结构&#39;信息     如果您到达堆栈的末尾但仍然有一个小括号,请打印信息