编写一个程序,它从标准输入读取c源代码,并验证所有的花括号都正确地成对出现。

编写一个程序,它从标准输入读取c源代码,并验证所有的花括号都正确地成对出现。注意:你不必担心注释内部、字符串常量内部和字符常量形式的花括号。

编写代码如下:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

int main()

{

    int ch;

    int braces=0;

    while((ch=getchar())!=EOF)

    {

      if(ch=='{')

      braces++;

      if(ch=='}')

       if(braces==0) printf("Extra closing brace!\n");

       else 

          braces--;

    }

    if(braces>0)

    printf("%d braces not matched",braces);

    system("pause");

    return 0;

}

 

通过这个小程序的应用,你可以联想到多种多个括号之间的配对情况,比如{ ( {} ) }...........................2011-11-25       10:37:07

你可能感兴趣的:(源代码)