commit 8cd43856c548e5c2d9497eb96a438934b21675cb
parent 9f9bfb85616e3f56411fa3b442c51db1cd8c2a21
Author: klewer-martin <martin.cachari@gmail.com>
Date: Sun, 18 Apr 2021 19:09:56 -0300
Update: improvements on func m_load()
Diffstat:
4 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,14 +1,14 @@
CC = gcc
FLAGS = -Wall -pedantic
-all: clean main.o matrix_struct.o
- $(CC) $(FLAGS) main.o matrix_struct.o
+all: clean main.o matrix.o
+ $(CC) $(FLAGS) main.o matrix.o
main.o:
$(CC) $(FLAGS) -c main.c
-matrix_struct.o:
- $(CC) $(FLAGS) -c matrix_struct.c
+matrix.o:
+ $(CC) $(FLAGS) -c matrix.c
clean:
rm -rf *.o
diff --git a/main.c b/main.c
@@ -24,7 +24,7 @@ int main (void)
*/
- m_initrand(&matrix);
+ m_load(&matrix);
m_print(&matrix);
putchar('\n');
diff --git a/matrix.c b/matrix.c
@@ -27,9 +27,14 @@ void m_initrand(matrix_t *matrix)
void m_load(matrix_t *matrix)
{
char buf[20];
+ int aux;
for(size_t i = 0; i < matrix->rows; i++) {
for(size_t j = 0; j < matrix->columns; j++) {
- fgets(buf, 20, stdin);
+ empty_string(buf, 20);
+ for(size_t k = 0; ((aux = getchar()) != '\n') && k < MAX_IN_LEN; k++)
+ if(isdigit(aux) || aux == '.')
+ buf[k] = aux;
+
matrix->array[i][j] = strtod(buf, NULL);
}
}
@@ -97,7 +102,7 @@ void m_divide(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
for(size_t i = 0; i < matrixA->rows; i++) {
for(size_t j = 0; j < matrixB->columns; j++) {
for(size_t k = aux = 0; k < matrixA->columns; k++)
- aux += ((matrixA->array[i][k])*(matrixB->array[k][j]));
+ aux += ((matrixA->array[i][k]) / (matrixB->array[k][j]));
result->array[i][j] = aux;
}
@@ -142,3 +147,10 @@ bool m_isSimetric(matrix_t *matrix)
return true * (aux == (matrix->rows * matrix->columns)) + false * (aux != (matrix->rows * matrix->columns));
}
+void empty_string(char *string, size_t len)
+{
+ if(string == NULL) return;
+
+ for(size_t i = 0; i < len; i++)
+ string[i] = '\0';
+}
diff --git a/matrix.h b/matrix.h
@@ -5,6 +5,9 @@
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
+#include <ctype.h>
+
+#define MAX_IN_LEN 6
typedef struct {
size_t rows, columns;
@@ -25,6 +28,8 @@ void m_create(size_t rows, size_t columns, matrix_t *matrix);
void m_destroy(matrix_t *matrix);
+void empty_string(char *string, size_t len);
+
bool m_isSimetric(matrix_t *matrix);
#endif