diff --git a/main.c b/main.c index 951e68a..dbef9ba 100644 --- a/main.c +++ b/main.c @@ -4,8 +4,15 @@ #include #include +#define _STR(X) #X +#define STR(X) _STR(X) + +#define MAX_FILE_PATH_LENGTH 256 + #define MAX_MATRIX_SIZE 500 -#define MAX_MATRIX_IN_FILE_SIZE MAX_MATRIX_SIZE + 2 +#define MAX_MATRIX_IN_FILE_SIZE (MAX_MATRIX_SIZE + 2) + +#define MAX_ITERATION_STEPS 100 typedef struct { int n; @@ -29,24 +36,39 @@ typedef enum { JACOBI = 0, GAUSS_SEIDEL = 1 } Method; +void flushStdin(); + bool load(const char* filename, Matrix* A, Vector* b, Vector* x); Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e); int readMatrixLine(FILE* file, double* matrixLine, int maxCols); int main(int argc, char* argv[]) { + char filePath[MAX_FILE_PATH_LENGTH]; + + if(argc >= 2) { + strncpy(filePath, argv[1], MAX_FILE_PATH_LENGTH); + } else { + puts("Please enter the path of the file you'd like to open"); + int result = scanf("%" STR(MAX_FILE_PATH_LENGTH) "[^\n]", filePath); + if(result == EOF || result == 0 || filePath[0] == 0) { + return 0; + } + if(result != 1) { + puts("Couldn't read file path - stopping"); + return 1; + } + flushStdin(); + } + Matrix* matrix = createMatrix(); Vector* b = createVector(); Vector* x = createVector(); - puts("Allocated ram"); int returnCode = 0; - if(argc != 2) { - puts("Program should be called with 1 argument specifying the file to load from\n"); - returnCode = 1; - } else { - if(load(argv[1], matrix, b, x)) { + while(true) { + if(load(filePath, matrix, b, x)) { puts("Data successfully loaded\nMatrix A:"); printMatrix(matrix); puts("Vector b:"); @@ -56,12 +78,24 @@ int main(int argc, char* argv[]) { Vector* result = solve(JACOBI, matrix, b, x, 0.00001); free(result); + break; } else { - puts("Failed to load data from file"); - returnCode = 2; + puts("Failed to load data from file.\nEnter new file path or press ctrl-c to exit"); + + 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"); + returnCode = 1; + goto end; + } + flushStdin(); } } +end: freeMatrix(matrix); freeVector(b); freeVector(x); @@ -70,7 +104,6 @@ int main(int argc, char* argv[]) { return returnCode; } - bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) { FILE* file = fopen(filename, "r"); if(file == NULL) { @@ -127,11 +160,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); + printf("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); + printf("Illegal line length found in line %i\n", i + 1); goto failure; } else { b->data[i] = matrix->data[i][cols]; @@ -151,7 +184,6 @@ success: failure: fclose(file); return false; - } @@ -177,11 +209,22 @@ int readMatrixLine(FILE* file, double* matrixLine, int maxCols) { return col; } +void flushStdin() { + int c; + while((c = getchar()) != '\n' && c != EOF && c != 0); +} + Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) { - Vector* vectors = malloc(sizeof(Vector)*100); + Vector* vectors = malloc(sizeof(Vector) * (MAX_ITERATION_STEPS + 1)); + + // MAX_ITERATION_STEPS enthält die maximal zulässige Anzahl an Iterationsschritten (100) + // Die einzelnen Vektoren müssen noch mit initVector initialisiert werden + + + // HIER kommt der Code hin ;) // on success - // Bitte setzt vectors[n+1].n = 0 + // Sei x die Anzahl der durchgeführten Iterationschritte. Dann setzt vectors[x+1].n = 0. Damit weiß das folgende Programm wie viele Schritte getätigt wurden. return vectors; // on failure