Application of the sequence of C ++ --- matching brackets ,---

Source: Internet
Author: User

Application of the sequence of C ++ --- matching brackets ,---

At the beginning, I learned the data structure and wrote a classic application using the delimiter, matching the brackets.


Algorithm ideas:

When you input a string, press '(', '[' into the sequence, and then press ') ''']' to stack the sequence and perform matching by parentheses. If the matching is successful, otherwise, the program ends and the mismatch information is entered,

If ') '']' matches all, it determines whether the Delimiter is null. If it is null, the input matches. Otherwise, the number of output symbols does not match.


# Include <iostream> using namespace std; # define max 100 struct stack {int Top; int MaxSize; char * element ;}; typedef struct stack Stack; void Init (Stack * S, int n) {if (n> 0) {S-> Top =-1; S-> MaxSize = n; s-> element = new char [S-> MaxSize];} bool IsFull (Stack * S) {return (S-> Top = S-> MaxSize-1 );} bool IsEmpty (Stack * S) {return (S-> Top =-1);} void push (Stack * S, char x) {if (! IsFull (S) S-> element [++ S-> Top] = x; else {cout <"full" <endl ;}} char pop (Stack * S) {if (! IsEmpty (S) return S-> element [S-> Top --]; else {cout <"empty" <endl; return-1 ;}} int main () {Stack * S = new Stack; Init (S, max); bool flag = true; bool flag_1 = true; bool flag_2 = true; cout <"enter a string:" <endl; char ch; while (ch = getchar ())! = '\ N') {switch (ch) {case' (': push (S, ch); break; case' [': push (S, ch); break; case ')': {if (pop (S )! = '(' & Flag_2) {cout <"')'" <"mismatch" <endl; flag_1 = false; flag = false; break ;} else {continue;} case ']': {if (pop (S )! = '[' & Flag_1) {cout <"']'" <"symbol mismatch" <endl; flag_2 = false; flag = false; break ;} else {continue ;}}} if (! IsEmpty (S) & flag) {cout <"quantity mismatch" <endl;} else if (flag) {cout <"matched" <endl ;} system ("pause"); return 0 ;}

They are simple definitions and functions.


C-language coding of brackets matching with stacks

# Define STACK_SIZE 160 // stack space size
# Define FLASE 0
# Define TRUE 1

Typedef int BOOL;
/** Stack definition **/
Typedef struct
{
Int top; // stack top pointer
Int ele [STACK_SIZE];
} Stack;

Void Initializing (Stack ** s) // The initialization Stack is empty.
{
* S = (Stack *) malloc (sizeof (Stack ));
(* S)-> top =-1;
}

Void Destory (Stack ** s) // destroy the Stack
{
Free (* s );
* S = NULL;
}

Void Clear (Stack * s) // Clear the Stack
{
S-> top =-1;
}

Char GetTop (Stack * s) // gets the top element of the Stack.
{
If (s-> top>-1 & s-> top <STACK_SIZE)
{
Return s-> ele [s-> top];
}
Else
Return '\ 0 ';
}

BOOL Isempty (Stack * s) // determines whether the Stack is empty.
{
If (-1 = s-> top)
Return FLASE;
Else
Return TRUE;
}

Void Push (Stack * s, char ch) // inbound Stack
{
If (STACK_SIZE-1 = s-> top)
{
Printf ("the stack space is full! ");
Return;
}
Else
{
+ + S-> top;
S-> ele [s-> top] = ch;
}
}

Char Pop (Stack * s) // output Stack
{
Char ch;
If (-1 = s-> top)
{
Printf ("Empty stack! ");
Return '\ 0 ';
}
Else
{
Ch = s-> ele [s-> top];
-- S-> top;
Return ch;
}
}

/****************************/

BOOL Match (char * ch) // use the stack to determine whether the brackets Match
{
Int I = 0;
Stack * s;
Initializing (& s); // initialize the stack
If (ch = NULL | * ch = '\ 0') // returns a NULL pointer or a NULL string.
Return TRUE;

While (ch [I]! = '\ 0 ')
{
If (ch [I] = '(') // left parenthesis ...... the remaining full text>

What is the error in using the parentheses matching (C Language Program) on the stack?

# Include <stdio. h>
# Include <stdlib. h>
# Include "iostream. h"
# Deprecision MAX 100

Typedef char Elem;

Typedef struct
{Elem a [MAX];
Int top;
} Sqstack;
Sqstack s1;

Void init (Sqstack * s );
Elem pop (Sqstack * s );
Void push (Sqstack * s, Elem e );

Void init (Sqstack * s)
{
S-> top =-1;
}
Void push (Sqstack * s, Elem e)
{
If (s-> top = MAX-1)
Printf ("stack full ");
Else
{
S-> top ++;
S-> a [s-> top] = e;
}
}
Elem pop (Sqstack * s)
{
Elem x;
If (s-> top =-1)
{
Printf ("stack underflow"); // x =-1;
}
Else
{
X = s-> a [s-> top];
S-> top --;
Return x;
}
}

Main ()
{
Sqstack s;
Init (& s );
Int j;
Char ch;
Elem e, x;
// Int top;
Sqstack s1;
Printf ("enter all the symbols, for example, {[[()]} can be paired, for example 【{]()} unable to pair ");
Printf ("the sequence of parentheses is as follows: \ n ");
Do
{
Ch = getchar ();
Switch (ch)
{
Case '(': push (& s, ch); break;
Case '[': push (& s, ch); break;
Case '{': push (& s, ch); break;
Case ')': {if (s. a [s. top] = '(')
Pop (& s );
// Else
// Printf ("no"); // return 0;
}
Case ']': {if (s. a [s. top] = '[')
Pop (& s );
// Else
// Printf ("no"); // return 0;
}
Case '}': {if (s. a [s. top] = '{')
Pop (& s );
// Else
// Printf ("no"); // return 0;
}
}
} While (ch! = '\ N ');
// Printf ("YES ");
If (s. top =-1)
Printf ("pairing successful") ...... remaining full text>

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.