Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct nodo{
  7. char codice[50];
  8. char nome[50];
  9. char cognome[50];
  10. char data[50];
  11. char via[50];
  12. char citta[50];
  13. int cap;
  14. int dataformattata;
  15. struct nodo* next;
  16. }anagrafica,*link;
  17.  
  18.  
  19. int formattadata(char *data);
  20. void strtolower(char *parola);
  21. link creanodo(anagrafica struttura, link next);
  22. link inseriscinodo(link nodo, anagrafica struttura);
  23. void stampalista(link head);
  24. anagrafica acquisiscinodotastiera(void);
  25. void acquisiscinodofile(link *head);
  26.  
  27. typedef enum
  28. {
  29. acquisisci_tastiera,acquisisci_file,ricerca,cancellazione_codice,cancellazione_date,stampa_file,errore
  30. }comandi;
  31. comandi leggicomando();
  32.  
  33.  
  34.  
  35. int main() {
  36. int flag=0;
  37. anagrafica temporanea;
  38. link head=NULL;
  39. while(flag==0)
  40. {
  41.  
  42. switch (leggicomando()) {
  43.  
  44.  
  45. case acquisisci_tastiera:
  46. temporanea=acquisiscinodotastiera();
  47. head=inseriscinodo(head,temporanea);
  48.  
  49. break;
  50. case acquisisci_file:
  51. acquisiscinodofile(&head);
  52.  
  53. break;
  54. case ricerca:
  55.  
  56. break;
  57. case cancellazione_codice:
  58. break;
  59. case cancellazione_date:
  60. break;
  61. case stampa_file:
  62. stampalista(head);
  63. break;
  64. case errore:
  65. printf("Comando sbagliato,esco dal programma\n");
  66. flag=1;
  67. break;
  68. }
  69. }
  70.  
  71.  
  72.  
  73. return 0;
  74. }
  75.  
  76.  
  77. int formattadata(char *data)
  78. {
  79. int anno,mese,giorno;
  80. int ris;
  81. sscanf(data,"%d/%d/%d",&giorno,&mese,&anno);
  82. ris=giorno+(mese*100)+(anno*10000);
  83. return ris;
  84.  
  85. }
  86. comandi leggicomando()
  87. {
  88. char parola[30];
  89. comandi c;
  90. char tabella[6][30]={"acquisisci_tastiera","acquisisci_file","ricerca","cancellazione_codice","cancellazione_date","stampa_file"};
  91. printf("Inserisci il comando che vuoi tra:\n");
  92. printf("Acquisisci_tastiera per aggiungere alla lista un dato item da tastiera\n");
  93. printf("Acquisisci_file per aggiungere alla lista un dato item da file\n");
  94. printf("Ricerca per cercare un elemento con il codice\n");
  95. printf("Cancella_codice per cancellare un elemento dalla lista con il codice desiderato\n");
  96. printf("Cancella_date per eliminare tutti gli elementi della lista compresi fra due date\n");
  97. printf("Stampa_file per stampare la lista\n\n");
  98. scanf("%s",parola);
  99. strtolower(parola);
  100. c=acquisisci_tastiera;
  101. while (c<errore && strcmp(tabella[c],parola)!=0)
  102. {
  103. c++;
  104. }
  105. return c;
  106.  
  107.  
  108. }
  109. void strtolower(char *parola)
  110. {
  111. int i;
  112. for (i=0;i<strlen(parola);i++)
  113. parola[i]=tolower(parola[i]);
  114.  
  115. return;
  116. }
  117.  
  118.  
  119. link creanodo(anagrafica struttura, link next)
  120. {
  121. link x;
  122. x=(anagrafica*)malloc(sizeof(anagrafica));
  123. if(x==NULL)
  124. {
  125. return NULL;
  126. }
  127. else
  128. {
  129. strcpy(x->nome,struttura.nome);
  130. strcpy(x->cognome,struttura.cognome);
  131. strcpy(x->codice,struttura.codice);
  132. x->cap=struttura.cap;
  133. strcpy(x->citta,struttura.citta);
  134. strcpy(x->data,struttura.data);
  135. strcpy(x->via,struttura.via);
  136. x->dataformattata=formattadata(struttura.data);
  137. x->next=next;
  138. }
  139. return x;
  140. }
  141.  
  142.  
  143. anagrafica acquisiscinodotastiera(void)
  144. {
  145. anagrafica base;
  146. printf("Inserisci nome:\n");
  147. scanf("%s",base.nome);
  148. printf("Inserisci cognome:\n");
  149. scanf("%s",base.cognome);
  150. printf("Inserisci via:\n");
  151. scanf("%s",base.via);
  152. printf("Inserisci codice:\n");
  153. scanf("%s",base.codice);
  154. printf("Inserisci cap:\n");
  155. scanf("%d",&base.cap);
  156. printf("Inserisci citta:\n");
  157. scanf("%s",base.citta);
  158. printf("Inserisci data in formato gg/mm/aaaa:\n");
  159. scanf("%s",base.data);
  160. base.dataformattata=formattadata(base.data);
  161. return base;
  162. }
  163. link inseriscinodo(link nodo, anagrafica struttura)
  164. {
  165. link x,p,tmp;
  166. if (nodo==NULL)
  167. {
  168. return creanodo(struttura,nodo);
  169. }
  170. for(x=nodo->next,p=nodo;x->next!=NULL;p=x,x=x->next)
  171. {
  172. if(p->dataformattata<struttura.dataformattata && struttura.dataformattata<x->dataformattata)
  173. {
  174. tmp=creanodo(struttura,p->next);
  175.  
  176. p->next=tmp;
  177. }
  178.  
  179. }
  180. return nodo;
  181.  
  182. }
  183. void stampalista(link head)
  184. {
  185. if(head==NULL)
  186. {
  187. return;
  188. }
  189. printf("%s %s %s %s %d %s %s\n ",head->nome,head->cognome,head->via,head->codice,head->cap,head->citta,head->data);
  190. stampalista(head->next);
  191. return;
  192. }
  193. void acquisiscinodofile(link *head)
  194. {
  195. char nomefile[50];
  196.  
  197. FILE* fp;
  198. anagrafica tmp;
  199. printf("Inserisci il nome del file\n");
  200. scanf("%s",nomefile);
  201. fp=fopen(nomefile,"r");
  202. if(fp==NULL)
  203. {
  204. printf("File non letto\n");
  205. return;
  206. }
  207. while (fscanf(fp,"%s %s %s %s %d %s %s",tmp.nome,tmp.cognome,tmp.via,tmp.codice,&tmp.cap,tmp.citta,tmp.data)!=EOF)
  208. {
  209. tmp.dataformattata=formattadata(tmp.data);
  210. (*head)= inseriscinodo(*head,tmp);
  211.  
  212. }
  213. fclose(fp);
  214. return;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement