88 lines
1.7 KiB
C
88 lines
1.7 KiB
C
// Laboratorio 3 - Esercizio 2
|
|
// Matteo Schiff - s295565
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#define MAX_LEN 255
|
|
|
|
typedef struct Lista
|
|
{
|
|
unsigned int N;
|
|
char *canzoni;
|
|
} lista;
|
|
|
|
lista *leggi_brani(int *N, int **playlist)
|
|
{
|
|
FILE *fp_read;
|
|
|
|
if ((fp_read = fopen("brani.txt", "r")) == NULL)
|
|
{
|
|
puts("Impossibile aprire il file");
|
|
return NULL;
|
|
}
|
|
|
|
fscanf(fp_read, "%u \n", N);
|
|
|
|
lista *raccolta = malloc(*N * sizeof(lista));
|
|
*playlist = malloc(*N * sizeof(int));
|
|
|
|
for (int i = 0; i < *N; i++)
|
|
{
|
|
fscanf(fp_read, "%u \n", &(raccolta[i].N));
|
|
raccolta[i].canzoni = malloc(raccolta[i].N * MAX_LEN * sizeof(char));
|
|
for (int j = 0; j < raccolta[i].N; j++)
|
|
{
|
|
fscanf(fp_read, "%s ", raccolta[i].canzoni + j * MAX_LEN);
|
|
}
|
|
}
|
|
|
|
fclose(fp_read);
|
|
return raccolta;
|
|
}
|
|
|
|
void free_brani(lista *mat, int N, int *playlist)
|
|
{
|
|
for (int i = 0; i < N; i++)
|
|
{
|
|
free(mat[i].canzoni);
|
|
}
|
|
|
|
free(mat);
|
|
free(playlist);
|
|
}
|
|
|
|
void stampa_playlist(lista *mat, int *playlist, int N, int depth)
|
|
{
|
|
if (N == depth)
|
|
{
|
|
puts("\nPlaylist:");
|
|
for (int i = 0; i < N; i++)
|
|
{
|
|
printf(" - %s\n", (mat[i].canzoni + playlist[i] * MAX_LEN));
|
|
}
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < mat[depth].N; i++)
|
|
{
|
|
playlist[depth] = i;
|
|
stampa_playlist(mat, playlist, N, depth + 1);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int N;
|
|
int *playlist;
|
|
lista *mat = leggi_brani(&N, &playlist);
|
|
|
|
if (mat == NULL)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
stampa_playlist(mat, playlist, N, 0);
|
|
free_brani(mat, N, playlist);
|
|
return 0;
|
|
}
|