diff --git a/Controller.c b/Controller.c new file mode 100644 index 0000000..04efa63 --- /dev/null +++ b/Controller.c @@ -0,0 +1,33 @@ +#include "DataShell.h" + +int main (void) +{ + char* fileName=NULL; + int numLines = 0, numElements; + int option; + + //Calling function to get name + menuOne(&fileName); + + numLines = NumberLines(fileName); //Calling function to count how many lines + numElements = countElements(fileName); //Calling function to count elements in line + + float **Matrix = malloc(sizeof(float) * numLines * numElements); + + readDB(Matrix, fileName, numElements); + + do{ + option = menuTwo(); + if(option == 1) + { + printMatrix(Matrix, &numLines, &numElements); + printf("\nPress [enter] to go back to the menu.\n"); + __fpurge(stdin); + getchar(); + } + }while(option != 2); + + + free(Matrix); + return 0; +} \ No newline at end of file diff --git a/DataShell.h b/DataShell.h new file mode 100644 index 0000000..210ac33 --- /dev/null +++ b/DataShell.h @@ -0,0 +1,150 @@ +// +// DataShell.h +// +// +// Created by Samantha Morris and Mauro Bernal on 19/03/2021. +// + +#ifndef DataShell_h +#define DataShell_h + +#include +#include +#include +#include + +/* Set EXTERN macro: */ + +#ifndef DataShell_IMPORT + #define EXTERN +#else + #define EXTERN extern +#endif + + +/* Function prototypes. */ + + +typedef uint8_t BYTE; + +EXTERN int NumberLines(char *fileName); +/* + * + * The function NumberLines sees how many lines there are in a csv. + * + * @params + * char *fileName + * Name of file + * + * + * @returns + * int +*/ + +FILE* openMyfile (char *fileName); +/* + * + * The function openMyfile opens a file. + * + * @params + * char *fileName + * Name of file + * + * + * @returns + * FILE* +*/ + +void closeMyfile(FILE *file); +/* + * + * The function closeMyfile close a file. + * + * @params + * char *fileName + * Name of file + * + * + * @returns + * void +*/ + +void readDB (float **Matrix, char *fileName, int numElements); +/* + * + * The function readDB reads a Data Base. + * + * @params + * float **Matrix + * Matrix where the DB is gonna be store + * char *fileName + * Name of file + * int numElements + * Number of elements in a line + * + * + * @returns + * void +*/ + +int countElements(char *fileName); +/* + * + * The function countElements counts the number of elements in a line from a csv. + * + * @params + * char *fileName + * Name of file + * + * + * @returns + * int +*/ + +void printMatrix (float **Matrix, int *numLines, int *numElements); +/* + * + * The function printMatrix prints the matrix that contains all data from a csv file. + * + * @params +* float **Matrix + * Matrix where the DB is gonna be store + * int numLines + * Number of lines in a csv file. + * int numElements + * Number of elements in a line + * + * + * @returns + * void +*/ + +void menuOne(char **fileName); +/* + * + * The function menuOne asks user for the name of the csv file that wants to open. + * + * @params + * char *fileName + * Name of file + * + * + * @returns + * void +*/ + +int menuTwo(void); +/* + * + * The function menuTwo asks user if he wants to print data. + * + * @params + * void + * + * + * @returns + * int +*/ + + +#endif /* DataShell.h */ \ No newline at end of file diff --git a/DataShell.pdf b/DataShell.pdf new file mode 100644 index 0000000..a6114e6 Binary files /dev/null and b/DataShell.pdf differ diff --git "a/Examen1/Primer examen parcial Grupo B Oto\303\261o 2020.pdf" "b/Examen1/Primer examen parcial Grupo B Oto\303\261o 2020.pdf" deleted file mode 100644 index ae9e790..0000000 Binary files "a/Examen1/Primer examen parcial Grupo B Oto\303\261o 2020.pdf" and /dev/null differ diff --git a/HELLO b/HELLO new file mode 100644 index 0000000..01229c8 --- /dev/null +++ b/HELLO @@ -0,0 +1 @@ +Hello TDA diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..75475eb --- /dev/null +++ b/Makefile @@ -0,0 +1,36 @@ +#Makefile for Array (sirve ya para todos los programas) + +#Pones los targets que no van a generar files +.PHONY = all CLEAN + +#Variables: +CC = gcc +world := hola #variable estatica +CFLAGS = -C0 -s -lm +linkers = -lm -lpthread + +#Functions +deps = $(wildcard *.c) #DEPENDECIES aqui van los .c esa fun. trae todos los .c +depsh = $(wildcard *.h) +BINS = $(deps: %.c = %.o) #del contenido de deps, copia todo lo que es .c y pegalo, pero concatenale .o + + +all: ${BINS} eje +#TARGET : {DEPENDENCIES ...} +# RULE + +BINS: $(deps) #trae todo lo que tenga .c $^ del lado derecho @$ del lado izquerdo + ${CC} -c $^ ${CFLAGS} + @echo "Generating binary objects" + + +eje: ${BINS} + ${CC} -o $@ $^ ${linkers} + @echo "Generating excecutable" + +PRINT: + @echo $(deps) $(BINS) + +CLEAN: + rm -rvf *.o TEST_ARRAY + @echo "Removing all binary objects and excecutables" \ No newline at end of file diff --git a/Model.c b/Model.c new file mode 100644 index 0000000..88e83b2 --- /dev/null +++ b/Model.c @@ -0,0 +1,106 @@ +#include "DataShell.h" + +//Function that prints a Matrix +void printMatrix (float **Matrix, int *numLines, int *numElements) +{ + for(int i = 0; i < *numLines; i++) + { + for(int j = 0; j < *numElements; j ++) + { + printf("%.6f ", *((*(Matrix+i))+j)); + } + printf("\n"); + } +} + +//Function that reads DB and stores it in a Matrix +void readDB (float **Matrix, char *fileName, int numElements) +{ + char buff[255]; + char * value; + float *ptr = NULL; + int cont = 0, n =0; + + FILE *file = openMyfile(fileName); + + while(!feof(file)) + { + fscanf(file, "%s", buff); + + //numElements = countElements(buff); + float *ptr = malloc(sizeof(float) * numElements); + value = strtok(buff, ","); + + while(value != NULL) + { + ptr[cont] = atof(value); //Stores value into ptr + cont ++; + value = strtok(NULL, ","); + } + cont = 0; + Matrix[n] = ptr; //Matrix now points to ptr + n ++; + + } + closeMyfile(file); + + return; +} + +//Function that tells how many elements does a csv line has +int countElements(char *fileName) +{ + int numElements = 0; + char buff[255]; + + FILE *file = openMyfile(fileName); + fscanf(file, "%s", buff); + char* value = strtok(buff, ","); + + while(value != NULL) + { + numElements ++; + value = strtok(NULL, ","); + } + //printf("num = %d ", numElements); + closeMyfile(file); + return numElements; +} + +//Function that opens a file +FILE* openMyfile (char *fileName) +{ + FILE *file = fopen(fileName, "r"); + + if(file == NULL) + { + printf("Couln“t open file\n"); + exit(0); + } + return file; +} + +//Function that closes a file +void closeMyfile(FILE *file) +{ + fclose(file); +} + +//Function that return number of lines +int NumberLines(char *fileName) +{ + int numLines = 0; + char buff[255]; + + FILE *file = openMyfile(fileName); + + while(!feof(file)) + { + fscanf(file, "%s", buff); + numLines ++; + } + + closeMyfile(file); + + return numLines; +} diff --git a/README.md b/README.md index b2236c4..4382a9b 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ DataShell Repository +This program reads from a CSV and stores it data in a Double Pointer Arithmetic. +To compile it you type "make" on the terminal. +And to execute it you type "./eje". diff --git a/View.c b/View.c new file mode 100644 index 0000000..5a67dfe --- /dev/null +++ b/View.c @@ -0,0 +1,22 @@ +#include "DataShell.h" + +void menuOne(char **fileName) +{ + system("clear"); + printf("Hi! :)\n"); + printf("Name of File: "); + scanf("%m[^\n]", fileName); //Reads until it finds a \n + printf("Reading from: %s...\n", *fileName); +} + +int menuTwo(void) +{ + int option; + + system("clear"); + printf("\nWhat do you wanna do?:\n[1] Print data. \n[2] Exit.\nAnswer: "); + scanf("%d", &option); + printf("\n"); + system("clear"); + return(option); +} \ No newline at end of file diff --git a/Examen1/data.csv b/data.csv similarity index 93% rename from Examen1/data.csv rename to data.csv index a6eeb05..ba235b9 100644 --- a/Examen1/data.csv +++ b/data.csv @@ -1,4 +1,3 @@ -,0 0,5.077850806 1,5.11361086 2,5.013413304 diff --git a/eje b/eje new file mode 100644 index 0000000..d862daf Binary files /dev/null and b/eje differ