diff --git a/main.c b/main.c index ee56e77..ab9b6ba 100644 --- a/main.c +++ b/main.c @@ -2,12 +2,13 @@ #include #include #include +#include #include #define _STR(X) #X #define STR(X) _STR(X) -#define MAX_FILE_PATH_LENGTH 256 +#define MAX_FILE_PATH_LENGTH FILENAME_MAX #define MAX_MATRIX_SIZE 500 #define MAX_MATRIX_IN_FILE_SIZE (MAX_MATRIX_SIZE + 2) @@ -55,7 +56,7 @@ int main(int argc, char* argv[]) { return 0; } if(result != 1) { - puts("Couldn't read file path - stopping"); + fputs("Couldn't read file path - stopping\n", stderr); return 1; } flushStdin(); @@ -69,25 +70,62 @@ int main(int argc, char* argv[]) { while(true) { if(load(filePath, matrix, b, x)) { - puts("Data successfully loaded\nMatrix A:"); - printMatrix(matrix); - puts("Vector b:"); - printVector(b); - puts("Vector x:"); - printVector(x); - Vector* result = solve(JACOBI, matrix, b, x, 0.00001); + // Debug outputs + //puts("Data successfully loaded\nMatrix A:"); + //printMatrix(matrix); + //puts("Vector b:"); + //printVector(b); + //puts("Vector x:"); + //printVector(x); + + puts("Please enter the algorithm to use:\n\t0: Jacobi (default)\n\t1: Gauss-Seidel"); + int algorithm; + + while(true) { + algorithm = getchar(); + if(algorithm == EOF) { + goto end; + } + if(algorithm == '0' || algorithm == '1') { + algorithm -= '0'; + break; + } + if(algorithm == '\n' || algorithm == 0) { + puts("Defaulting to Jacobi"); + algorithm = 0; + break; + } + fputs("Please enter 0, 1 or leave empty to exit!\n", stderr); + } + + puts("Please enter the precision to use:"); + double e; + int scan; + while(true) { + scan = scanf("%lf", &e); + if(scan == EOF) { + goto end; + } + if(scan == 1) { + break; + } + flushStdin(); + fputs("Invalid input - please enter a valid floating point number!\n", stderr); + } + + Vector* result = solve(algorithm, matrix, b, x, e); free(result); break; } else { - puts("Failed to load data from file.\nEnter new file path or press ctrl-c to exit"); + fputs("Failed to load data from file.\nEnter new file path or leave empty to exit.\n", stderr); int result = scanf("%" STR(MAX_FILE_PATH_LENGTH) "[^\n]", filePath); if(result == EOF || result == 0 || filePath[0] == 0) { goto end; } if(result != 1) { - puts("Couldn't read file path - stopping"); + fputs("Couldn't read file path - stopping\n", stderr); returnCode = 1; goto end; } @@ -107,7 +145,7 @@ end: bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) { FILE* file = fopen(filename, "r"); if(file == NULL) { - printf("Failed to open file \"%s\"\n", filename); + fprintf(stderr, "Failed to open file \"%s\"\n", filename); return false; } else { double* firstLineBuffer = malloc(sizeof(double) * (MAX_MATRIX_IN_FILE_SIZE)); @@ -115,12 +153,12 @@ bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) { int colsInFile = readMatrixLine(file, firstLineBuffer, MAX_MATRIX_IN_FILE_SIZE); if(colsInFile == -1) { - puts("Unexpected input on line 0"); + fputs("Unexpected input on line 0", stderr); free(firstLineBuffer); goto failure; } else if(colsInFile == -2) { - printf("Exceeded maximum matrix size of %i\n", MAX_MATRIX_SIZE); + fprintf(stderr, "Exceeded maximum matrix size of %i\n", MAX_MATRIX_SIZE); free(firstLineBuffer); goto failure; @@ -140,7 +178,7 @@ bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) { colsInLine = readMatrixLine(file, matrix->data[i], colsInFile); if(colsInLine < 0) { if(i == cols - 1) { - printf("Optional parameters are being used\n"); + puts("Optional parameters are being used"); // Matrix is one smaller than assumed cols--; @@ -160,11 +198,11 @@ bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) { } goto success; } else { - printf("Line %i contains illegal formatting - please fix!\n", i + 1); + fprintf(stderr, "Line %i contains illegal formatting - please fix!\n", i + 1); goto failure; } } else if(colsInLine != colsInFile) { - printf("Illegal line length found in line %i\n", i + 1); + fprintf(stderr, "Illegal line length found in line %i\n", i + 1); goto failure; } else { b->data[i] = matrix->data[i][cols];