2012-12-07 306 views
5

我已经被要求作为奖金编程挑战来查看大括号是否匹配随机字符串或像这样的字符:{1 + 1}这将返回1,而{1 + 1})会返回0. 这是我迄今为止的,但它似乎没有做任何事情。任何帮助将是伟大的?谢谢C++检查大括号是否匹配

//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}"); 

} 
+2

您是否尝试单步执行调试器中的代码以查看实际发生了什么? * –

+0

*似乎没有做任何事情* - 可以在这里使用更多的信息。 –

+0

你有相当多的冗余。也许你应该有一个'bool pop_if_possible(std :: stack ,char)'方法,这样你就可以编写'if(c ==')'&&!pop_if_possible(stack,'(')){return false; }' – MSalters

回答

1

,但它似乎并没有做任何事情

它做一些事情。它打印This program checks brace ([{}]) matching in a string.

你打电话给checkBraces ("{1+1}"),但你没有做任何与返回的值。由于这个调用可以被优化,所以从某种意义上说,你的程序似乎没有任何作用。

因此,让它做一些事情。打印要测试的字符串,然后打印测试结果。一旦你做完了,你应该测试一下,当你完成这个时,你应该测试更多。不要只测试简单的案例,如{i+1}。测试应该通过的错综复杂的案例,以及应该失败的测试案例。

学习如何测试和学习如何进行调试与学习如何编写代码一样重要的技能(如果不是更重要的技能)。

+0

我不能相信我所要做的只是cout << checkBraces(input);我的程序非常感谢。我已经测试了几个例子,因为我知道它应该返回1。 –

6

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

难道您原本是这样的:

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

人提示:学习如何使用调试器。在开始编码之前,您应该学习如何进行调试,而不仅仅是“hello world”。

+0

'“{(})”'被解释为:当遇到''}'时,堆栈的顶部将是''(''。 – MSalters

+0

@ MSalters啊,这是真的,我错过了。 –

+0

好吧好吧,使用你的IF语句你给我提供了我的程序似乎工作得很好。我只需要能够提示用户输入一个字符串,并返回1或0.我认为我可以通过使该方法是一个布尔而不是一个int来完成? –

2

您应该做的最低要求是打印checkBraces的结果。

2

作为已经说过的内容,我会说你可以减少代码量。无论如何,你把字符放入你的栈中,为什么不用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 
} 

我没有写一切,因为它总是最好自己去寻找答案一个,它会自动进行比较。