Expression brackets matching (stack), expression stack

Source: Internet
Author: User

Expression brackets matching (stack), expression stack
Description

Assume that an expression is composed of English letters (lower case), operators (+,-, *,/), and parentheses (circles). "@" is used as the end character of the expression. Compile a program to check whether the left and right parentheses in the expression match. If YES, "YES" is returned; otherwise, "NO" is returned ". The expression length is less than 255, and the left parentheses are less than 20.

Input/Output Format

Input Format:

The input file stack. in contains a row of data, that is, an expression.

Output Format:

The output file stack. out contains a row, that is, "YES" or "NO ".

Input and Output sample

Input example #1:

Stack. in

2 * (x + y)/(_x )@

Input example #2:

Stack. in

(25 + x) * (a + B )@

 

Output sample #1:

Stak. out

YES

Output sample #2:

Stak. out

NO

Ideas

Encounter "(", k ++; encounter ")", k --. If k is 0 at the end, YES is output; otherwise, NO is output.

Code # include <stdio. h> # include <string. h> char a [260]; int main () {int I = 0, k = 0, len; gets (a); len = strlen (a)-1; for (I = 0; I <= len; I ++) {if (a [I] = '(') k ++; if (a [I] = ') k --;} if (k = 0) printf ("YES"); else printf ("NO "); return 0;} View Code

 

Related Article

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.