Advertisement
Panuozzo77

Untitled

Sep 21st, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. #include "hierarchy.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #define MAXL1 20
  7. #define MAXL2 50
  8.  
  9. struct hier
  10. {
  11.     char *name;
  12.     char **songs;
  13.     int num;
  14. };
  15.  
  16. int charChecker(char *p)
  17. {
  18.     if(p==NULL)
  19.     {
  20.         printf("errore di allocazione caratteri");
  21.         exit(9);
  22.     }
  23.     return 0;
  24. }
  25.  
  26. hierarchy newAuthor(char *nm)
  27. {
  28.     hierarchy new=malloc(sizeof(hierarchy));
  29.     new->name=malloc(strlen(nm)+1);
  30.     new->songs=malloc(sizeof(char*));
  31.     strcpy(new->name, nm);
  32.     new->num=0;
  33. }
  34.  
  35. void addSong(hierarchy p, char *nm)
  36. {
  37.     p->songs=realloc(p->songs, sizeof(char*)*(numEl(p)+1));
  38.     if(!p->songs) exit(9);
  39.     p->songs[numEl(p)]=malloc(strlen(nm)+1);
  40.     strcpy(p->songs[numEl(p)], nm);
  41.     p->num++;
  42. }
  43.  
  44. char *authorName(hierarchy p)
  45. {
  46.     return p->name;
  47. }
  48.  
  49. char **songNames(hierarchy p)
  50. {
  51.     return p->songs;
  52. }
  53.  
  54. int numEl(hierarchy p)
  55. {
  56.     return p->num;
  57. }
  58.  
  59. void printHierarchy(hierarchy p)
  60. {
  61.     for(int i=0; i<numEl(p); i++)
  62.     printf("%s\t %s\n", authorName(p), songNames(p)[i]);
  63. }
  64.  
  65. void printAllHierarchy(hierarchy *p, int n)
  66. {
  67.     for(int i=0; i<n; i++)
  68.     printHierarchy(p[i]);
  69. }
  70.  
  71. //funzione di servizio
  72. int searchAuthor(hierarchy* res, char *author, int numAuthors)
  73. {  
  74.     for(int i=0; i<numAuthors ; i++)
  75.     if(strcmp(authorName(res[i]), author)==0)
  76.     return 1;  //trovato
  77.     return 0;  //non trovato
  78. }
  79.  
  80. //funzione di servizio
  81. int findAuthorNum(hierarchy *res, char *author, int numAuthors)
  82. {
  83.     int i=0;
  84.     for(; i<numAuthors ; i++)
  85.     if(strcmp(authorName(res[i]), author)==0)
  86.     return i;
  87. }
  88.  
  89. void newAuthor2(hierarchy *p, int i, char *name)
  90. {
  91.     p[i]=malloc(sizeof(hierarchy));
  92.     if(!p[i]) exit (9);
  93.     p[i]->name=malloc(strlen(name)+1);
  94.     charChecker(p[i]->name);
  95.     strcpy(p[i]->name, name);
  96.     p[i]->num=0;
  97. }
  98.  
  99. hierarchy *extract(FILE *p)
  100. {
  101.     int numAuthors=0, MAX=5;
  102.     char *author, *song;
  103.     hierarchy *result=malloc(sizeof(hierarchy)*MAX);
  104.     while(!feof(p))
  105.     {
  106.         if(numAuthors>=MAX)
  107.         {
  108.             MAX+=1;
  109.             result=realloc(result, sizeof(hierarchy)*MAX);
  110.         }
  111.         author=malloc(sizeof(char)*MAXL1);
  112.         song=malloc(sizeof(char)*MAXL2);
  113.         charChecker(author);
  114.         charChecker(song);
  115.         fscanf(p, "%s\t %s", author, song);
  116.         author=realloc(author, strlen(author)+1);
  117.         song=realloc(song, strlen(song)+1);
  118.         charChecker(author);
  119.         charChecker(song);
  120.         if(!searchAuthor(result, author, numAuthors))   //se l'autore non viene trovato
  121.         {
  122.             newAuthor2(result, numAuthors, author);     //aggiungo l'autore    
  123.             addSong(result[numAuthors], song);       //aggiungo la canzone
  124.             numAuthors++;
  125.         }
  126.         //l'autore è già presente
  127.         else
  128.         addSong(result[findAuthorNum(result, author, numAuthors)], song);   //trovo la corrispondenza nell'array
  129.         free(author);
  130.         free(song);
  131.     }
  132. }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement