Bracket Matching--stack

Source: Internet
Author: User

#include <iostream>
using namespace Std;
typedef char Stackentry;
const int maxstack = maximum size of 100;//stack
Class stack{
Public
Stack ();
void Pop ();
void push (const stackentry &item);
void Top (Stackentry &item) const;
BOOL empty () const;
Private
int count;
Stackentry Data[maxstack];
};

void stack::p ush (const stackentry &item)//If the stack is not full, the element item is pressed into the top of the stack, otherwise the error
{
if (Count >= maxstack)
cout << "Stack overflow, cannot be pressed into the element. ";
Else
data[count++] = Item;
}

void stack::p op ()//If the stack is not empty, the top element of the stack is deleted, otherwise the error
{
if (count = = 0)
cout << "Stack Overflow, unable to eject elements. ";
Else
--count;
}

void Stack::top (Stackentry &item) const//If the stack is not empty, remove the top element of the stack and put it in the item, otherwise the error
{
if (count = = 0)
cout << "Stack Overflow, unable to read elements. ";
Else
item = data[count-1];
}

BOOL Stack::empty () const//Judging whether the stack is empty
{
if (Count > 0)
return false;
return true;
}

Stack::stack ()//Build function, initialize an empty stack
{
Count = 0;
}

int main ()
{
stack openings;
char symbol;
BOOL match = true;
cout << "Please enter a line of bracketed text:" << Endl;
while (match && (symbol = Cin.get ())! = ' \ n ') {
if (symbol = = ' {' | | | symbol = = ' (' | | symbol = = ' [')
Openings.push (symbol);
if (symbol = = '} ' | | Symbol = = ') ' | | Symbol = = '] ')
if (Openings.empty ()) {
cout << "Mismatched closing parenthesis detected" << symbol << Endl;
Match = false;
System ("pause");
return 0;
}
Else
{
Char Topsymbol;
Openings.top (Topsymbol);
Openings.pop ();
Match = (symbol = = '} ' &&topsymbol = = ' {')
|| (symbol = = ') ' &&topsymbol = = ' (')
|| (symbol = = '] ' &&topsymbol = = ' [');
if (!match) {
cout << "Bracket Type mismatch" << match << symbol << Endl;
System ("pause");
return 0;
}
}
}
if (!openings.empty ())
{
cout << "Extra left parenthesis detected." << Endl;
System ("pause");
}
Else
{
cout << "All brackets are perfectly matched, thank you!" << Endl;
System ("pause");
}
return 0;
}

Bracket Matching--stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.