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