Advertisement
Denny707

L16-07-2014

Jun 15th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX 50
  4.  
  5. struct nodo{
  6. char cognome[MAX];
  7. char nome[MAX];
  8. int eta;
  9. int prezzo;
  10. struct nodo* link;
  11. };
  12. int i=0;
  13. void add(struct nodo * head,char nome[], char cognome[], int eta,int prezzo){
  14. if(head->link==NULL){
  15. struct nodo* nuovo =( struct nodo * ) malloc ( sizeof ( struct nodo));
  16. nuovo->eta=eta;
  17. nuovo->prezzo=prezzo;
  18. strcpy(nuovo->nome, nome);
  19. strcpy(nuovo->cognome, cognome);
  20.  
  21. nuovo->link=NULL;
  22. head->link=nuovo;
  23. }else{
  24. add(head->link,nome,cognome,eta,prezzo);
  25. }
  26. }
  27.  
  28. void display(struct nodo * head){
  29. if(head){
  30. printf("%s %s %d [%d]\n",head->nome,head->cognome,head->eta,head->prezzo);
  31. display(head->link);
  32. }
  33. }
  34.  
  35. int costruisciLista(struct nodo * head){
  36. FILE *fp;
  37. char buff[50];
  38. int stato;
  39. fp = fopen("UTENTI_PAGANTI.txt", "r");
  40.  
  41. int somma=0;
  42.  
  43. while(stato!=EOF){
  44. char nome[MAX],cognome[MAX];
  45. int prezzo=0, eta=0;
  46. stato=fscanf(fp,"%s",cognome);
  47. stato=fscanf(fp,"%s",nome);
  48. stato=fscanf(fp,"%d",&eta);
  49. stato=fscanf(fp,"%d",&prezzo);
  50.  
  51. if(stato!=EOF){
  52. somma+=eta;
  53. add(head,nome,cognome,eta,prezzo);
  54. i++;
  55. }
  56. }
  57.  
  58. fclose(fp);
  59. return somma/i;
  60. }
  61.  
  62. int incasso(struct nodo* head,int somma){
  63. if(head){
  64. return incasso(head->link,somma+head->prezzo);
  65. }
  66. return somma;
  67. }
  68.  
  69. int main(int argc, const char * argv[]) {
  70. struct nodo* head =(struct nodo*)malloc(sizeof(struct nodo));
  71. head->eta=2;
  72. head->prezzo=2;
  73. head->link=NULL;
  74. printf("La media degli anni dei visitatori รจ: %i, mentre il loro numero รจ %d\n",costruisciLista(head),i);
  75. printf("Infine l'incasso รจ pari ad %i\n",incasso(head->link,0));
  76. //display(head->link);
  77.  
  78. return 0;
  79. }
  80.  
  81. /* create file
  82. FILE *fp;
  83.  
  84. fp = fopen("UTENTI_PAGANTI.txt", "w+");
  85. fprintf(fp, "Vituviano Ermanno 35 12\n");
  86. fprintf(fp, "De Rosa Antonia 22 12\n");
  87. fprintf(fp, "Albano Vittorio 18 6\n");
  88. fprintf(fp, "Tintori Enry 65 6\n");
  89. fclose(fp);
  90.  
  91. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement