Data Structure Application-brackets Matching Test

Source: Internet
Author: User

 

/* -------------------------------------------------------------------------------- Description of the matching program in parentheses: Check the matching program in parentheses. Assume that the expression can contain parentheses and square brackets. The nesting order is random, that is, ([] () or [([] [])]. the format is correct. [(]), [(), or ()] are incorrect. Methods for checking whether parentheses match can be described by the concept of "Expected urgent program. For example, consider the following sequence of parentheses: [([] [])] 1 2 3 4 5 6 7 8 when the computer accepts the first bracket, it looks forward to the appearance of the eighth matching bracket. However, the second bracket of the wait momentum, at this time, the first brace "[" can only be placed by the side temporarily, the third parenthesis "[", the program it expects to match is more urgent than the second bracket, then the second bracket can only be pulled by the side, let the third bracket, obviously the expectation of the second bracket is more urgent than the first bracket; after accepting the fourth bracket, the expectation of the third bracket is satisfied. After the removal, the expectation matching of the second bracket becomes the most urgent task at present ,...., and so on. This program is also an example of the data structure book, but it does not provide code, only provides ideas, so I also implement it .. However, this program is also quite fun. ------ Seed finished * // * ------------------------------------------------------------------------- useSqStack. c function: Provides the test code edited by Seed, 2011 ------------------------------------------------------------------- */# include <stdio. h> # include "match. h "int main (void) {printf (" welcome to use the bracket matching program: \ n "); BranketMatch (); printf (" Bye "); return 0;}/* ------------------------------------------------------- match. h function: Provides function declaration and type definition --------------------------------------------------- */# ifndef MATCH_H _ # define MATCH_H_typedef int Status; typedef char SElemType; # define OK 1 # define ERROR 0 # define TRUE 1 # define FALSE 0 # define OVERFLOW-1 # define STACK_ININ_SIZE 100 # define STACKINCREMENT 10 # define CMP (ch) (ch! = ']' & Ch! = ') # Define SUCCESS (ch, e) (' = e & ')' = ch) | ('[' = e & ch = ']') # define ELSE (ch) (ch! = ']' & Ch! = ')' & Ch! = '[' & Ch! = '(') Typedef struct {SElemType * base; SElemType * top; int stacksize;} SqStack; Status DestoryStack (SqStack * S); Status ClearStack (SqStack * S ); status Push (SqStack * S, SElemType e); Status Pop (SqStack * S, SElemType * e); Status GetTop (SqStack S, SElemType * e ); status StackEmpty (SqStack S); int cmp (SqStack * S, SElemType ch); Status InitStack (SqStack * S); void BranketMatch (); void PrintStack (SqStack * S); # Endif/* ------------------------------------------------------- SqStack. function c: provides methods for implementing ----------------------------------------------------- */# include <stdio. h> # include <string. h> # include "match. h "# include <stdlib. h> Status DestoryStack (SqStack * S) {free (S-> base); S-> base = S-> top = NULL; return OK ;} // DestoryStackStatus ClearStack (SqStack * S) {S-> top = S-> base; return OK;} // ClearStackStatus Push (SqStack * S), SElemType e) {if (S-> top-S-> base> = S-> stacksize) {S-> base = (SElemType *) realloc (S-> base, (S-> stacksize + STACKINCREMENT) * sizeof (SElemType); if (NULL = S-> base) {puts ("ERROR"); exit (OVERFLOW );} s-> top = S-> base + S-> stacksize; S-> stacksize + = STACKINCREMENT;} * (S-> top) ++ = e; return OK ;} // PushStatus Pop (SqStack * S, SElemType * e) {if (S-> top = S-> base) return ERROR; * e = * -- S-> top; return OK;} // PopStatus GetTop (SqStack S, SElemType * e) {if (S. top = S. base) return ERROR; * e = * (S. top-1); return OK;} // GetTopStatus StackEmpty (SqStack S) {return (S. base = S. top);} // StackEmpty # define CMP (ch) (ch! = ']' & Ch! = ') // # Define SUCCESS (ch, e) (' = e & ')' = ch) | ('[' = e & ch = ']') # define ELSE (ch) (ch! = ']' & Ch! = ')' & Ch! = '[' & Ch! = '(') Int cmp (SqStack * S, SElemType ch) {SElemType e; if (ELSE (ch) {return-1;} else if (StackEmpty (* S) & CMP (ch) {return TRUE;} else {GetTop (* S, & e); if (SUCCESS (ch, e) return FALSE; else if (! SUCCESS (ch, e) & (ch = ']' | ch = ') return-1;} return TRUE ;} // cmpStatus InitStack (SqStack * S) {S-> base = (SElemType *) malloc (STACK_ININ_SIZE * sizeof (SElemType); if (NULL = S-> base) {puts ("ERROR"); exit (OVERFLOW);} S-> top = S-> base; S-> stacksize = stack_in_size; return OK ;} // InitStackvoid BranketMatch () {SqStack S; SElemType ch; InitStack (& S); printf ("Enter brackets, for example, '(', '(', '[', ']': "); while ((Ch = getchar ())! = 'Q') {switch (cmp (& S, ch) {case 0: Pop (& S, & ch); puts ("matched"); break; case 1: Push (& S, ch); puts ("failed to match, change urgency"); break; default: puts ("invalid input. "); break;} // switch-casewhile (getchar ()! = '\ N') continue; PrintStack (& S); printf ("\ n please input brackets, for example, '(', '(', '[', ']': ");} // whileClearStack (& S); DestoryStack (& S );} // BranketMatchvoid PrintStack (SqStack * S) {SElemType * temp; temp = S-> base; printf ("current stack content: \ n"); while (temp! = S-> top) {printf ("% 2c", * temp); temp ++;} // PrintStack

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.