Improve error output and complete main()\nmore comments
This commit is contained in:
72
main.c
72
main.c
@@ -2,12 +2,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#define _STR(X) #X
|
#define _STR(X) #X
|
||||||
#define STR(X) _STR(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_SIZE 500
|
||||||
#define MAX_MATRIX_IN_FILE_SIZE (MAX_MATRIX_SIZE + 2)
|
#define MAX_MATRIX_IN_FILE_SIZE (MAX_MATRIX_SIZE + 2)
|
||||||
@@ -55,7 +56,7 @@ int main(int argc, char* argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(result != 1) {
|
if(result != 1) {
|
||||||
puts("Couldn't read file path - stopping");
|
fputs("Couldn't read file path - stopping\n", stderr);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
flushStdin();
|
flushStdin();
|
||||||
@@ -69,25 +70,62 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
if(load(filePath, matrix, b, x)) {
|
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);
|
free(result);
|
||||||
break;
|
break;
|
||||||
} else {
|
} 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);
|
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;
|
||||||
}
|
}
|
||||||
if(result != 1) {
|
if(result != 1) {
|
||||||
puts("Couldn't read file path - stopping");
|
fputs("Couldn't read file path - stopping\n", stderr);
|
||||||
returnCode = 1;
|
returnCode = 1;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -107,7 +145,7 @@ end:
|
|||||||
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) {
|
||||||
printf("Failed to open file \"%s\"\n", filename);
|
fprintf(stderr, "Failed to open file \"%s\"\n", filename);
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
double* firstLineBuffer = malloc(sizeof(double) * (MAX_MATRIX_IN_FILE_SIZE));
|
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);
|
int colsInFile = readMatrixLine(file, firstLineBuffer, MAX_MATRIX_IN_FILE_SIZE);
|
||||||
|
|
||||||
if(colsInFile == -1) {
|
if(colsInFile == -1) {
|
||||||
puts("Unexpected input on line 0");
|
fputs("Unexpected input on line 0", stderr);
|
||||||
|
|
||||||
free(firstLineBuffer);
|
free(firstLineBuffer);
|
||||||
goto failure;
|
goto failure;
|
||||||
} else if(colsInFile == -2) {
|
} 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);
|
free(firstLineBuffer);
|
||||||
goto failure;
|
goto failure;
|
||||||
@@ -140,7 +178,7 @@ bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) {
|
|||||||
colsInLine = readMatrixLine(file, matrix->data[i], colsInFile);
|
colsInLine = readMatrixLine(file, matrix->data[i], colsInFile);
|
||||||
if(colsInLine < 0) {
|
if(colsInLine < 0) {
|
||||||
if(i == cols - 1) {
|
if(i == cols - 1) {
|
||||||
printf("Optional parameters are being used\n");
|
puts("Optional parameters are being used");
|
||||||
|
|
||||||
// Matrix is one smaller than assumed
|
// Matrix is one smaller than assumed
|
||||||
cols--;
|
cols--;
|
||||||
@@ -160,11 +198,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 + 1);
|
fprintf(stderr, "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 + 1);
|
fprintf(stderr, "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];
|
||||||
|
|||||||
Reference in New Issue
Block a user