#include <stdio.h>
int paren = 0, brace = 0, brack = 0;
void search(int c);
void findend(void);
void echo_quote(int before1);
main()
{
int c;
extern int paren, brace, brack;
while ((c = getchar())!=EOF){
if(c == '/'){
if((c = getchar()) == '*')
findend();
else
search(c);
}else if (c == '/'' || c == '"')
echo_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");
}
else if(brack > 0){
printf("Unbalanced brackets/n");
}
else if(paren > 0){
printf("Unbalanced parentheses/n");
}
return 0;
}
void search(int c)
{
extern int paren, brace, brack;
if(c == '(')
++paren;
else if(c == ')')
--paren;
else if(c == '{')
++brace;
else if(c == '}')
--brace;
else if(c == '[')
++brack;
else if(c == ']')
--brack;
}
void findend(void)
{
int a, b;
a = getchar();
b = getchar();
while (a != '*' || b != '/')
{
a = b;
b = getchar();
}
}
void echo_quote(int c)
{
int d;
while((d = getchar()) != c)
{
putchar(d);
if(d = '//')
getchar();
}
}
本题的题意是检查几个括号的配对,在同一行只检查后面的括号多加时才进行错误提示,在全部输入结束后对所有的左括号多于右括号进行错误提示