编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。
#include <stdio.h>
int main()
{
int ch = 0;
int count = 0;
while ((ch=getchar())!=EOF)
{
if (ch == '{')
{
count++;
}
else if (ch == '}' && count == 0)
{
printf("不匹配\n");
return 0;
}
else if (ch == '}' && count != 0)
{
count--;
}
}
if (count == 0)
{
printf("匹配\n");
}
else
{
printf("不匹配\n");
}
return 0;
}
程序运行结果如下:

本文介绍了一个简单的C程序,用于验证C源代码中所有花括号是否正确配对。程序通过标准输入读取代码并跟踪左花括号的数量,确保每个右花括号都有对应的左花括号。
3349

被折叠的 条评论
为什么被折叠?



