PI-ITBA / 2024_01

7 stars 0 forks source link

MuseumADT_parcial #282

Closed FrancoBrandaa closed 4 months ago

FrancoBrandaa commented 5 months ago

Buenas ,me gustaria ver correciones sobre este codigo (pasa los asserts). No se si era preferible usar un vector estatico. link al parcial:https://docs.google.com/document/d/1geeQe_7hxMvd04GMjC3dG2keYbTuiPKM7M3F8KGhTAI/edit?pli=1

#include "museum.h"

struct node{
    char * name;
    struct node * tail;
}; //ordenada de manera alfabetica

struct ticket{
    int  amounttickets;
    struct node * first;
    struct node * next;
};

struct museumTicketCDT{
    int total;
    struct ticket * tickets;
   //int next;
};

museumTicketADT newMuseumTicket(void){
    museumTicketADT aux = malloc(sizeof(struct museumTicketCDT));
    aux ->total = 0;
    aux ->tickets = calloc(366,sizeof(struct ticket)); //hago apuntar a una zona de 366 structs inicializdas en 0 o nuLL sus campos
    return aux; //podria seer un vector estatico pero deberia inicializar todo en 0 recorriendo primero 
} 

static struct node * add(struct node * list, const char * visitor, int * flag){ //estara ordenado
    int c ;
    if(list == NULL || (c =strcasecmp(list->name, visitor)) > 0){
        struct node * aux = malloc(sizeof(struct node));
        aux->name = malloc(strlen(visitor)+1 * sizeof(char)); 
        strcpy(aux->name, visitor);
        aux->tail = list;
        *flag = 1;
        return aux;
    }
    if (c == 0){
        return list;
    }
    list->tail = add(list->tail,  visitor, flag);
    return list;
}

int addTicket(museumTicketADT museumTicketADT, size_t dayOfYear, const char * visitor){
    if( dayOfYear > 366 ||  dayOfYear == 0 ){
        return 0;
    }
    int flag = 0;
    int index = dayOfYear-1;
    // para anadir el nombre hago el tipico add;
    museumTicketADT->tickets[index].first = add(museumTicketADT->tickets[index].first,visitor, &flag);
    if(flag){
        museumTicketADT->total++;
        return ++museumTicketADT->tickets[index].amounttickets;
    }
    return 0;
}

int dayTickets(const museumTicketADT museumTicketADT, size_t dayOfYear){
    if( dayOfYear > 366 ||  dayOfYear == 0 ){
        return -1;
    }
    return museumTicketADT->tickets[dayOfYear - 1].amounttickets;
}

void toBeginByDay(museumTicketADT museumTicketADT, size_t dayOfYear){
    if( dayOfYear > 366 ||  dayOfYear == 0 ){
        exit(1);
    }
    int index = dayOfYear-1;
    museumTicketADT->tickets[index].next = museumTicketADT->tickets[index].first ;
}

size_t hasNextByDay(museumTicketADT museumTicketADT, size_t dayOfYear){
    if(museumTicketADT->tickets[dayOfYear-1].next == NULL){
        return 0;
    }
    return 1;
}

char * nextByDay(museumTicketADT museumTicketADT, size_t dayOfYear){ 
    if (!hasNextByDay(museumTicketADT,dayOfYear)){
        exit(1);
    }
    struct node * aux = museumTicketADT->tickets[dayOfYear-1].next;
    museumTicketADT->tickets[dayOfYear-1].next = museumTicketADT->tickets[dayOfYear-1].next->tail;
    return aux->name;
}

int yearTickets(const museumTicketADT museumTicketADT){
    return museumTicketADT->total;
}

static void freerec(struct node * list){
    if(list == NULL ){
        return;
    }
    freerec(list->tail);
    free(list->name);
    free(list);
}
void freeMuseumTicket(museumTicketADT museumTicketADT){
    for (int i = 0 ; i<366 ; i++){
        freerec(museumTicketADT->tickets[i].first);
    }
    free(museumTicketADT->tickets);
    free(museumTicketADT);
}
marcelogarberoglio commented 5 months ago

Sí, era más simple usar un vector estático, ya que la cantidad es constante. El vector no va a crecer ni decrecer. No está bien el uso de magic numbers Recuerden no hacer

 if(museumTicketADT->tickets[dayOfYear-1].next == NULL){
        return 0;
    }
    return 1;

sino

return museumTicketADT->tickets[dayOfYear-1].next != NULL){
FrancoBrandaa commented 5 months ago

muchas gracias marce!!