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