From bfa5fb3ce8365795be3e980f3049fee6c33676cf Mon Sep 17 00:00:00 2001 From: Siphalor Date: Tue, 17 Mar 2020 20:39:21 +0100 Subject: [PATCH] Finish --- main.c | 160 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 67 deletions(-) diff --git a/main.c b/main.c index 6559bab..4bad51f 100644 --- a/main.c +++ b/main.c @@ -87,15 +87,16 @@ int main(int argc, char* argv[]) { 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; } + flushStdin(); + if(algorithm == '0' || algorithm == '1') { + algorithm -= '0'; + break; + } fputs("Please enter 0, 1 or leave empty to exit!\n", stderr); } @@ -107,30 +108,63 @@ int main(int argc, char* argv[]) { if(scan == EOF) { goto end; } + flushStdin(); 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 { - 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 == NULL) { + puts("Given equation system doesn't converge!\nEnter 0 to repeat the program (default) or 1 to exit"); + algorithm = getchar(); + + if(algorithm != '\n' && algorithm != 0) { + flushStdin(); + if(algorithm == '1') { + returnCode = 10; + goto end; + } + } + } else { + puts("Solution found!\nEnter 0 to just print the solution (default) or 1 to print the whole sequence of iteration steps!"); + algorithm = getchar(); + if(algorithm != '\n' && algorithm != 0) + flushStdin(); + if(algorithm == '1') { + int i = 0; + do { + printVector(&result[i]); + free(result[i].data); + } while(result[++i].n != 0 && i <= MAX_ITERATION_STEPS); + free(result); + } else { + int i = 0; + while(result[++i].n != 0 && i < MAX_ITERATION_STEPS) { + free(result[i - 1].data); + } + printVector(&result[i-1]); + free(result); + } + break; } - if(result != 1) { - fputs("Couldn't read file path - stopping\n", stderr); - returnCode = 1; - goto end; - } - flushStdin(); + } else { + fputs("Failed to load data from file.\n", stderr); } + + puts("Enter file path or leave empty 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) { + fputs("Couldn't read file path - stopping\n", stderr); + returnCode = 1; + goto end; + } + flushStdin(); } end: @@ -255,8 +289,6 @@ void flushStdin(void) { Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) { Vector* vectors = malloc(sizeof(Vector) * (MAX_ITERATION_STEPS + 1)); - - initVector(&vectors[0], b->n); memcpy(vectors[0].data, x->data, b->n * sizeof(double)); @@ -264,67 +296,61 @@ Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) { double temp; double delta = 0.0; - if (method == 0){ - - do { - delta= 0.0; - initVector(&vectors[vectorCount],b->n); - for(int i=0; in; i++){ - temp=0.0; - for(int j=0; jn; j++){ - if(j!=i){ - temp= temp+A->data[i][j]*vectors[vectorCount-1].data[j]; - } - } - vectors[vectorCount].data[i]=(b->data[i]-temp)/A->data[i][i]; - delta=fmax(fabs(vectors[vectorCount].data[i]-vectors[vectorCount-1].data[i]),delta); - } - printVector(&vectors[vectorCount]); - vectorCount++; - } while (delta > e && vectorCount < MAX_ITERATION_STEPS); - - } - - if (method ==1){ - - - do{ + if (method == JACOBI){ + do { + delta = 0.0; + initVector(&vectors[vectorCount], b->n); + for(int i = 0; i < b->n; i++){ + temp = 0.0; + for(int j = 0; j < b->n; j++){ + if(j != i){ + temp = temp + A->data[i][j] * vectors[vectorCount - 1].data[j]; + } + } + vectors[vectorCount].data[i] = (b->data[i] - temp) / A->data[i][i]; + delta = fmax(fabs(vectors[vectorCount].data[i] - vectors[vectorCount - 1].data[i]), delta); + } + vectorCount++; + if (vectorCount >= MAX_ITERATION_STEPS) + goto fail; + } while (delta > e); + } else { + do { delta = 0.0; - if (vectorCount==1){ + if (vectorCount == 1){ initVector(&vectors[vectorCount], b->n); memcpy(vectors[vectorCount].data, x->data, b->n * sizeof(double));} else { initVector(&vectors[vectorCount], b->n); - memcpy(vectors[vectorCount].data, vectors[vectorCount-1].data, b->n * sizeof(double));} + memcpy(vectors[vectorCount].data, vectors[vectorCount - 1].data, b->n * sizeof(double));} - for (int i = 0; in; i++){ - temp=0.0; - for (int j=0; jn; j++){ - if (j!=i){ - temp = temp + A->data[i][j]*vectors[vectorCount].data[j]; + for (int i = 0; i < b->n; i++){ + temp = 0.0; + for (int j = 0; j < b->n; j++){ + if (j != i){ + temp = temp + A->data[i][j] * vectors[vectorCount].data[j]; } } - vectors[vectorCount].data[i] = (b->data[i]-temp)/A->data[i][i]; - delta=fmax(fabs(vectors[vectorCount].data[i]-vectors[vectorCount-1].data[i]),delta); - + vectors[vectorCount].data[i] = (b->data[i] - temp) / A->data[i][i]; + delta = fmax(fabs(vectors[vectorCount].data[i] - vectors[vectorCount - 1].data[i]), delta); } - printVector(&vectors[vectorCount]); vectorCount++; - }while(delta > e && vectorCount < MAX_ITERATION_STEPS); + if (vectorCount >= MAX_ITERATION_STEPS) + goto fail; + } while(delta > e); } - // 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 - // 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. - vectors[vectorCount].n=0; - + vectors[vectorCount].n = 0; return vectors; + +fail: + for (int i = 0; i < vectorCount; i++) { + free(vectors[i].data); + } + + free(vectors); + return NULL; } inline Matrix* createMatrix(void) {