Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Controller.c
Original file line number Diff line number Diff line change
@@ -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;
}
150 changes: 150 additions & 0 deletions DataShell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//
// DataShell.h
//
//
// Created by Samantha Morris and Mauro Bernal on 19/03/2021.
//

#ifndef DataShell_h
#define DataShell_h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

/* 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 */
Binary file added DataShell.pdf
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions HELLO
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello TDA
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 ...}
#<tab> 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"
106 changes: 106 additions & 0 deletions Model.c
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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".
22 changes: 22 additions & 0 deletions View.c
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 0 additions & 1 deletion Examen1/data.csv → data.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
,0
0,5.077850806
1,5.11361086
2,5.013413304
Expand Down
Binary file added eje
Binary file not shown.