Hi People, good morning. The program has to output curly brackets, square brackets and/or parentheses in case of absences. E.g.:
I only know how to deal with the first case. I wish you could explain me how I can deal with the other three.Code:1) [thames --> [thames] 2) thames] --> [thames] 3) (thames[thames{thames --> (thames)[thames]{thames} 4) thames --> (thames) or [thames] or {thames} (what I choose)
Code:#include <stdio.h> #include <stdlib.h> #define MAXLINE 100 enum syntax_states { NORMAL_CHAR = 0, PARENTHESES_BEGIN, PARENTHESES_END, SQUARE_BRACKETS_BEGIN, SQUARE_BRACKETS_END, CURLY_BRACKETS_BEGIN, CURLY_BRACKETS_END }; int main(void) { int c; int i = 0; char s[MAXLINE]; enum syntax_states state = NORMAL_CHAR; while(EOF != (c = fgetc(stdin))) { if(c != '\n') { s[i++] = c; } switch(state) { case NORMAL_CHAR: if(c == '[') state = SQUARE_BRACKETS_BEGIN; else if(c == '{') state = CURLY_BRACKETS_BEGIN; else if(c == '(') state = PARENTHESES_BEGIN; break; case SQUARE_BRACKETS_BEGIN: if(c == '\n' && s[i-1] != ']') { s[i++] = ']'; state = NORMAL_CHAR; } break; case SQUARE_BRACKETS_END: // ?? case CURLY_BRACKETS_BEGIN: if(c == '\n' && s[i-1] != '}') { s[i++] = '}'; state = NORMAL_CHAR; } break; case PARENTHESES_BEGIN: if(c == '\n' && s[i-1] != ')') { s[i++] = ')'; state = NORMAL_CHAR; } break; } } s[i] = '\0'; fputs(s, stdout); return 0; }



8Likes
LinkBack URL
About LinkBacks



