commit 0a7e45daf88589593eef58cb4ec2bb9f01dff073
parent 1a7d9a1e43b5b9c4d27eb036b120bc463d816a4e
Author: klewer-martin <martin.cachari@gmail.com>
Date: Wed, 21 Apr 2021 13:32:25 -0300
Update: now the first option of the main prompt (read dimension and load values) works fine
Diffstat:
6 files changed, 70 insertions(+), 42 deletions(-)
diff --git a/main.c b/main.c
@@ -6,41 +6,19 @@
int main (void)
{
-/* matrix_t matrix;
- matrix_t matrix2;
- matrix_t matrix_r;
+ /*
+ matrix_t matrix;
+ m_load_dim(&matrix);
- m_create(R, R, &matrix);
- m_create(R, R, &matrix2);
- m_create(R, R, &matrix_r);
+ m_create(matrix.rows, matrix.columns, &matrix);
- m_load(N, M, matrix);
- m_print(N, M, matrix);
-
- putchar('\n');
-
- m_transpose(&matrix, &matrix_transpose);
- m_print(&matrix_transpose);
-
-
- m_load(&matrix);
+ m_initrand(&matrix);
m_print(&matrix);
putchar('\n');
- m_load(&matrix2);
- m_print(&matrix2);
- putchar('\n');
-
- m_multiply(&matrix, &matrix2, &matrix_r);
- m_print(&matrix_r);
- putchar('\n');
-
m_destroy(&matrix);
- m_destroy(&matrix2);
- m_destroy(&matrix_r);
-
-
*/
prompt_welcome();
+
return 0;
}
diff --git a/matrix.c b/matrix.c
@@ -48,6 +48,30 @@ status_t m_load(matrix_t *matrix)
return OK;
}
+status_t m_load_dim(matrix_t *matrix)
+{
+ if(matrix == NULL) return ERROR_NULL_POINTER;
+
+ char *buffer;
+ buffer = (char *)malloc(DIM_BUFFER_MAX_SIZE * sizeof(char));
+ if(buffer == NULL) return ERROR_ALLOCATING_MEMORY;
+
+ empty_string(buffer, DIM_BUFFER_MAX_SIZE);
+
+ printf("Enter no. of rows: \n");
+ fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
+ matrix->rows = strtol(buffer, NULL, 10);
+
+ empty_string(buffer, DIM_BUFFER_MAX_SIZE);
+
+ printf("Enter no. of columns: \n");
+ fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
+ matrix->columns = strtol(buffer, NULL, 10);
+
+ free(buffer);
+ return OK;
+}
+
status_t m_transpose(matrix_t *matrix, matrix_t *matrix_transpose)
{
if((matrix == NULL) || (matrix_transpose == NULL))
diff --git a/matrix.h b/matrix.h
@@ -10,6 +10,7 @@
#include "status.h"
#define MAX_IN_LEN 6
+#define DIM_BUFFER_MAX_SIZE 6
typedef struct {
size_t rows, columns;
@@ -19,6 +20,7 @@ typedef struct {
status_t m_print(matrix_t *matrix);
status_t m_initrand(matrix_t *matrix);
status_t m_load(matrix_t *matrix);
+status_t m_load_dim(matrix_t *matrix);
status_t m_transpose(matrix_t *matrix, matrix_t *matrix_transpose);
status_t m_add(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result);
status_t m_substract(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result);
diff --git a/prompt.c b/prompt.c
@@ -2,16 +2,13 @@
status_t prompt_welcome(void)
{
-/* Main loop */
- while( 1 ) {
- printf("Welcome to matrix-calculator!\n");
- printf("What do you want to do?\n");
- printf("1.- Load a matrix's values of dimensions X x Y by hand.\n");
- printf("2.- Load a matrix's values with a .txt file.\n");
- printf("3.- Create a random matrix of dimensions X x Y.\n");
-
- user_input(MAIN_PROMPT);
- }
+ printf("Welcome to matrix-calculator!\n");
+ printf("What do you want to do?\n");
+ printf("1.- Load a matrix's values of dimensions X x Y by hand.\n");
+ printf("2.- Load a matrix's values with a .txt file.\n");
+ printf("3.- Create a random matrix of dimensions X x Y.\n");
+
+ main_prompt();
return OK;
}
@@ -29,10 +26,29 @@ status_t user_input(user_input_t option)
status_t main_prompt(void)
{
- int i;
- i = getchar();
-
- i = (i - '0');
+ char *buffer;
+ size_t i;
+
+ buffer = (char *)malloc(DIM_BUFFER_MAX_SIZE * sizeof(char));
+ if(buffer == NULL) return ERROR_ALLOCATING_MEMORY;
+
+ empty_string(buffer, DIM_BUFFER_MAX_SIZE);
+ fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
+ i = strtol(buffer, NULL, 10);
+
+ switch(i) {
+ case 1:
+ {
+ matrix_t matrix;
+ m_load_dim(&matrix);
+
+ m_create(matrix.rows, matrix.columns, &matrix);
+
+ m_load(&matrix);
+ m_print(&matrix);
+ m_destroy(&matrix);
+ }
+ }
return OK;
}
diff --git a/prompt.h b/prompt.h
@@ -4,6 +4,11 @@
#include "matrix.h"
#include "status.h"
+#include <unistd.h>
+
+#define MAX_PROMPT_CMD 3
+#define SIZE_OF_BUFFER 10
+
typedef enum {
MAIN_PROMPT
} user_input_t;
@@ -11,5 +16,6 @@ typedef enum {
status_t prompt_welcome(void);
status_t user_input(user_input_t option);
status_t main_prompt(void);
+status_t load_m_hand(matrix_t *matrix);
#endif
diff --git a/status.h b/status.h
@@ -5,6 +5,8 @@ typedef enum {
OK,
ERROR_MATRIX_DIMENSION,
ERROR_NO_USER_INPUT,
+ ERROR_MAX_CMD_PROMPT,
+ ERROR_ALLOCATING_MEMORY,
ERROR_NULL_POINTER
} status_t;