-
-
Save parzibyte/61c1ca255b209a3dfe5a380132873737 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Función que regresará un detalle de película en caso de encontrarla | |
struct detalleDePelicula * | |
buscar(char horaInicio[MAXIMA_LONGITUD_CADENA], char cine[MAXIMA_LONGITUD_CADENA], char sala[MAXIMA_LONGITUD_CADENA], | |
char dia[MAXIMA_LONGITUD_CADENA]) { | |
// Vamos a usar este temporal para leer toda la pila | |
struct detalleDePelicula *temporal = superior; | |
// Recorremos toda la pila | |
while (temporal != NULL) { | |
// Si encontramos que coincide lo buscado con lo que hay en la pila en este momento, entonces regresamos el detalle | |
if ( | |
strcmp(cine, temporal->cine) == 0 && | |
strcmp(sala, temporal->sala) == 0 && | |
strcmp(horaInicio, temporal->horaInicio) == 0 && | |
strcmp(dia, temporal->dia) == 0 | |
) { | |
return temporal; | |
} | |
// Y vamos avanzando | |
temporal = temporal->siguiente; | |
} | |
// En caso de terminar de recorrer la pila y que no haya nada, regresamos NULL | |
return NULL; | |
} | |
// Agrega una película por primera vez | |
void | |
agregarPorPrimeraVez(char nombre[MAXIMA_LONGITUD_CADENA], char horaInicio[MAXIMA_LONGITUD_CADENA], | |
char cine[MAXIMA_LONGITUD_CADENA], char sala[MAXIMA_LONGITUD_CADENA], | |
char dia[MAXIMA_LONGITUD_CADENA], char clasificacion) { | |
// Requerir RAM para alojar este detalle de película | |
struct detalleDePelicula *d = malloc(sizeof(struct detalleDePelicula)); | |
// Llenamos el struct. Copiamos el nombre, hora, etcétera | |
memcpy(d->nombre, nombre, MAXIMA_LONGITUD_CADENA); | |
memcpy(d->horaInicio, horaInicio, MAXIMA_LONGITUD_CADENA); | |
memcpy(d->cine, cine, MAXIMA_LONGITUD_CADENA); | |
memcpy(d->sala, sala, MAXIMA_LONGITUD_CADENA); | |
memcpy(d->dia, dia, MAXIMA_LONGITUD_CADENA); | |
d->clasificacion = clasificacion; | |
// Y como estamos agregando un elemento, ahora éste es el superior, y su siguiente es el que antes era el superior | |
d->siguiente = superior; | |
superior = d; | |
} | |
// Si ya existe, la actualiza. Si no, la agrega por primera vez | |
void agregarOActualizar(char nombre[MAXIMA_LONGITUD_CADENA], char horaInicio[MAXIMA_LONGITUD_CADENA], | |
char cine[MAXIMA_LONGITUD_CADENA], char sala[MAXIMA_LONGITUD_CADENA], | |
char dia[MAXIMA_LONGITUD_CADENA], char clasificacion) { | |
// Buscar película con estos datos | |
struct detalleDePelicula *existente = buscar(horaInicio, cine, sala, dia); | |
// Si no existe, lo agregamos por primera vez | |
if (existente == NULL) { | |
agregarPorPrimeraVez(nombre, horaInicio, cine, sala, dia, clasificacion); | |
printf("\nPelicula agregada\n"); | |
} else { | |
// Si no, solo actualizamos (y lo indicamos) | |
memcpy(existente->nombre, nombre, MAXIMA_LONGITUD_CADENA); | |
memcpy(existente->horaInicio, horaInicio, MAXIMA_LONGITUD_CADENA); | |
memcpy(existente->cine, cine, MAXIMA_LONGITUD_CADENA); | |
memcpy(existente->sala, sala, MAXIMA_LONGITUD_CADENA); | |
memcpy(existente->dia, dia, MAXIMA_LONGITUD_CADENA); | |
existente->clasificacion = clasificacion; | |
printf("\nPelicula ya existia, fue reemplazada\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment