fork download
  1. // classificazione_prodotti.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. // --- DEFINIZIONE ADT Mappa ---
  8. typedef struct Nodo {
  9. char *keyword;
  10. char *categoria;
  11. struct Nodo *next;
  12. } Nodo;
  13.  
  14. typedef struct {
  15. Nodo *testa;
  16. } Mappa;
  17.  
  18. // --- FUNZIONI DI UTILITÀ ---
  19. char *strdup_sicuro(const char *src) {
  20. if (!src) return NULL;
  21. char *dest = malloc(strlen(src) + 1);
  22. if (dest) strcpy(dest, src);
  23. return dest;
  24. }
  25.  
  26. void to_lowercase(char *str) {
  27. for (; *str; ++str) *str = tolower(*str);
  28. }
  29.  
  30. // --- INTERFACCIA ADT ---
  31. Mappa *crea_mappa() {
  32. Mappa *mappa = malloc(sizeof(Mappa));
  33. if (mappa) mappa->testa = NULL;
  34. return mappa;
  35. }
  36.  
  37. void distruggi_mappa(Mappa *mappa) {
  38. if (!mappa) return;
  39. Nodo *corr = mappa->testa;
  40. while (corr) {
  41. Nodo *temp = corr;
  42. corr = corr->next;
  43. free(temp->keyword);
  44. free(temp->categoria);
  45. free(temp);
  46. }
  47. free(mappa);
  48. }
  49.  
  50. void inserisci_mappa(Mappa *mappa, const char *keyword, const char *categoria) {
  51. if (!mappa || !keyword || !categoria) return;
  52. Nodo *nodo = malloc(sizeof(Nodo));
  53. if (!nodo) return;
  54. nodo->keyword = strdup_sicuro(keyword);
  55. nodo->categoria = strdup_sicuro(categoria);
  56. if (!nodo->keyword || !nodo->categoria) {
  57. free(nodo->keyword);
  58. free(nodo->categoria);
  59. free(nodo);
  60. return;
  61. }
  62. nodo->next = mappa->testa;
  63. mappa->testa = nodo;
  64. }
  65.  
  66. const char *trova_categoria(Mappa *dizionario, const char *prodotto) {
  67. static char nome_copy[100];
  68. strncpy(nome_copy, prodotto, sizeof(nome_copy));
  69. nome_copy[sizeof(nome_copy) - 1] = '\0';
  70. to_lowercase(nome_copy);
  71.  
  72. Nodo *corr = dizionario->testa;
  73. while (corr) {
  74. if (strstr(nome_copy, corr->keyword)) {
  75. return corr->categoria;
  76. }
  77. corr = corr->next;
  78. }
  79. return "Categoria sconosciuta";
  80. }
  81.  
  82. void mostra_per_categoria(Mappa *prodotti, const char *categoria) {
  83. Nodo *corr = prodotti->testa;
  84. printf("Prodotti nella categoria '%s':\n", categoria);
  85. while (corr) {
  86. if (strcmp(corr->categoria, categoria) == 0) {
  87. printf("- %s\n", corr->keyword);
  88. }
  89. corr = corr->next;
  90. }
  91. }
  92.  
  93. void salva_mappa_su_file(Mappa *prodotti, const char *nomefile) {
  94. FILE *f = fopen(nomefile, "w");
  95. if (!f) return;
  96. Nodo *corr = prodotti->testa;
  97. while (corr) {
  98. fprintf(f, "%s;%s\n", corr->keyword, corr->categoria);
  99. corr = corr->next;
  100. }
  101. fclose(f);
  102. }
  103.  
  104. void carica_mappa_da_file(Mappa *prodotti, const char *nomefile) {
  105. FILE *f = fopen(nomefile, "r");
  106. if (!f) return;
  107. char riga[256];
  108. while (fgets(riga, sizeof(riga), f)) {
  109. char *newline = strchr(riga, '\n');
  110. if (newline) *newline = '\0';
  111. char *sep = strchr(riga, ';');
  112. if (sep) {
  113. *sep = '\0';
  114. inserisci_mappa(prodotti, riga, sep + 1);
  115. }
  116. }
  117. fclose(f);
  118. }
  119.  
  120. void inizializza_dizionario_base(Mappa *dizionario) {
  121. struct iniziale {
  122. const char *keyword;
  123. const char *categoria;
  124. } diz[] = {
  125. {"pasta", "Sfarinati"}, {"riso", "Sfarinati"},
  126. {"farina", "Sfarinati"}, {"salame", "Salumeria/Macelleria"},
  127. {"prosciutto", "Salumeria/Macelleria"}, {"pollo", "Salumeria/Macelleria"},
  128. {"manzo", "Salumeria/Macelleria"}, {"formaggio", "Salumeria/Macelleria"},
  129. {"carota", "Verdura/Frutta"}, {"cipolla", "Verdura/Frutta"},
  130. {"mela", "Verdura/Frutta"}, {"banana", "Verdura/Frutta"},
  131. {"acqua", "Bevande"}, {"birra", "Bevande"},
  132. {"vino", "Bevande"}, {"aranciata", "Bevande"},
  133. };
  134. int n = sizeof(diz) / sizeof(diz[0]);
  135. for (int i = 0; i < n; i++) {
  136. inserisci_mappa(dizionario, diz[i].keyword, diz[i].categoria);
  137. }
  138. }
  139.  
  140. // --- MAIN ---
  141. int main() {
  142. Mappa *dizionario = crea_mappa();
  143. Mappa *prodotti = crea_mappa();
  144. inizializza_dizionario_base(dizionario);
  145. carica_mappa_da_file(prodotti, "dati.txt");
  146.  
  147. char input[100];
  148. int scelta;
  149.  
  150. while (1) {
  151. printf("\n--- Menu ---\n");
  152. printf("1. Classifica un prodotto\n");
  153. printf("2. Mostra prodotti per categoria\n");
  154. printf("3. Esci\n");
  155. printf("Scelta: ");
  156. if (scanf("%d", &scelta) != 1) break;
  157. getchar(); // Consuma newline
  158.  
  159. if (scelta == 1) {
  160. printf("Inserisci il nome del prodotto: ");
  161. fgets(input, sizeof(input), stdin);
  162. input[strcspn(input, "\n")] = '\0';
  163. const char *categoria = trova_categoria(dizionario, input);
  164. printf("Il prodotto \"%s\" appartiene al settore: %s\n", input, categoria);
  165. if (strcmp(categoria, "Categoria sconosciuta") != 0) {
  166. inserisci_mappa(prodotti, input, categoria);
  167. }
  168. } else if (scelta == 2) {
  169. printf("Inserisci il nome della categoria: ");
  170. fgets(input, sizeof(input), stdin);
  171. input[strcspn(input, "\n")] = '\0';
  172. mostra_per_categoria(prodotti, input);
  173. } else if (scelta == 3) {
  174. printf("Salvataggio e uscita...\n");
  175. salva_mappa_su_file(prodotti, "dati.txt");
  176. distruggi_mappa(dizionario);
  177. distruggi_mappa(prodotti);
  178. break;
  179. } else {
  180. printf("Scelta non valida.\n");
  181. }
  182. }
  183. return 0;
  184. }
  185.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
--- Menu ---
1. Classifica un prodotto
2. Mostra prodotti per categoria
3. Esci
Scelta: