Programming/c언어
c언어 자료구조 - 괄호식검사
Kangsecu
2019. 3. 25. 12:26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | typedef char element; /* 스택의 객체 자료형 정의 */ // 프로그램 5.3활용 // 괄호 검사 함수 int check_matching(char *in) { StackType s; char ch, open_ch; int i, n = strlen(in); // n= 문자열의 길이 init(&s); // 스택의 초기화 for (i = 0; i < n; i++) { ch = in[i]; // ch = 다음 문자 switch (ch){ case '(': case '[': case '{'://여기 뭔가를 채워 넣야지 push(&s, ch); break; case ')': case ']': case '}': if (is_empty(&s)) return FALSE; else { open_ch = pop(&s); if (open_ch == '(' && ch != ')' || open_ch == '{' && ch != '}' || open_ch == '[' && ch != ']'){ return -1; } break; } } } if (!is_empty(&s)) return FALSE; // 스택에 남아있으면 오류 return TRUE; } //ㅏ int main() { if (check_matching("{ A[(i+1)]=0; }") == TRUE) printf("괄호검사성공\n"); else printf("괄호검사실패\n"); } | cs |