#include #include #include #if 0 #define FILENAME "sample.txt" #else #define FILENAME "input.txt" #endif int biggest[3]; int bigger(int val) { for(int i = 2; i >= 0; i--) if(val > biggest[i]) return i; return -1; } void shift(int n, int val) { for(int i = 1; i <= n; i++) biggest[i-1] = biggest[i]; biggest[n] = val; } void check(int val) { int b = bigger(val); if(b != -1) shift(b, val); } void parse() { FILE *fp = fopen(FILENAME, "r"); if(!fp) { fprintf(stderr, "ERROR: Could not open file %s\n", FILENAME); exit(1); } char line[256]; int sum = 0; while(fgets(line, sizeof(line), fp) != NULL) { if(line[0] == '\n') { check(sum); sum = 0; } sum += atoi(line); } check(sum); // the file does not end in a newline fclose(fp); } int main(void) { for(int i = 0; i < 3; i++) biggest[i] = INT_MIN; parse(); int sum = 0; for(int i = 0; i < 3; i++) { printf("%d\n", biggest[i]); sum += biggest[i]; } printf("\n%d\n", sum); return 0; }