Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

编写一个程序,它从标准输入读取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


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史