スタックなしで括弧、中括弧、大括弧の一致をチェック
3つのカウンタの初期値をゼロに設定し、それぞれ小かっこ、中かっこ、大かっこを記録します.最初のスキャンを開始し、左から右へ各文字を順次スキャンし、左かっこカウンタに1を加算し、右かっこカウンタに1を減算し、カウンタがゼロ未満になった場合、エラーを報告して終了します.スキャンが終了すると、3つのカウンタにゼロでないカッコが一致しない場合は、エラーを報告してチェックを終了します.
最初のスキャンは、3つのカッコ自体が正しく一致していることを正しく説明した場合に終了します.現在発生する可能性のあるエラーは、{(}{)},({[)]}など、異なるカッコで交差する可能性があります.
カウンタはゼロになり、左から右に各文字を順次スキャンし、右かっこに遭遇するまでカウンタに1を追加し、右かっこの前の文字を逆スキャンします.右括弧カウンタプラス1に遭遇し、左括弧カウンタマイナス1に遭遇し、最初と同じタイプの左括弧に遭遇するまで、このタイプのカウンタはゼロであり、この場合、他の2つの括弧カウンタがゼロでない場合は、クロスエラーが発生し、エラーを報告してスキャンを終了します.そうでない場合、カウンタはゼロになり、次の右かっこを右にスキャンし続け、文字列全体をスキャンするまで繰り返します.
最初のスキャンは、3つのカッコ自体が正しく一致していることを正しく説明した場合に終了します.現在発生する可能性のあるエラーは、{(}{)},({[)]}など、異なるカッコで交差する可能性があります.
カウンタはゼロになり、左から右に各文字を順次スキャンし、右かっこに遭遇するまでカウンタに1を追加し、右かっこの前の文字を逆スキャンします.右括弧カウンタプラス1に遭遇し、左括弧カウンタマイナス1に遭遇し、最初と同じタイプの左括弧に遭遇するまで、このタイプのカウンタはゼロであり、この場合、他の2つの括弧カウンタがゼロでない場合は、クロスエラーが発生し、エラーを報告してスキャンを終了します.そうでない場合、カウンタはゼロになり、次の右かっこを右にスキャンし続け、文字列全体をスキャンするまで繰り返します.
#define _using_goto_in_bracketsmatchingcheck_ 1
/*
:szsrc 。
: 1 , 0 。
*/
int bracketsmatchingcheck(const char *szsrc)
{
const char *ptr = szsrc;
char c;
char ch;
long parenthesis = 0; /* () */
long brackets = 0; /* [] */
long braces = 0; /* {} */
if (szsrc == NULL)
return 1;
while((int)(c = *ptr++)) /* */
{
scanfir:
if (c == '(')
{
++parenthesis;
#ifdef _using_goto_in_bracketsmatchingcheck_
if ((int)(c = *ptr++))
goto scanfir;
else
break;
#endif
}
else if (c == ')')
{
if (parenthesis == 0)
return 0;
--parenthesis;
#ifdef _using_goto_in_bracketsmatchingcheck_
if ((int)(c = *ptr++))
goto scanfir;
else
break;
#endif
}
else if (c == '[')
{
++brackets;
#ifdef _using_goto_in_bracketsmatchingcheck_
if ((int)(c = *ptr++))
goto scanfir;
else
break;
#endif
}
else if (c == ']')
{
if (brackets == 0)
return 0;
--brackets;
#ifdef _using_goto_in_bracketsmatchingcheck_
if ((int)(c = *ptr++))
goto scanfir;
else
break;
#endif
}
else if (c == '{')
{
++braces;
#ifdef _using_goto_in_bracketsmatchingcheck_
if ((int)(c = *ptr++))
goto scanfir;
else
break;
#endif
}
else if (c == '}')
{
if (braces == 0)
return 0;
--braces;
}
}
if (parenthesis != 0 || brackets != 0 || braces != 0) /* , */
return 0;
ptr = szsrc;
while((int)(ch = *ptr)) /* */
{
scansec:
if (ch == ']')
{
const char *ptr1 = ptr - 1;
parenthesis = 0;
brackets = 1;
braces = 0;
while(1)
{
if ((c = *ptr1--) == '[')
{
if ((--brackets) == 0)
{
if (parenthesis == 0 && braces == 0)
{
#ifdef _using_goto_in_bracketsmatchingcheck_
if ((int)(ch = *(++ptr)))
goto scansec;
else
return 1;
#else
break;
#endif
}
else
return 0;
}
}
else if (c == ']')
++brackets;
else if (c == ')')
++parenthesis;
else if (c == '(')
{
if (parenthesis == 0)
return 0;
--parenthesis;
}
else if (c == '}')
++braces;
else if (c == '{')
--braces;
}
}
else if (ch == '}')
{
const char *ptr1 = ptr - 1;
parenthesis = 0;
brackets = 0;
braces = 1;
while(1)
{
if ((c = *ptr1--) == '{')
{
if ((--braces) == 0)
{
if (parenthesis == 0 && brackets == 0)
{
#ifdef _using_goto_in_bracketsmatchingcheck_
if ((int)(ch = *(++ptr)))
goto scansec;
else
return 1;
#else
break;
#endif
}
else
return 0;
}
}
else if (c == '}')
++braces;
else if (c == ')')
++parenthesis;
else if (c == '(')
{
if (parenthesis == 0)
return 0;
--parenthesis;
}
else if (c == ']')
++brackets;
else if (c == '[')
--brackets;
}
}
++ptr;
}
return 1;
}