aboutsummaryrefslogtreecommitdiff
path: root/C
diff options
context:
space:
mode:
Diffstat (limited to 'C')
-rw-r--r--C/MatrixCalculator.c78
1 files changed, 61 insertions, 17 deletions
diff --git a/C/MatrixCalculator.c b/C/MatrixCalculator.c
index ecd9370..14c3754 100644
--- a/C/MatrixCalculator.c
+++ b/C/MatrixCalculator.c
@@ -17,11 +17,15 @@
#define CMND_DIV_ROW "div"
#define CMND_SUM_ROW "sum"
+#define M_SUC 0
+#define M_ERR_FEW_ARGS 1
+#define M_ERR_BAD_ROW_INDEX 2
+
/* Matrix commands */
-void sumRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]);
-void multRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]);
-void divRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]);
-void swapRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]);
+int sumRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]);
+int multRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]);
+int divRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]);
+int swapRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]);
/* Arrays */
void assignArrValues(int cols, double arrDest[], double arrSrc[]);
@@ -38,14 +42,23 @@ int main() {
* Get matricies
*/
+ printf("-- Matrix parameters --\n");
+
+ char inputBuffer[MAX_LINE_WIDTH];
int rows, cols, hasVars;
- scanf("%d %d %d\n", &rows, &cols, &hasVars);
+
+ getInput(inputBuffer);
+ rows = atoi(strtok(inputBuffer, MATRIX_NUMBER_DELIMITER));
+ cols = atoi(strtok(NULL, MATRIX_NUMBER_DELIMITER));
+ hasVars = atoi(strtok(NULL, MATRIX_NUMBER_DELIMITER));
+
+ /* Assign input to matrix */
+
+ printf("-- Matrix values --\n");
double matrix[rows][cols];
double vars[rows][cols];
- char inputBuffer[MAX_LINE_WIDTH];
-
inputToMatrix(inputBuffer, rows, cols, matrix);
if (hasVars)
@@ -55,24 +68,24 @@ int main() {
* Modify matricies
*/
- printf("Commands:\n");
+ printf("-- Commands: --\n");
- char *command;
+ char *command; int result = 0;
do {
getInput(inputBuffer);
command = strtok(inputBuffer, MATRIX_NUMBER_DELIMITER);
if (!strcmp(inputBuffer, CMND_SWAP_ROWS))
- swapRows(command, rows, cols, matrix, hasVars, vars);
+ result = swapRows(command, rows, cols, matrix, hasVars, vars);
else if (!strcmp(inputBuffer, CMND_MULT_ROW))
- multRows(command, rows, cols, matrix, hasVars, vars);
+ result = multRows(command, rows, cols, matrix, hasVars, vars);
else if (!strcmp(inputBuffer, CMND_DIV_ROW))
- divRows(command, rows, cols, matrix, hasVars, vars);
+ result = divRows(command, rows, cols, matrix, hasVars, vars);
else if (!strcmp(inputBuffer, CMND_SUM_ROW))
- sumRows(command, rows, cols, matrix, hasVars, vars);
+ result = sumRows(command, rows, cols, matrix, hasVars, vars);
else if (!strcmp(inputBuffer, CMND_PRINT_MATRIX))
printMatrix(rows, cols, matrix);
@@ -80,6 +93,14 @@ int main() {
else if (!strcmp(inputBuffer, CMND_PRINT_VARS))
printMatrix(rows, cols, vars);
+ else if (strcmp(inputBuffer, CMND_END)) printf("# Command %s doesn't exist!\n", command);
+
+ if (result == 1)
+ printf("# Too few arguments for command %s!\n", command);
+ else if (result == 2)
+ printf("# Bad row index!\n");
+ result = 0;
+
} while (strcmp(inputBuffer, CMND_END) != 0);
/*
@@ -95,16 +116,21 @@ int main() {
* Matrix commands
*/
-void sumRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int sumVars, double vars[][matrixCols]) {
+int sumRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int sumVars, double vars[][matrixCols]) {
/* Get input */
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
int rowSrc = atoi(command) - 1;
+ if (rowSrc < 0 || rowSrc >= matrixRows) return M_ERR_BAD_ROW_INDEX;
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
int rowDst = atoi(command) - 1;
+ if (rowDst < 0 || rowDst >= matrixRows) return M_ERR_BAD_ROW_INDEX;
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
double mult = atof(command);
double tmp[matrixCols];
@@ -126,15 +152,20 @@ void sumRows(char *command, int matrixRows, int matrixCols, double matrix[][matr
for (int i = 0; i < matrixCols; i++)
vars[rowDst][i] += tmp[i];
}
+
+ return M_SUC;
}
-void multRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int multVars, double vars[][matrixCols]) {
+int multRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int multVars, double vars[][matrixCols]) {
/* Get input */
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
int row = atoi(command) - 1;
+ if (row < 0 || row >= matrixRows) return M_ERR_BAD_ROW_INDEX;
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
double amount = atof(command);
/* Multiply matrix row */
@@ -145,15 +176,20 @@ void multRows(char *command, int matrixRows, int matrixCols, double matrix[][mat
if (multVars)
multArrValues(matrixCols, vars[row], amount);
+
+ return M_SUC;
}
-void divRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int divVars, double vars[][matrixCols]) {
+int divRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int divVars, double vars[][matrixCols]) {
/* Get input */
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
int row = atoi(command) - 1;
+ if (row < 0 || row >= matrixRows) return M_ERR_BAD_ROW_INDEX;
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
double amount = atof(command);
/* Divide matrix row */
@@ -164,16 +200,22 @@ void divRows(char *command, int matrixRows, int matrixCols, double matrix[][matr
if (divVars)
multArrValues(matrixCols, vars[row], 1.0/amount);
+
+ return M_SUC;
}
-void swapRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]) {
+int swapRows(char *command, int matrixRows, int matrixCols, double matrix[][matrixCols], int swapVars, double vars[][matrixCols]) {
/* Get input */
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
int row1 = atoi(command) - 1;
+ if (row1 < 0 || row1 >= matrixRows) return M_ERR_BAD_ROW_INDEX;
command = strtok(NULL, MATRIX_NUMBER_DELIMITER);
+ if (command == NULL) return M_ERR_FEW_ARGS;
int row2 = atoi(command) - 1;
+ if (row2 < 0 || row2 >= matrixRows) return M_ERR_BAD_ROW_INDEX;
double tmp[matrixCols];
@@ -190,6 +232,8 @@ void swapRows(char *command, int matrixRows, int matrixCols, double matrix[][mat
assignArrValues(matrixCols, vars[row1], vars[row2]);
assignArrValues(matrixCols, vars[row2], tmp);
}
+
+ return M_SUC;
}
/*