#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