mcalc

simple interactive program to perform matrix operations. (WIP)
Index Commits Files Refs README LICENSE
commit 2686c71d37bf331e84bdaf1a134f5e8d8642f01c
parent 0a7e45daf88589593eef58cb4ec2bb9f01dff073
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Wed, 21 Apr 2021 14:33:06 -0300

Update;

Diffstat:
Mmain.c | 6++++--
Mmatrix.c | 10++++++++--
Mprompt.c | 14++++++++------
Mprompt.h | 4++--
Mstatus.h | 1+
5 files changed, 23 insertions(+), 12 deletions(-)
diff --git a/main.c b/main.c
@@ -2,8 +2,6 @@
 #include "matrix.h"
 #include "prompt.h"
 
-#define R 3
-
 int main (void)
 {
     /*
@@ -18,7 +16,11 @@ int main (void)
 
     m_destroy(&matrix);
     */
+    status_t st;
+
     prompt_welcome();
+    if((st = main_prompt()) != OK)
+        return st;
 
     return 0;
 }
diff --git a/matrix.c b/matrix.c
@@ -33,11 +33,17 @@ status_t m_load(matrix_t *matrix)
 {
     if(matrix == NULL) return ERROR_NULL_POINTER;
 
+    printf("%s", "Matrix of dimension N x M:\n\
+            ( 00 01 .. 0N )\n\
+            ( 10 01 .. 1N )\n\
+            ( .. .. .. .. )\n\
+            ( M0 M1 .. MN )\n");
     char buf[MAX_IN_LEN];
     int aux;
     for(size_t i = 0; i < matrix->rows; i++) {
         for(size_t j = 0; j < matrix->columns; j++) {
             empty_string(buf, MAX_IN_LEN);
+            printf("Enter value %ld%ld of the matrix.\n", i, j);
             for(size_t k = 0; ((aux = getchar()) != '\n') && k < MAX_IN_LEN; k++)
                 if(isdigit(aux) || (aux == '.') || (aux == '-'))
                     buf[k] = aux;
@@ -58,13 +64,13 @@ status_t m_load_dim(matrix_t *matrix)
 
     empty_string(buffer, DIM_BUFFER_MAX_SIZE);
 
-    printf("Enter no. of rows: \n");
+    printf("%s", "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");
+    printf("%s", "Enter no. of columns: \n");
     fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
     matrix->columns = strtol(buffer, NULL, 10);
 
diff --git a/prompt.c b/prompt.c
@@ -1,15 +1,12 @@
 #include "prompt.h"
 
-status_t prompt_welcome(void)
+void prompt_welcome(void)
 {
     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("1.- Load a matrix's values of dimensions N x M 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;
+    printf("3.- Create a random matrix of dimensions N x M.\n");
 }
 
 status_t user_input(user_input_t option)
@@ -36,7 +33,10 @@ status_t main_prompt(void)
 
     fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
     i = strtol(buffer, NULL, 10);
+    if((i < 1) || (i > MAX_MAIN_PROMPT_CMD))
+        return ERROR_MAX_MAIN_PROMPT_REACHED;
 
+    putchar('\n');
     switch(i) {
         case 1:
             {
@@ -45,7 +45,9 @@ status_t main_prompt(void)
 
                 m_create(matrix.rows, matrix.columns, &matrix);
 
+                putchar('\n');
                 m_load(&matrix);
+                printf("%s", "The matrix you entered is:\n");
                 m_print(&matrix);
                 m_destroy(&matrix);
         }
diff --git a/prompt.h b/prompt.h
@@ -6,14 +6,14 @@
 
 #include <unistd.h>
 
-#define MAX_PROMPT_CMD 3
+#define MAX_MAIN_PROMPT_CMD 3
 #define SIZE_OF_BUFFER 10
 
 typedef enum {
     MAIN_PROMPT
 } user_input_t;
 
-status_t prompt_welcome(void);
+void prompt_welcome(void);
 status_t user_input(user_input_t option);
 status_t main_prompt(void);
 status_t load_m_hand(matrix_t *matrix);
diff --git a/status.h b/status.h
@@ -7,6 +7,7 @@ typedef enum {
     ERROR_NO_USER_INPUT,
     ERROR_MAX_CMD_PROMPT,
     ERROR_ALLOCATING_MEMORY,
+    ERROR_MAX_MAIN_PROMPT_REACHED,
     ERROR_NULL_POINTER
 } status_t;