5

作为一项额外的编程挑战,我被要求查看大括号是否匹配这样的随机字符串或字符:{1+1} 这将返回 1,而 {1+1}) 将返回 0。这就是我所拥有的到目前为止,但它似乎没有做任何事情。任何帮助都会很棒?谢谢

//bonus.cpp
#include <iostream>
#include <string>
#include <queue>
#include <stack>

using namespace std;

int checkBraces (string s)
{
    //int myLength = s.length();
    std::stack<int> stack;
    char d;

    for (int i = 0; i < s.length(); i++)
    {
        char c = s[i];

        if (c == '(')
        {
            stack.push(c);
        }
        else if (c == '[') 
        {
            stack.push(c);
        }
        else if (c == '{')
        {
            stack.push(c);
        }

        else if (c == ')')
        {
            if (stack.empty())
            {
                return false;
            }
            else
            {
                d = stack.top();
                stack.pop();
                if (d != '(')
                {
                    return false;
                }
            }
        }

        else if (c == ']')
        {
            if (stack.empty())
            {
                return false;
            }
            else
            {
                d = stack.top();
                stack.pop();
                if (d != '[')
                {
                    return false;
                }
            }
        }
        else if (c == '}')
        {
            if (stack.empty())
            {
                return false;
            }
            else
            {
                d = stack.top();
                stack.pop();
                if (d != '{')
                {
                    return false;
                }
            }
        }
    }

    if (stack.empty()) return true;
    else return false;

}


int main()
{
    cout << "This program checks brace ([{}]) matching in a string." << endl;

    checkBraces ("{1+1}");

}
4

4 回答 4

6

是什么让你觉得它什么都没做?确实如此。它检查大括号,但你没有对返回做任何事情checkBraces,顺便说一句,它应该返回 a bool,而不是 a int

你的意思可能是这样的:

if (checkBraces ("{1+1}"))
   cout << "matching";
else
   cout << "not matching";

专业提示:学习如何使用调试器。在开始编写“hello world”以外的任何代码之前,您应该学习如何调试。

于 2012-12-07T09:36:53.220 回答
3

作为已经说过的补充,我想说您可以减少代码量。无论如何,您将字符放入堆栈中,为什么不使用std::stack<char>?

您可以将大括号保存到另一个字符串中,以使用std::algorithms之一自动比较它

const std::string openingBraces("{[(");
const std::string closingBraces("}])");

if (std::find(openingBraces.begin(), openingBraces.end(), currentChar) != openingBraces.end())
    yourStack.push(currentChar);
else if (std::find(closingBraces.begin(), closingBraces.end(), currentChar) != closingBraces.end())
{
    // check if currentChar is matching the one on top of your stack
}

我还没有写完所有东西,因为最好自己找到答案。

于 2012-12-07T10:01:16.317 回答
2

您应该做的最低限度是打印 checkBraces 的结果。

于 2012-12-07T09:38:24.247 回答
1

but it doesn't seem to do anything

It does do something. It prints This program checks brace ([{}]) matching in a string..

You are calling checkBraces ("{1+1}") but you aren't doing anything with the returned value. Since this call can be optimized away, you are in a sense correct that your program doesn't seem to do anything.

So make it do something. Print the string that is to be tested, then print the result of the test. Once you have done that, you should test, and when you're done with that, you should test some more. Don't just test easy cases such as {i+1}. Test convoluted cases that should pass, and also test cases that should fail.

Learning how to test and learning how to debug are just as important skills (if not more important skills) as is learning how to write code.

于 2012-12-07T10:04:15.170 回答