gpt4 book ai didi

c++ - 我应该使用 map 、3D 数组还是坚持使用堆栈?

转载 作者:行者123 更新时间:2023-11-30 03:43:42 25 4
gpt4 key购买 nike

到目前为止,除了开大括号多于闭大括号的情况外,我可以使它在每种情况下都能完美地工作。每当堆栈仍然包含任何左括号时,我都不知道如何找到那个特定的左括号。我想知道是否包括 map 或更改我的堆栈以接收 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

最佳答案

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

struct Bracket {
char type;
int line;
int col;
}

顺便说一句:存储预期的右括号(而不是左括号)可能是有意义的:计算你推的位置很简单,并且会简化当前由 3x && 和 2x 组成的匹配||

关于c++ - 我应该使用 map 、3D 数组还是坚持使用堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35965689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com