#include
int brace, brack, paren;
void in_quote(int c);
void in_comment(void);
void search(int c);
main()
{
int c;
extern int brace, brack, paren;
//声明全局变量
while((c = getchar()) != EOF){
if(c == '/'){
if((c = getchar()) == '*')//遇到注释
in_comment();
//去掉注释
else
search(c);
//
}else if(c == '\'' || c == '"')//遇到单引号或双引号
in_quote(c);
else
search(c);
if(brace < 0){//先有右括号的情况
printf("Unbalanced braces\n");
brace = 0;
}else if(brack < 0){
printf("Unbalanced brackets\n");
brack = 0;
}else if(paren < 0){
printf("Unbalanced parentheses\n");
paren = 0;
}
}
if(brace > 0)//输出完少了右括号的情况
printf("Unbalanced braces\n");
if(brack > 0)
printf("Unbalanced brackets\n");
if(paren > 0)
printf("Unbalanced parentheses\n");
}
void search(int c)
{
extern int brace, brack, paren;
if(c == '{')
++brace;
else if(c == '}')
--brace;
else if(c == '[')
++brack;
else if(c == ']')
--brack;
else if(c == '(')
++paren;
else if(c == ')')
--paren;
}
void in_comment(void)
{
int c, d;
c = getchar();
d = getchar();
while(c != '*' || d != '/'){
c = d;
d = getchar();
}
}
void in_quote(int c)//对引号之间的字符不进行左右括号的判断
{
int d;
while((d = getchar()) != c)
if(d == '\\')
getchar();
//将\后的一个字符忽略掉不进行上面的while里面的判断 防止是引号继而跳出这个while循环
【编写一个程序,查找C语言中的基本语法错误,如圆括号、方括号、花括号不配对等,正确处理引号与注释】}