Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
funny stack overflow
#include <stdio.h> #include <stdlib.h> #include <stdint.h> struct array { char *data; size_t size; size_t i; }; static int array_resize(struct array *array) { if (array->size > SIZE_MAX / 2) { return 1; } if (array->size == 0) { array->size = 1; } else { array->size *= 2; } char *tmp = realloc(array->data, array->size); if (tmp == NULL) { return 1; } array->data = tmp; return 0; } static int array_push(struct array *array, char c) { if (array->i < array->size) { array->data[array->i++] = c; } else { if (array_resize(array) != 0) { return 1; } array_push(array, c); } return 0; } static int array_pop(struct array *array, char *result) { if (array->i > 0) { array->i--; if (result != NULL) { *result = array->data[array->i]; } return 0; } return 1; } static int eval(struct array *array, char *line, size_t read) { array->i = 0; for (size_t i = 0; i < read; i++) { switch (line[i]) { case '*': { if (array_pop(array, NULL) != 0) { return 1; } break; } case '(': case '[': { if (array_push(array, line[i]) != 0) { return 1; } break; } case ')': { char result; if (array_pop(array, &result) != 0) { return 3; } if (result != '(') { printf("wrong input: %zu\n", i); return 2; } break; } case ']': { char result; if (array_pop(array, &result) != 0) { return 3; } if (result != '[') { printf("wrong input: %zu\n", i); return 2; } break; } } } return 0; } int main(void) { char *line = NULL; size_t len = 0; ssize_t read; struct array array = {.data = NULL, .size = 0, .i = 0}; while ((read = getline(&line, &len, stdin)) > 0) { printf("%s", line); int ret = eval(&array, line, (size_t)read); if (ret == 1) { printf("intern problem\n"); break; } else if (ret == 2) { printf("wrong input\n"); } else if (ret == 3) { printf("wrong number input\n"); } else { printf("ok\n"); } } free(line); free(array.data); }
run
|
edit
|
history
|
help
0
Max number of a vector
min max pole
Pointer_Indirectare_N
Roots of a Quadratic Equation
vyměna proměnych pomoci parametru
Euclides 2
Hello World C
Fibonacci search by Henry Kroll III www.thenerdshow.com
Tablero de ajedrez
Cuadrado asteriscos hueco con regalito