8586 square brackets Matching Test, 8586 square brackets matching
Today, I took out the data structure learning code and shared it with netizens. I should be able to test it!
8586 bracket Matching Test
Time Limit: 1000 MS Memory Limit: 1000 K
Total Submit: 679 Accepted: 182
Type: Program Language: Not Limited
Description
Use the stack to compile the Matching Test Program for parentheses that meet the following requirements: assume that the expression can contain two kinds of parentheses: parentheses and square brackets. The nesting order is random, that is, ([] (). or [[] [] is in the correct format. [(] Or ([() or ()] is in an incorrect format. Enter an expression that contains the brackets to check whether the brackets are paired. Some check () functions are provided in this question. It is required to complete the check () function and complete the entire program.
Typedef char SElemType;
# Include "malloc. h"
# Include "stdio. h"
# Include "math. h"
# Include "process. h" // exit ()
# Define OK 1
# Define ERROR 0
# Define TRUE 1
# Define FALSE 0
Typedef int Status; // Status indicates the function type, and its value is the function result Status code, such as OK.
# Define STACK_INIT_SIZE 10 // initial storage space allocation
# Define STACKINCREMENT 2 // increment of storage space allocation
Struct SqStack
{
SElemType * base; // The base value is NULL before and after stack construction.
SElemType * top; // stack top pointer
Int stacksize; // The currently allocated storage space, in the unit of Elements
}; // Ordered Stack
Status InitStack (SqStack & S)
{
}
Status StackEmpty (SqStack S)
{
}
Status Push (SqStack & S, SElemType e)
{
}
Status Pop (SqStack & S, SElemType & e)
{
}
Void check ()
{// Checks whether brackets are paired with any input string
SqStack s;
SElemType ch [80], * p, e;
If (InitStack (s) // the stack is initialized successfully.
{
// Printf ("Enter the expression \ n ");
__________________________________;
P = ch;
While (* p) // does not reach the end of the string
Switch (* p)
{
Case '(':
Case '[':_______________________;
Break; // The left bracket is written into the stack, and p ++
Case ')':
Case ']': if (! StackEmpty (s) // stack is not empty
{
_________________________; // The top element of the stack is displayed.
If (* p = ')' & e! = '(' | ___________________&&___________________)
// The top element of the pop-up stack is not paired with * p
{
Printf ("isn' t matched pairs \ n ");
Exit (ERROR );
}
Else
{
__________________________;
Break; // jump out of the switch statement
}
}
Else // stack is empty
{
Printf ("lack of left parenthesis \ n ");
Exit (ERROR );
}
Default: ______________________; // no other characters are processed, and the pointer is moved back.
}
If (StackEmpty (s) // the stack is empty when the string ends.
Printf ("matching \ n ");
Else
Printf ("lack of right parenthesis \ n ");
}
}
Void main ()
{
Check ();
}
Input
Line 1: Enter an expression string that contains parentheses or square brackets and cannot exceed 80 characters.
Output
The first line: If the input expression matches the brackets, the output is "matching". If the expression does not match, the output is: "isn' t matched pairs ", or "lack of left parenthesis" or "lack of right parenthesis"
Sample Input
8*[3 * (35-23)]
Sample Output
Matching
Author
Yqm
Answer:
Typedef char SElemType;
# Include "malloc. h"
# Include "stdio. h"
# Include "math. h"
# Include "stdlib. h" // exit ()
# Define OK 1
# Define ERROR 0
# Define TRUE 1
# Define FALSE 0
Typedef int Status; // Status indicates the function type, and its value is the function result Status code, such as OK.
# Define STACK_INIT_SIZE 10 // initial storage space allocation
# Define STACKINCREMENT 2 // increment of storage space allocation
Struct SqStack
{
SElemType * base; // The base value is NULL before and after stack construction.
SElemType * top; // stack top pointer
Int stacksize; // The currently allocated storage space, in the unit of Elements
}; // Ordered Stack
Status InitStack (SqStack & S)
{
S. base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof (SElemType ));
If (! S. base) return FALSE;
S. top = S. base;
S. stacksize = STACK_INIT_SIZE;
Return OK;
}
Status StackEmpty (SqStack S)
{
If (S. base = S. top) return 1;
Else return 0;
}
Status Push (SqStack & S, SElemType e)
{
If (S. top-S. base> = S. stacksize)
{
S. base = (SElemType *) realloc (S. base, (S. stacksize + STACKINCREMENT) * sizeof (SElemType ));
If (! S. base) return FALSE;
S. top = S. base + S. stacksize;
S. stacksize + = STACKINCREMENT;
}
* (S. top) = e;
S. top ++;
Return OK;
}
Status Pop (SqStack & S, SElemType & e)
{
If (S. base = S. top)
Return FALSE;
-- S. top;
E = * (S. top );
Return OK;
}
Void check ()
{// Checks whether brackets are paired with any input string
SqStack s;
SElemType ch [80], * p, e;
If (InitStack (s) // the stack is initialized successfully.
{
// Printf ("Enter the expression \ n ");
Gets (ch );
P = ch;
While (* p) // does not reach the end of the string
Switch (* p)
{
Case '(':
Case '[': Push (s, * p ++ );
Break; // The left bracket is written into the stack, and p ++
Case ')':
Case ']': if (! StackEmpty (s) // stack is not empty
{
Pop (s, e); // The top element of the stack is displayed.
If (* p = ')' & e! = '(' | * P = ']' & e! = '[')
// The top element of the pop-up stack is not paired with * p
{
Printf ("isn' t matched pairs \ n ");
Exit (ERROR );
}
Else
{
P ++;
Break; // jump out of the switch statement
}
}
Else // stack is empty
{
Printf ("lack of left parenthesis \ n ");
Exit (ERROR );
}
Default: p ++; // other characters are not processed, and the pointer is moved back.
}
If (StackEmpty (s) // the stack is empty when the string ends.
Printf ("matching \ n ");
Else
Printf ("lack of right parenthesis \ n ");
}
}
Int main ()
{
Check ();
}