C 프로그램 괄호 일치 검사 (상세 설명 포함)
5205 단어 DSP
주의:
1. 괄호 가 일치 하지 않 을 수 있 는 것 을 제외 하고 입력 한 C 소스 프로그램 은 다른 문법 오류 가 없습니다.
2. 문자 상수, 문자열 상수 및 주석 에서 괄호 는 처리 되 어 서 는 안 됩 니 다. 주석 은 한 줄 주석 / / 여러 줄 / * * / 주석 을 포함 합 니 다.
3. 문자 와 문자열 상수 에는 특별한 전의 문자 (\, \) 가 포함 되 어 있 지 않 습 니 다.
5. 프로그램 에 의미 있 는 괄호 가 나타 나 는 개 수 는 200 개 를 넘 지 않 는 다.
일치 하지 않 는 판단 규칙:
1. 일치 하지 않 는 오른쪽 괄호 (')' 또는 '}' 을 만 났 을 때 이 오른쪽 괄호 와 있 는 줄 번 호 를 출력 합 니 다.
2. 프로그램 처리 가 끝 났 을 때 일치 하지 않 는 왼쪽 괄호 가 존재 할 때 이 왼쪽 괄호 와 있 는 줄 번 호 를 출력 합 니 다.
【 입력 형식 】
현재 디 렉 터 리 아래 파일 example. c 를 열 고 괄호 가 일치 하 는 지 확인 합 니 다.
[출력 형식]
괄호 가 일치 하지 않 을 경우 출력 은 먼저 일치 하지 않 는 괄호 와 그 소재 의 줄 번 호 를 판단 할 수 있 습 니 다. 괄호 가 일치 하지 않 을 때 아래 요구 에 따라 관련 정 보 를 출력 합 니 다.
without maching at line
그 중에서 '{', '}', '(') '등 기호 로 이 기호 가 있 는 줄 번호 이다.
전체 프로그램 괄호 가 일치 하면 다음 순서에 따라 괄호 가 일치 하 는 상황 을 출력 하고 중간 에 빈 칸 이 없습니다.
(){(()){}}
[샘플 입력 1]
현재 디 렉 터 리 에 파일 example. c 의 내용 을 입력 하면 다음 과 같 습 니 다.
#include
int main(){
printf("{ hello world }"); // }
)
[샘플 출력 1]
without maching ')' at line 4
[샘플 입력 2]
현재 디 렉 터 리 에 파일 example. c 의 내용 을 입력 하면 다음 과 같 습 니 다.
#include
int main(){
printf("{ hello world }d"); /* }*/
[샘플 출력 2]
without maching '{' at line 2
[샘플 입력 3]
현재 디 렉 터 리 에 파일 example. c 의 내용 을 입력 하면 다음 과 같 습 니 다.
#include
int main(){
printf("{ hello world }d"); /* }*/
}
[샘플 출력 3]
(){()}
[예시 설명]
샘플 1: 주석 부분 과 문자열 의 괄호 는 고려 하지 않 습 니 다. 프로그램 을 처리 한 후에 얻 은 괄호 서열 은 () {()) 입 니 다. 오른쪽 괄호 를 만 났 을 때 가장 가 까 운 왼쪽 괄호 와 일치 하지 않 습 니 다. 마지막 작은 괄호 와 큰 괄호 가 일치 하지 않 는 것 을 발 견 했 습 니 다.
샘플 2: 처리 후의 괄호 순 서 는 () {() 이 고 마지막 에 오른쪽 괄호 가 없 으 면 이에 대응 하 는 왼쪽 괄호 와 일치 하지 않 습 니 다.
#include
#include
int main()
{
FILE *in;
in = fopen("example.c", "r");
int line = 1;
int error = 0;
char stack_bracket[200], bracket[200] , linenum[1000]; //stack ,bracket 。 bracket
int p_to_stack = -1 , p_to_bracket = -1;
char temp;
int loop = 1;
while(loop)
{
temp = fgetc(in);
if(temp==EOF) break;
else if(temp=='
') line++;
else if(temp=='"') // , ,
{
while(1)
{
temp = fgetc(in);
if(temp=='\\') temp = fgetc(in);
else if(temp=='"') break;
}
}
else if(temp=='\'') // , , , \'
{
while(1)
{
temp = fgetc(in);
if(temp=='\\') temp = fgetc(in);
else if(temp=='\'') break;
}
}
else if(temp=='/') // , ,
{
temp = fgetc(in);
if(temp=='/')
{
while( (temp = fgetc(in)) != '
' )
;
line++;
}
else if(temp=='*')
{
int zhushi = 0;
temp = fgetc(in);
while(1)
{
if(zhushi) break;
while(temp != '*')
{
temp = fgetc(in);
if(temp == '
') line++;
}
temp = fgetc(in);
if(temp=='
') line++;
if(temp=='/') zhushi = 1;
}
}
}
else if(temp=='(') // , ,p_to_stack , linenum
{
stack_bracket[++p_to_stack] = temp;
bracket[++p_to_bracket] = temp;
linenum[p_to_stack] = line;
}
else if(temp=='{')
{
stack_bracket[++p_to_stack] = temp;
bracket[++p_to_bracket] = temp;
linenum[p_to_stack] = line;
}
else if(temp==')')
{
bracket[++p_to_bracket] = temp;
linenum[p_to_stack] = line;
if(p_to_stack == -1) // ,
{
printf("without maching '%c' at line %d
", temp, line);
error = 1; // , error 0 bracket
}
else
{
if( stack_bracket[p_to_stack]=='(' )
{
p_to_stack--;
}
else // , ,
{
printf("without maching '%c' at line %d
", temp, line);
error = 1;
}
}
}
else if(temp=='}')
{
bracket[++p_to_bracket] = temp;
linenum[p_to_stack] = line;
if(p_to_stack == -1)
{
printf("without maching '%c' at line %d
", temp, line);
error = 1;
}
else
{
if( stack_bracket[p_to_stack]=='{' )
{
p_to_stack--;
}
else
{
printf("without maching '%c' at line %d
", temp, line);
error = 1;
}
}
}
else
continue;
}
if(p_to_stack==0) // , ,
{
printf("without maching '%c' at line %d
",stack_bracket[p_to_stack] , linenum[p_to_stack]);
error = 1;
}
bracket[++p_to_bracket] = '\0';
if(error == 0) printf("%s", bracket);
fclose(in);
return 0;
}