本文整理汇总了C++中Bracket::Matchc方法的典型用法代码示例。如果您正苦于以下问题:C++ Bracket::Matchc方法的具体用法?C++ Bracket::Matchc怎么用?C++ Bracket::Matchc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bracket
的用法示例。
在下文中一共展示了Bracket::Matchc方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
std::string text;
getline(std::cin, text); // get the input string
std::stack <Bracket> opening_brackets_stack;
for (int position = 0; position < text.length(); ++position) {
char next = text[position];
if (next == '(' || next == '[' || next == '{') { // store the left bracket in the stack
Bracket parenStart(next, position);
opening_brackets_stack.push(parenStart);
}
else if (next == ')' || next == ']' || next == '}') { // if right bracket is detected
if (opening_brackets_stack.empty()) // if the stack is empty, which means no left bracket is stored
{
std::cout << (position + 1) << std::endl; // output the position of this right bracket, break loop
break;
}
else
{
Bracket parenStartOut = opening_brackets_stack.top(); // get the latest left bracket,
//remove it from stack
opening_brackets_stack.pop();
if(!parenStartOut.Matchc(next)) // check if the latest left bracket is matched with
// the current right bracket
{
std::cout << (position + 1) << std::endl; // if not, output the position of this right bracket, break loop
break;
}
}
}
if (position == text.length() - 1) // if reach the end of the string
{
if (!opening_brackets_stack.empty()) // if the stack is not empty, output the position of
//left bracket at the end of the stack (the first unmatched one)
{
Bracket firstUnMatched('(',0); // initialization, initial values don't matter here
while(!opening_brackets_stack.empty())
{
firstUnMatched = opening_brackets_stack.top();
opening_brackets_stack.pop();
}
std::cout << (firstUnMatched.position + 1) << std::endl;
}
else // if stack has no left bracket, which means all the brackets have been matched
{
std::cout << "Success" << std::endl;
}
}
}
return 0;
}
示例2: main
int main() {
bool isBalanced = true;
int errorPos = 0;
std::string text;
getline(std::cin, text);
std::stack <Bracket> opening_brackets_stack;
for (int position = 0; position < text.length(); ++position) {
char next = text[position];
if (next == '(' || next == '[' || next == '{') {
// Process opening bracket, write your code here
Bracket b = Bracket(next, position);
opening_brackets_stack.push(b);
}
if (next == ')' || next == ']' || next == '}') {
// Process closing bracket, write your code here
if( opening_brackets_stack.empty() == false )
{
Bracket b = opening_brackets_stack.top();
if ( b.Matchc(next) == false ) {
isBalanced = false;
errorPos = position;
break;
} else
opening_brackets_stack.pop();
} else
{
isBalanced = false;
errorPos = position;
break;
}
}
}
if(opening_brackets_stack.empty() == false && isBalanced == true)
{
isBalanced = false;
errorPos = opening_brackets_stack.top().position;
}
// Printing answer, write your code here
if ( isBalanced )
std::cout << "Success" << "\n";
else
std::cout << errorPos+1 << "\n";
return 0;
}