Bracket matching detection-stack Application

Source: Internet
Author: User

Cause:
Monday was my girlfriend's birthday. I had no choice but to debug the company's interface, so I couldn't help myself. As a result, I worked overtime at the company on weekends, today I went to work with my girlfriend on Monday. Alas, I am so grateful to my girlfriend. The height of a man depends largely on the women around me. I wish my baby a happy birthday.
Question requirements:
Contains left brackets, right brackets, and upper-case letters in a string (up to 100 characters in length). This is required (same as a common arithmetic operator) any left parenthesis matches the right parenthesis that is located on the right and closest to it from the inside to the outside. Write a program, find the left and right parentheses that cannot be matched, output the original string, and mark the following line with the unmatched parentheses. The left brackets that cannot be matched are marked with "$", and the right brackets that cannot be matched are marked "? "Annotation.
Input:
The input includes multiple groups of data. Each group contains one row of data. It contains only left and right brackets and uppercase and lowercase letters. the string length cannot exceed 100 characters.
Note: cin. getline (str, 100) can contain up to 99 characters!
Output:
For each group of output data, two rows are output. The first row contains the original input characters. The second row consists of "$ ","? "And space," $ "and "? "Indicates that the corresponding left and right brackets cannot match.
Sample input:
) (Rttyy () sss )(
Sample output:
) (Rttyy () sss )(
? ? $
Algorithm idea:
Bracket matching is a typical stack application, so I use the chain stack, the idea is as follows:

(1) traverse the input string for the first time,
1. If it is a left bracket, it is added to the stack and the corresponding position of the array is marked with spaces.
2. if it is a right brace, check whether the stack is empty. If it is not empty, check that there is a corresponding left brace and both the stack is out; if it is empty, there is no corresponding left brace, mark the array '? '
(2) The second traversal of the input string,
1. If the brackets are left
2. if it is a left parenthesis, check whether the stack is empty. If it is not empty, check that there is a corresponding right brace and the stack is out at the same time. If it is empty, there is no corresponding left brace, mark the array with '$'
The code is implemented as follows:
[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
 
// Use the data structure of the chain Stack
Struct stacknode
{
Char bracket;
Struct stacknode * next;
};
Typedef struct
{
Struct stacknode * top;
} Linkstack;
 
/**
* Description: Initialize the chain stack.
*/
Void initstack (linkstack * s)
{
S-> top = NULL;
}
 
/**
* Description; determines whether the stack is empty.
*/
Int stackempty (linkstack * s)
{
Int flag;
 
Flag = (s-> top = NULL )? 1: 0;
 
Return flag;
}
 
/**
* Description: The inbound operation.
*/
Void push (linkstack * s, char str)
{
Struct stacknode * p;
P = malloc (sizeof (struct stacknode ));
P-> bracket = str;
P-> next = s-> top;
S-> top = p;
}
 
/**
* Description: Out-of-stack operations
*/
Char pop (linkstack * s)
{
Char str;
Struct stacknode * p = s-> top;
Str = p-> bracket;
S-> top = p-> next;
Free (p );
Return str;
}
 
Int main ()
{
Char input [101];
Char flag [101];
Char ch;
Int I, len, j, k;
Linkstack * s;
S = malloc (sizeof (linkstack ));
 
While (scanf ("% s", input )! = EOF)
{
Len = strlen (input );
Initstack (s );
For (I = 0; I <len; I ++)
{
Switch (input [I])
{
Case '(':
// Left parenthesis into the stack
Push (s, input [I]);
Flag [I] = '';
Break;
Case ')':
// Judge whether the stack is empty
If (stackempty (s ))
{
Flag [I] = '? ';
} Else
{
Flag [I] = '';
Ch = pop (s );
}
Break;
Default:
Flag [I] = '';
Break;
}
}
Initstack (s );
For (I = len-1; I> = 0; I --)
{
Switch (input [I])
{
Case ')':
// Right bracket into Stack
Push (s, input [I]);
Break;
Case '(':
// Judge whether the stack is empty
If (stackempty (s ))
{
Flag [I] = '$ ';
} Else
{
Flag [I] = '';
Ch = pop (s );
}
Break;
Default:
Break;
}
}
 
// Print the output
Printf ("% s \ n", input, flag );
Memset (input, '\ 0', sizeof (input ));
Memset (flag, '\ 0', sizeof (flag ));
}
 
Return 0;
}

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.