mcalc

simple interactive program to perform matrix operations. (WIP)
Index Commits Files Refs README LICENSE
commit c9de449843789124b87d195fdc34e3cdccbb56b5
parent d990fb6f5c33f920e7fc8915e7e1b4fec3182bee
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Fri, 23 Apr 2021 01:21:27 -0300

Update: Interactive part;

Diffstat:
Mmain.c | 13+++++++++++--
Mmatrix.c | 9+++++----
Mmatrix.h | 1+
Mprompt.c | 54++++++++++++++++++++++++++++++++++++++++--------------
Mprompt.h | 4+++-
5 files changed, 60 insertions(+), 21 deletions(-)
diff --git a/main.c b/main.c
@@ -18,9 +18,18 @@ int main (void)
     */
     status_t st;
 
+    /*    Only prints the main header    */
     prompt_welcome();
-    if((st = main_prompt()) != OK)
-        return st;
 
+    matrix_t matrix;
+
+    /*    The main prompt ask for a matrix interactively and returns a pointer to it    with the values already initialized    */
+    while( 1 ) {
+        if((st = get_matrix(&matrix)) != OK) {
+            /*    Prompt what do you want to do with the matrix you entered?    */
+            printf("%s", "What do you want to do with the matrix you entered?: ");
+            return st;
+        }
+    }
     return 0;
 }
diff --git a/matrix.c b/matrix.c
@@ -37,19 +37,20 @@ status_t m_load(matrix_t *matrix)
             ( 00 01 .. 0N )\n\
             ( 10 01 .. 1N )\n\
             ( .. .. .. .. )\n\
-            ( M0 M1 .. MN )\n");
+            ( M0 M1 .. MN )\n\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);
+            printf("Enter value %ld%ld of the matrix: ", i, j);
             for(size_t k = 0; ((aux = getchar()) != '\n') && k < MAX_IN_LEN; k++)
                 if(isdigit(aux) || (aux == '.') || (aux == '-'))
                     buf[k] = aux;
             
             matrix->array[i][j] = strtod(buf, NULL);
         }
+        putchar('\n');
     }
     return OK;
 }
@@ -64,13 +65,13 @@ status_t m_load_dim(matrix_t *matrix)
 
     empty_string(buffer, DIM_BUFFER_MAX_SIZE);
 
-    printf("%s", "Enter no. of rows: \n");
+    printf("%s", "Enter no. of rows: ");
     fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
     matrix->rows = strtol(buffer, NULL, 10);
 
     empty_string(buffer, DIM_BUFFER_MAX_SIZE);
 
-    printf("%s", "Enter no. of columns: \n");
+    printf("%s", "Enter no. of columns: ");
     fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
     matrix->columns = strtol(buffer, NULL, 10);
 
diff --git a/matrix.h b/matrix.h
@@ -15,6 +15,7 @@
 typedef struct {
     size_t rows, columns;
     double **array;
+    size_t id;
 } matrix_t;
 
 status_t m_print(matrix_t *matrix);
diff --git a/prompt.c b/prompt.c
@@ -2,18 +2,16 @@
 
 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 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 N x M.\n");
+    printf("%s", "Welcome to matrix-calculator!\n\n");
+    /*
+    printf("Welcome to matrix-calculator!\n\n1.- Load a matrix's values of dimensions N x M by hand.\n2.- Load a matrix's values with a .txt file.\n3.- Create a random matrix of dimensions N x M.\n\nWhat do you want to do?: ");
+    */
 }
 
 status_t user_input(user_input_t option)
 {
     switch (option) {
         case MAIN_PROMPT:
-            main_prompt();
             break;
         
         default: return ERROR_NO_USER_INPUT;
@@ -21,8 +19,9 @@ status_t user_input(user_input_t option)
     return OK;
 }
 
-status_t main_prompt(void)
+status_t get_matrix(matrix_t *matrix)
 {
+    printf("1.- Load a matrix's values of dimensions N x M by hand.\n2.- Load a matrix's values with a .txt file.\n3.- Create a random matrix of dimensions N x M.\nq - exit\n\nWhat do you want to do?: ");
     char *buffer;
     size_t i;
 
@@ -32,24 +31,51 @@ status_t main_prompt(void)
     empty_string(buffer, DIM_BUFFER_MAX_SIZE);
 
     fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
+    if(buffer[0] == '\n') {
+        while(buffer[0] == '\n') {
+            printf("%s", "What do you want to do?: ");
+            fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
+        }
+    } else if (buffer[0] == 'q') {
+        exit(0);
+    }
+
+
     i = strtol(buffer, NULL, 10);
     if((i < 1) || (i > MAX_MAIN_PROMPT_CMD))
         return ERROR_MAX_MAIN_PROMPT_REACHED;
 
+    empty_string(buffer, DIM_BUFFER_MAX_SIZE);
+
     putchar('\n');
     switch(i) {
         case 1:
             {
-                matrix_t matrix;
-                m_load_dim(&matrix);
+                /*    Enter the matrix Number(1 .. 128, Default 1):    */
+                printf("%s", "Enter the matrix Number (1 .. 128, Default 1): ");
+
+                fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin);
+                if(buffer[0] == '\n') {
+                    i = 1;
+                } else {
+                    i = strtol(buffer, NULL, 10);
+                }
+
+                /*    Now i contains the ID of the created matrix    */
+                matrix->id = i;
+                printf("The id you entered is: %lu\n", matrix->id);
 
-                m_create(matrix.rows, matrix.columns, &matrix);
+                /*    Now we ask for the dimensions of the matrix    */
+                m_load_dim(matrix);
 
+                /*    And we create the matrix    */
+                m_create(matrix->rows, matrix->columns, matrix);
+
+                putchar('\n');
+                m_load(matrix);
+                printf("%s", "The matrix you entered is: \n");
+                m_print(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
@@ -14,8 +14,10 @@ typedef enum {
 } user_input_t;
 
 void prompt_welcome(void);
+
 status_t user_input(user_input_t option);
-status_t main_prompt(void);
+status_t get_matrix(matrix_t *matrix);
+status_t matrix_menu_prompt(void);
 status_t load_m_hand(matrix_t *matrix);
 
 #endif