C Language:: Verify curly braces appear in pairs

Source: Internet
Author: User
Tags array definition

Topic Requirements

Write a program that reads the C source code from the standard input (terminal) and verifies that all curly braces are correctly paired.

Note: You don't have to worry about the curly braces inside the comment, inside the string constant, and in the form of a character constant.

Algorithm analysis

Let's first determine if the number of left curly braces is equal.

If the left and right curly braces vary in number, it is definitely not true!

If the left and right curly braces are equal in number, must be a pair???

It doesn't seem to be.

Let's give an example of what might happen when the left and right curly braces are equal:

1.{{{{{}}}} (Match)

2.{}{}{}{}{} (Match)

...

3.}}}}}{{{{{(not matched)

4.{{{{}}}}}{(not matched)

......

In the 1th and 2nd cases, the number of curly braces is equal and matched, so we don't have to analyze it.

The 3rd and 4th cases, which are equal but mismatched in the number of curly braces in the left and right, we need to find out what they have in common.

With a little analysis, we can draw the following conclusions:

When the right curly brace appears, if the number of left curly braces is less than the number of right curly braces, the left and the curly braces do not match.

Some people may not understand the above conclusions, I would like to explain in detail:

(If you indicate that you have understood the conclusion, please jump directly to the algorithm summary section)

Let's first analyze the situation 3:

When we see the first right curly brace, we find that it doesn't have a left curly brace in front of it.

Therefore, situation 3 must belong to the case of mismatched curly braces.

Let's analyze the situation 4:

We look at the 1th right curly brace and find that it has 4 left curly braces in front of it;

2nd right curly brace with 4 left curly braces in front

......

Until the 5th right curly brace, preceded by only 4 left curly braces.

Therefore situation 3 also belongs to the case that the left and right curly braces do not match.

Algorithm Summary

Based on the above analysis, we draw the following conclusions:

It takes two conditions to determine whether the left and right curly braces appear in pairs:

1. The left and right curly braces must be equal in number.

2. When the right curly brace appears, the number of left curly braces must be greater than the right curly brace.

Algorithm Transfer Program

According to the requirements of the topic, we need to get the data from the standard input first.

We can use the scanf () function, the GetChar () function, the gets () function ...

(Do not know these functions of children's shoes, please click the function name directly)

Both the scanf () function and the Get () function need to specify a character array to store.

If you use these two functions, you have to define a character array.

When you define a character array, you face an array overflow or wasted space.

(the array definition is small, it may overflow, the definition is large, and the space is wasted.)

Therefore, it is most appropriate to use the GetChar () function.

The GetChar () function can only accept one character at a time from the console, so we need to work with loops.

So, we use the GetChar () function to get the data from the standard input through the loop operation.

You can then complete this code:

Define an integer variable to receive the console data int ch = 0;//hint Information printf ("Please enter a character to end with Ctrl + Z: \ n");//Read the character from the console and determine if the loop condition is met. while ((Ch=getchar ())!=eof) {...//determine if the left and right curly braces match the code}

Next we begin to write the code that analyzes whether the left and right curly braces match.

According to the algorithm, we can set two int variables left and right, respectively, to record the number of opening curly braces and the number of closing curly braces.

If the left curly brace is encountered, then left++;

If the right curly brace is encountered, we first have to determine if the number of left curly braces is greater than the right curly brace, right++, or exit directly.

Defines an integer variable used to receive console data int ch = 0;//defines an integer variable used to calculate the number of left curly braces for an int. = 0;//defines an integer variable used to calculate the number of closing curly braces int right = 0;//hint information printf ("Please enter a paragraph of characters End With Ctrl + Z: \ n ");//Read characters from the console and determine if the loop condition is met. while ((Ch=getchar ())!=eof) {//encounters a left curly brace, left+1 if (ch== ' {') {left++;} Encountered right curly brace if (ch== '} ') {//left brackets number greater than right curly braces right+1 if (left>right) {right++;} else{//Otherwise this is the case//{{{{}}}}}{//}}}}}{{{{{//.....//Direct Exit program printf ("Mismatch!\n"); return 0;}} If the program can go here//it has ruled out}}}}{{{{this special case//If the number of left curly braces equals the number of right curly braces if (right==left) {printf ("match!\n");} else{printf ("Mismatch!\n");}

The final complete code is as follows:

#include <stdio.h>int main () {///define an integer variable to receive the console data int ch = 0;//defines an integer variable used to calculate the number of left curly braces for an int = 0;//defines an integer variable to be used to calculate the number of right curly braces int right = 0;//message printf ("Please enter a character to end with Ctrl + Z: \ n");//Read the character from the console and determine if the loop condition is met. while ((Ch=getchar ())!=eof) {//encounters a left curly brace, left+1 if (ch== ' {') {left++;} Encountered right curly brace if (ch== '} ') {//left brackets number greater than right curly braces right+1 if (left>right) {right++;} else{//Otherwise this is the case//{{{{}}}}}{//}}}}}{{{{{//.....//Direct Exit program printf ("Mismatch!\n"); return 0;}} If the program can go here//it has ruled out}}}}{{{{this special case//If the number of left curly braces equals the number of right curly braces if (right==left) {printf ("match!\n");} else{printf ("Mismatch!\n");} return 0;}
Program Optimization

Writing here, it's like it's over?

In fact.... And no!

In the above procedure, we used two variables left and right to record the number of curly braces.

In fact, we don't need to know the exact value of left and right.

Therefore, we can use a variable to record the state of the left and right curly braces.

We define an integer variable count.

If you encounter left curly braces, count++;

If you encounter a closing curly brace, determine if count is greater than 0, or exit directly if Count--,.

The final code is as follows:

Read this article, if you have not yet how to use the C language to verify that curly braces are paired ....

Please pay attention to the public number Gxdydd, leave a message under the public number, I will definitely reply when I am free!

This article is from the high-minor blog, if you need to reprint, please specify the source!

Original address: http://gaoxiaodiao.cpm/p/9.html

      Extended Reading
    1. scanf () function functions, prototypes, usages, and instances
    2. GetChar () functions, prototypes, usages and examples
    3. Gets () function function, prototype, usage and example

C Language:: Verify curly braces appear in pairs

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.