标签:c语言
(三种if的情况考虑完整,注意解决问题的思想)
#include <stdio.h>
int main()
{
int count=0;
char ch;
while((ch=getchar())!=EOF) //直到出现文件结束标志,不再进入循环
{
if(ch==‘{‘)
{
count++;
}
if(ch==‘}‘&&count==0) //前无‘{‘,而后有‘}‘,即不匹配
{
printf("不匹配\n");
return 0;
}
if(ch==‘}‘&&count!=0) //前有‘{‘,后有‘}‘
{
count--;
}
}
if(count==0) //即前面的‘{‘和后面的‘}‘数量相抵消,匹配了
{
printf("匹配\n");
}
else
{
printf("不匹配");
}
return 0;
}
本文出自 “感悟” 博客,转载请与作者联系!
编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现
标签:c语言
原文地址:http://10707684.blog.51cto.com/10697684/1696389