#include #include #include #define IMP #include "../../file.h" size_t cstr_char_count(const char *cstr, char c); int cmp_sort(const void *this, const void *other); int main(int argc, char *argv[]) { enum file_err ferr; long res = 0; char *raw_input = NULL; size_t raw_input_size = 0; raw_input = (char *) file_read_all("./input.txt", &raw_input_size, &ferr); if ( ferr != FILE_ERR_OK ) { fprintf(stderr, "Failed to open file: %s\n", file_err_to_cstr(ferr)); exit(EXIT_FAILURE); } if ( argc == 1 ) { char *cstr = raw_input; loop_p1: switch ( *(cstr++) ) { case '\0': goto exit_loop; case '(': ++res; break; case ')': --res; break; } goto loop_p1; } else { size_t i = 0; char *cstr = raw_input; loop_p2: switch ( *(cstr + i) ) { case '\0': goto exit_loop; case '(': ++res; break; case ')': --res; if ( res < 0 ) { res = ((long) i) + 1; goto exit_loop; } break; } ++i; goto loop_p2; } exit_loop: printf("Result: %ld\n", res); free(raw_input); (void) argc; (void) argv; return 0; } size_t cstr_char_count(const char *cstr, char c) { size_t ret = 0; while ( *cstr != '\0' ) { ret += ( *cstr == c ); cstr++; } return ret; } int cmp_sort(const void *this, const void *other) { long this_ = *((long*) this); long other_ = *((long*) other); return (int) (this_ - other_); }