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

黄明

我有一个作业,并且在最近的两天里一直在窃听,我一直在做伪代码,但仍然没有正确。例如,如果我输入“ 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; } }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

C ++在类模板中使用函数模板

来自分类Dev

C ++在类变量的赋值中使用大括号

来自分类Dev

在C ++中的多个类中使用模板类

来自分类Dev

在一类中使用模板和虚方法的C ++

来自分类Dev

如何在类C ++中使用模板化结构

来自分类Dev

在堆栈中使用线程(C ++)

来自分类Dev

在堆栈中使用线程(C ++)

来自分类Dev

在C ++中使用模板

来自分类Dev

在C#中使用LINQ将XML解析为类

来自分类Dev

在C#中使用LINQ将XML解析为类

来自分类Dev

C ++-模板类堆栈实现中的反向函数

来自分类Dev

返回模板类的函数中的C ++空尖括号

来自分类Dev

在C ++模板中嵌套的类中使用基类的成员时出错

来自分类Dev

在C ++中的另一个类中使用模板类

来自分类Dev

在C ++中使用带有默认模板参数的模板类时出现“模板参数过多错误”

来自分类Dev

使用堆栈类的Postfix计算,C ++

来自分类Dev

在C中使用递归的堆栈溢出

来自分类Dev

在C中使用堆栈引发异常

来自分类Dev

C中的堆栈括号匹配

来自分类Dev

如何在C ++中使用模板化函数实现父类?

来自分类Dev

C ++:如何在类成员中使用未命名的模板参数?

来自分类Dev

如何在C ++中使用通用引用参数为模板类编写构造函数

来自分类Dev

C ++:使用堆栈检查字符串中括号和括号的顺序是否正确

来自分类Dev

如何在C ++中的另一个模板函数中使用属于模板化类的嵌套类型?

来自分类Dev

在C ++中使用功能模板作为模板模板参数

来自分类Dev

在C ++类中使用<random>

来自分类Dev

在Arduino中使用C ++类

来自分类Dev

使用模板化链表的C ++堆栈-内存泄漏

Related 相关文章

热门标签

归档