This commit is contained in:
2020-03-17 20:39:21 +01:00
parent 20e98a1105
commit bfa5fb3ce8

94
main.c
View File

@@ -87,15 +87,16 @@ int main(int argc, char* argv[]) {
if(algorithm == EOF) { if(algorithm == EOF) {
goto end; goto end;
} }
if(algorithm == '0' || algorithm == '1') {
algorithm -= '0';
break;
}
if(algorithm == '\n' || algorithm == 0) { if(algorithm == '\n' || algorithm == 0) {
puts("Defaulting to Jacobi"); puts("Defaulting to Jacobi");
algorithm = 0; algorithm = 0;
break; break;
} }
flushStdin();
if(algorithm == '0' || algorithm == '1') {
algorithm -= '0';
break;
}
fputs("Please enter 0, 1 or leave empty to exit!\n", stderr); fputs("Please enter 0, 1 or leave empty to exit!\n", stderr);
} }
@@ -107,19 +108,53 @@ int main(int argc, char* argv[]) {
if(scan == EOF) { if(scan == EOF) {
goto end; goto end;
} }
flushStdin();
if(scan == 1) { if(scan == 1) {
break; break;
} }
flushStdin();
fputs("Invalid input - please enter a valid floating point number!\n", stderr); fputs("Invalid input - please enter a valid floating point number!\n", stderr);
} }
Vector* result = solve(algorithm, matrix, b, x, e); 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);
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;
}
} 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); int result = scanf("%" STR(MAX_FILE_PATH_LENGTH) "[^\n]", filePath);
if(result == EOF || result == 0 || filePath[0] == 0) { if(result == EOF || result == 0 || filePath[0] == 0) {
goto end; goto end;
@@ -131,7 +166,6 @@ int main(int argc, char* argv[]) {
} }
flushStdin(); flushStdin();
} }
}
end: end:
freeMatrix(matrix); freeMatrix(matrix);
@@ -255,8 +289,6 @@ void flushStdin(void) {
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) * (MAX_ITERATION_STEPS + 1)); Vector* vectors = malloc(sizeof(Vector) * (MAX_ITERATION_STEPS + 1));
initVector(&vectors[0], b->n); initVector(&vectors[0], b->n);
memcpy(vectors[0].data, x->data, b->n * sizeof(double)); memcpy(vectors[0].data, x->data, b->n * sizeof(double));
@@ -264,8 +296,7 @@ Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
double temp; double temp;
double delta = 0.0; double delta = 0.0;
if (method == 0){ if (method == JACOBI){
do { do {
delta = 0.0; delta = 0.0;
initVector(&vectors[vectorCount], b->n); initVector(&vectors[vectorCount], b->n);
@@ -279,15 +310,11 @@ Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
vectors[vectorCount].data[i] = (b->data[i] - temp) / A->data[i][i]; 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); delta = fmax(fabs(vectors[vectorCount].data[i] - vectors[vectorCount - 1].data[i]), delta);
} }
printVector(&vectors[vectorCount]);
vectorCount++; vectorCount++;
} while (delta > e && vectorCount < MAX_ITERATION_STEPS); if (vectorCount >= MAX_ITERATION_STEPS)
goto fail;
} } while (delta > e);
} else {
if (method ==1){
do { do {
delta = 0.0; delta = 0.0;
if (vectorCount == 1){ if (vectorCount == 1){
@@ -307,24 +334,23 @@ Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
} }
vectors[vectorCount].data[i] = (b->data[i] - temp) / A->data[i][i]; 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); delta = fmax(fabs(vectors[vectorCount].data[i] - vectors[vectorCount - 1].data[i]), delta);
} }
printVector(&vectors[vectorCount]);
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; return vectors;
fail:
for (int i = 0; i < vectorCount; i++) {
free(vectors[i].data);
}
free(vectors);
return NULL;
} }
inline Matrix* createMatrix(void) { inline Matrix* createMatrix(void) {