gpt4 book ai didi

c++ - 在 C++ 中使用堆栈模板类解析括号

转载 作者:搜寻专家 更新时间:2023-10-31 00:15:35 25 4
gpt4 key购买 nike

我有一个家庭作业,最近两天一直在窃听,我一直在做伪代码,但仍然没有做对。例如,如果我输入“mike]”或“mike]123”,我的程序会崩溃,因为堆栈是空的……据我观察,程序会在以下情况下崩溃:- 堆栈为空- 并且有一个右括号PS:在us2012的帮助下,我可以修复崩溃问题。然而,结果并不对。它不是打印出“无效”,而是输出“有效”

:(

这是我教授的伪代码:

def parse_parenthesis(str):
stack = create a new empty stack of OpenParen objects
for i from 0 to str.size() - 1:
if str[i] is an open parenthesis
stack.push(new OpenParen(str[i]))
else if str[i] is not a close parenthesis:
# str[i] is not a parenthesis of any kind, so ignore it
continue
# otherwise str[i] must be a close parenthesis, try to
# match it with the most recent open paren, on the top
# of the stack
else if stack is empty
return false;
else if stack.peek() is of the same type as str[i]:
# close properly
stack.pop()
else
return false;
if stack is not empty
return false;
else
return true

这是我目前所拥有的:

.cpp文件

bool ParenMatching(const string& s, unique_ptr<string>& output)
{
unique_ptr<OpenParen> stack(new OpenParen);

bool validOpen, validClose, valid;
bool match; //haveCloseParen;
/*string unMatch = "Unmatch";
string unExpected = "Unexpected close paren";
string noError = "No Error";*/

for (size_t i = 0; i < s.length(); i++)
{
// check if its open parenthesis
validOpen = stack->IsOpenParen(s[i]);
// check if its close parenthesis
validClose = stack->IsCloseParen(s[i]);

// if there is open paren, push into the stack
if(validOpen)
stack->PushObj(s[i]);
else if(!validClose)
{
continue;
}
else if(stack->GetObj().IsEmpty())
valid = false;
else if(match = IsMatchParen(s[i], stack))
stack->PopObj();
else
valid = false;
}

if(!stack->GetObj().IsEmpty())
valid = false;
else
valid = true;
return valid;
}

bool IsMatchParen(const char c, const unique_ptr<OpenParen>& stack)
{
bool valid;
if(c == ')' && stack->PeekObj() == '(')
valid = true;
else if (c == ']' && stack->PeekObj() == '[')
valid = true;
else if (c == '}' && stack->PeekObj() == '{')
valid = true;
else if (c == '>' && stack->PeekObj() == '<')
valid = true;
else
valid = false;
return valid;
}

OpenParen.cpp

// Check if its open paren
bool OpenParen::IsOpenParen(const char c)
{
bool isOpen;
if(c == '(' || c == '[' || c == '{' || c == '<')
isOpen = true;
else
isOpen = false;
return isOpen;
}

// check if its close paren
bool OpenParen::IsCloseParen(const char c)
{
bool isClose;
if(c == ')' || c == ']' || c == '}' || c == '>')
isClose = true;
else
isClose = false;
return isClose;
}

最佳答案

gcc 4.7.3: g++ -Wall -Wextra -std=c++0x parens.cpp

#include <iostream>
#include <stack>
#include <string>
#include <vector>

bool isOpen(char c) {
return c == '(' || c == '[' || c == '{' || c == '<'; }

bool isClose(char c) {
return c == ')' || c == ']' || c == '}' || c == '>'; }

bool isMatch(char c1, char c2) {
return (c1 == '(' && c2 == ')')
|| (c1 == '[' && c2 == ']')
|| (c1 == '{' && c2 == '}')
|| (c1 == '<' && c2 == '>'); }

bool parse(const std::string& s) {
std::stack<std::string::value_type> stk;

for (std::string::size_type i = 0; i < s.size(); ++i) {
if (isOpen(s[i])) { stk.push(s[i]); }
else if (isClose(s[i])) {
if (!stk.empty() && isMatch(stk.top(), s[i])) { stk.pop(); }
else { return false; } } }

return stk.empty(); }

int main() {
std::vector<std::string> ptests = {
"", "()", "()()", "(())", "a(a)a" };
std::vector<std::string> ftests = {
"(", ")", ")(", ")()(", "))((" };

for (const auto& t : ptests) {
if (!parse(t)) { std::cout << "fail: " << t << std::endl; } }

for (const auto& t : ftests) {
if (parse(t)) { std::cout << "fail: " << t << std::endl; } }
}

关于c++ - 在 C++ 中使用堆栈模板类解析括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19061715/

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