用于检查输入中的平衡括号的代码 [英] Code to check the balanced brackets in input

查看:58
本文介绍了用于检查输入中的平衡括号的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个代码来检查输入字符串中的括号对,并打印出Success(对于具有匹配对的输入)或第一个不匹配的右括号的从1开始的索引。但是我得到了偶数匹配支架输入的基于1的索引。 (例如输入[]我得到1的输出而不是成功)。我明白我的逻辑有一个错误。有人可以指出错误吗?



I'm trying to write a code to check the bracket pairs in an input string and print out either "Success" (for an input with matched pair) or the the 1-based index of the first unmatched closing bracket. However I'm getting the 1-based index for even matched bracket inputs. (for ex. for the input [ ] I'm getting an output of 1 instead of "Success"). I understand that there is a mistake in my logic. Could someone point out the mistake?

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

struct Bracket {
    Bracket(char type, int position):
        type(type),
        position(position)
    {}

    bool Matchc(char c) {
        if (type == '[' && c == ']')
            return true;
        if (type == '{' && c == '}')
            return true;
        if (type == '(' && c == ')')
            return true;
        return false;

    }

    char type;
    int position;
};


int main() {
    std::string text;
    getline(std::cin, text);
    int z;
    int len = text.size();

    std::stack <Bracket> opening_brackets_stack;
    for (int position = 0; position < len ; ++position) {
        char next = text[position];
        Bracket brackObj(next,0);

        if (next == '(' || next == '[' || next == '{') {
            opening_brackets_stack.push(brackObj);
        }

        if (next == ')' || next == ']' || next == '}') {

            if(brackObj.Matchc(next) == false || opening_brackets_stack.empty() == false)
            {
               z = position;
            }

            else
            {
                opening_brackets_stack.pop();
            }
        }
    }

    if (opening_brackets_stack.empty()==true)
    {
        std::cout << "Success";
    }

    else
    {
        std::cout << z;
    }
    return 0;
}





我的尝试:



我正在使用堆栈来推送所有打开的括号,并在找到匹配的右括号时弹出它们。



对于非匹配输入(例如([{)]),它应返回4的输出,并为匹配输入返回Success(对于ex。({[]}))。



What I have tried:

I'm using a stack to push all the open brackets and pop them when a matching closing bracket is found.

For a non-matching input (for ex. ([{)] ) it should return an output of 4, and "Success" for a matching input ( for ex. ({[]}) ).

推荐答案

嗯......调试器可能会很快告诉你这个!

看看你正在做什么:

Um...The debugger would probably show you this very quickly!
Look at what you are doing:
if(brackObj.Matchc(next) == false || opening_brackets_stack.empty() == false)

什么是当你尝试将它与一个小括号进行比较时,输入brackObj的

提示:考虑何时创建一个新的支架实例。它们都是括号吗?它们都是开括号吗?

What is the type of brackObj when you try to compare it with a close bracket?
Hint: think about when you create each new instance of a Bracket. Are they all brackets? Are they all open brackets?


Quote:

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个错误。

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于用于检查输入中的平衡括号的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆