#include <stdio.h>
int main()
{
int count = 0;
int ch = 0;
while((ch=getchar()) != EOF)
{
if(ch == '{')
{
count++;
}
else if(ch == '}')
{
if(count > 0)
count--;
else if(count == 0)
{
printf("不匹配\n");
return 0;
}
}
}
if(count == 0)
printf("匹配\n");
else
printf("不匹配\n");
return 0;
}
编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现
本文介绍了一个使用C语言编写的简单程序,该程序能够检查输入字符串中的花括号是否正确匹配。通过逐个读取字符并跟踪打开的括号数量来实现这一目标。如果在没有匹配的打开括号的情况下遇到关闭括号,则会立即报告错误。

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



