标准输入即从键盘输入进去的代码,使用getchar()/scanf()来获取字符,然后用putchar()打印字符。
代码中存在的花括号成对出现即 每有一个“{”就必须有一个“}”来匹配,并且如果先出现“}”,后边不用读取 “{”,直接输出不匹配。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch = 0;
int count = 0;
while ((ch = getchar()) != EOF) //getchar()获取字符,以整型返回。EOF(end of file)文件结束标志。
{
if (ch == '{')
count++;
//需判断}{形式,若先出现},count一定为0。比如:{}{}、{{{、}{}
if ((ch == '}') && (count == 0))
{
printf("不匹配\n");
return 0;
}
if ((ch == '}') && (count != 0))
count--;
}
if (count == 0)
printf("匹配!\n");
else
{
printf("不匹配!\n");
}
system("pause");
return 0;
}
当敲入一段字符,用“Enter”键确定写入,在按“ctrl+z”结束写入。
程序执行。