Improve main function and add comments to solve()

This commit is contained in:
2020-02-11 13:35:20 +01:00
parent a933d4a548
commit f478f29e05

73
main.c
View File

@@ -4,8 +4,15 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#define _STR(X) #X
#define STR(X) _STR(X)
#define MAX_FILE_PATH_LENGTH 256
#define MAX_MATRIX_SIZE 500 #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 { typedef struct {
int n; int n;
@@ -29,24 +36,39 @@ typedef enum {
JACOBI = 0, GAUSS_SEIDEL = 1 JACOBI = 0, GAUSS_SEIDEL = 1
} Method; } Method;
void flushStdin();
bool load(const char* filename, Matrix* A, Vector* b, Vector* x); bool load(const char* filename, Matrix* A, Vector* b, Vector* x);
Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e); Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e);
int readMatrixLine(FILE* file, double* matrixLine, int maxCols); int readMatrixLine(FILE* file, double* matrixLine, int maxCols);
int main(int argc, char* argv[]) { 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(); Matrix* matrix = createMatrix();
Vector* b = createVector(); Vector* b = createVector();
Vector* x = createVector(); Vector* x = createVector();
puts("Allocated ram");
int returnCode = 0; int returnCode = 0;
if(argc != 2) { while(true) {
puts("Program should be called with 1 argument specifying the file to load from\n"); if(load(filePath, matrix, b, x)) {
returnCode = 1;
} else {
if(load(argv[1], matrix, b, x)) {
puts("Data successfully loaded\nMatrix A:"); puts("Data successfully loaded\nMatrix A:");
printMatrix(matrix); printMatrix(matrix);
puts("Vector b:"); puts("Vector b:");
@@ -56,12 +78,24 @@ int main(int argc, char* argv[]) {
Vector* result = solve(JACOBI, matrix, b, x, 0.00001); Vector* result = solve(JACOBI, matrix, b, x, 0.00001);
free(result); free(result);
break;
} else { } else {
puts("Failed to load data from file"); puts("Failed to load data from file.\nEnter new file path or press ctrl-c to exit");
returnCode = 2;
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); freeMatrix(matrix);
freeVector(b); freeVector(b);
freeVector(x); freeVector(x);
@@ -70,7 +104,6 @@ int main(int argc, char* argv[]) {
return returnCode; return returnCode;
} }
bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) { bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) {
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if(file == NULL) { if(file == NULL) {
@@ -127,11 +160,11 @@ bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) {
} }
goto success; goto success;
} else { } else {
printf("Line %i contains illegal formatting - please fix!\n", i); printf("Line %i contains illegal formatting - please fix!\n", i + 1);
goto failure; goto failure;
} }
} else if(colsInLine != colsInFile) { } 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; goto failure;
} else { } else {
b->data[i] = matrix->data[i][cols]; b->data[i] = matrix->data[i][cols];
@@ -151,7 +184,6 @@ success:
failure: failure:
fclose(file); fclose(file);
return false; return false;
} }
@@ -177,11 +209,22 @@ int readMatrixLine(FILE* file, double* matrixLine, int maxCols) {
return col; 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* 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 // 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; return vectors;
// on failure // on failure