验证终端输入的C代码中花括号的正确成对出现

来源:互联网 发布:ubuntu c语言调试工具 编辑:程序博客网 时间:2024/04/26 22:16

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


#include <stdio.h>#include<stdlib.h>//#include <string.h>int main(){int cout=0,num=0;char ch;char *p=&ch;while((ch=getchar())!=EOF){//gets(ch);switch (*p){case '{':cout++;break;case '}':num++;break;default:p++;   //无括号继续往后读字符        }       }if(cout==num){ printf("括号匹配\n");     system("pause");  //include<stdlib.h>      //getchar();}else{  printf("匹配失败\n");      system("pause");  //include<stdlib.h> //getchar();}  return 0;}


0 0
原创粉丝点击