Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 25
  5.  
  6. typedef struct node *link;
  7.  
  8. typedef struct Item{
  9. char codice[5];
  10. char*p_nome,*p_cognome,*p_categoria;
  11. struct{
  12. int a,m,g;
  13. } date;
  14. int ore;
  15. }item;
  16.  
  17. struct node{
  18. item atleta;
  19. link next;
  20. };
  21.  
  22. link newNode(item,link);
  23. link caricastrutture(link);
  24. void stampa(link);
  25. void stampa_a_video_anagrafica(link);
  26. void stampa_su_file_anagrafica(link);
  27. link aggiungi_al_fondo(link);
  28. link listSearch(link,char k[5]);
  29. link cancella_chiave(link h,char c[5]);
  30. void ricerca_codice(link);
  31. void aggiorna_ore(head);
  32.  
  33.  
  34. int main()
  35. {
  36. char c[5];
  37. link head=NULL;
  38. head=caricastrutture(head);
  39. printf("\n\n\n");
  40. stampa(head);
  41. //stampa_a_video_anagrafica(head);
  42. //stampa_su_file_anagrafica(head);
  43. //ricerca_codice(head);
  44. //aggiorna_ore(head);
  45. //head=aggiungi_al_fondo(head);
  46. printf("\ncodice dell'atleta da eliminare:");
  47. scanf("%s",c);
  48. printf("\n%s");
  49. head=cancella_chiave(head,c);
  50. //stampa(head);
  51. return 0;
  52. }
  53.  
  54. link newNode(item atleta,link next) {
  55. link x = malloc(sizeof *x);
  56. if (x==NULL)
  57. return NULL;
  58. else {
  59. x->atleta = atleta;
  60. x->next = next;
  61. }
  62. return x;
  63. }
  64.  
  65.  
  66. link caricastrutture(link head){
  67. FILE *fp=fopen("atleti.txt","r");
  68. char nome[MAX],cognome[MAX],categorie[MAX];
  69. int n,i;
  70. item a;
  71. fscanf(fp,"%d",&n);
  72. printf("%d",n);
  73. for(i=0;i<n;i++){
  74. fscanf(fp,"%s %s %s %s %d/%d/%d %d",a.codice,nome,cognome,categorie,&a.date.g,&a.date.m,&a.date.a,&a.ore);
  75. printf("\n%s %s %s %s %d/%d/%d %d",a.codice,nome,cognome,categorie,a.date.g,a.date.m,a.date.a,a.ore);
  76. a.p_nome=strdup(nome);
  77. a.p_cognome=strdup(cognome);
  78. a.p_categoria=strdup(categorie);
  79. head=newNode(a,head);
  80. printf("\n%s %s %s %s %d/%d/%d %d",head->atleta.codice,head->atleta.p_nome,head->atleta.p_cognome,head->atleta.p_categoria,head->atleta.date.g,head->atleta.date.m,head->atleta.date.a,head->atleta.ore);
  81. // free(a.p_nome);
  82. //free(a.p_cognome);
  83. //free(a.p_categoria);
  84. printf("\n");
  85. }
  86. //*V=a;
  87. return head;
  88. }
  89.  
  90. void stampa(link head){
  91. link x;
  92. for (x=head; x!=NULL; x=x->next){
  93. printf("\n%s %s %s %s %d/%d/%d %d",x->atleta.codice,x->atleta.p_nome,x->atleta.p_cognome,x->atleta.p_categoria,x->atleta.date.g,x->atleta.date.m,x->atleta.date.a,x->atleta.ore);
  94. }
  95. }
  96.  
  97. void stampa_a_video_anagrafica(link head){
  98. link x;
  99. for (x=head; x!=NULL; x=x->next){
  100. printf("\n%s %s %d/%d/%d",x->atleta.p_nome,x->atleta.p_cognome,x->atleta.date.g,x->atleta.date.m,x->atleta.date.a);
  101. }
  102. }
  103.  
  104. void stampa_su_file_anagrafica(link head){
  105. FILE *out=fopen("out.txt","w");
  106. link x;
  107. for (x=head; x!=NULL; x=x->next){
  108. fprintf(out,"%s %s %d/%d/%d\n",x->atleta.p_nome,x->atleta.p_cognome,x->atleta.date.g,x->atleta.date.m,x->atleta.date.a);
  109. }
  110. }
  111.  
  112. link listSearch(link h,char k[5]) {
  113. link x;
  114. for (x=h; x!=NULL; x=x->next){
  115. if (strcmp(k,x->atleta.codice)==0){
  116. printf("trovato---->%s",x->atleta.p_nome);
  117. return x;
  118. }
  119. }
  120. return NULL;
  121. }
  122.  
  123.  
  124. void ricerca_codice(link head){
  125. link x;
  126. char cod[5];
  127. printf("\nscrivere codice da ricercare:");
  128. scanf("%s",cod);
  129. printf("%s",cod);
  130. x=listSearch(head,cod);
  131. printf("%s %s",x->atleta.p_nome,x->atleta.p_cognome);
  132. }
  133.  
  134. void aggiorna_ore(link head){
  135. link x;
  136. printf("a chi vuoi aggiornare le ore:");
  137. char cod[5];
  138. scanf("%s",cod);
  139. x=listSearch(head,cod);
  140. scanf("%d",&(x->atleta.ore));
  141. printf("%s %s %s %s %d/%d/%d %d",x->atleta.codice,x->atleta.p_nome,x->atleta.p_cognome,x->atleta.p_categoria,x->atleta.date.g,x->atleta.date.m,x->atleta.date.a,x->atleta.ore);
  142. }
  143.  
  144. link aggiungi_al_fondo(link head){
  145. char nome[MAX],cognome[MAX],categorie[MAX];
  146. item a;
  147. scanf("%s %s %s %s %d/%d/%d %d",a.codice,nome,cognome,categorie,&a.date.g,&a.date.m,&a.date.a,&a.ore);
  148. a.p_nome=strdup(nome);
  149. a.p_cognome=strdup(cognome);
  150. a.p_categoria=strdup(categorie);
  151. link x;
  152. if (head==NULL)
  153. return newNode(a, NULL);
  154. for (x=head; x->next!=NULL; x=x->next);
  155. x->next = newNode(a, NULL);
  156. return head;
  157. }
  158.  
  159. link cancella_chiave(link h,char c[5]) {
  160.  
  161. link x,p;
  162. if (h == NULL)
  163. return NULL;
  164. for (x=h, p=NULL; x!=NULL; p=x, x=x->next) {
  165. if (strcmp(c, x->atleta.codice)==0) {
  166. printf("trovato----->%s",x->atleta.codice);
  167. if (x==h) h=x->next;
  168. else p->next=x->next;
  169. free(x);
  170. break;
  171. }
  172. }
  173. return h;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement