Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define MAX 50
- struct nodo{
- char cognome[MAX];
- char nome[MAX];
- int eta;
- int prezzo;
- struct nodo* link;
- };
- int i=0;
- void add(struct nodo * head,char nome[], char cognome[], int eta,int prezzo){
- if(head->link==NULL){
- struct nodo* nuovo =( struct nodo * ) malloc ( sizeof ( struct nodo));
- nuovo->eta=eta;
- nuovo->prezzo=prezzo;
- strcpy(nuovo->nome, nome);
- strcpy(nuovo->cognome, cognome);
- nuovo->link=NULL;
- head->link=nuovo;
- }else{
- add(head->link,nome,cognome,eta,prezzo);
- }
- }
- void display(struct nodo * head){
- if(head){
- printf("%s %s %d [%d]\n",head->nome,head->cognome,head->eta,head->prezzo);
- display(head->link);
- }
- }
- int costruisciLista(struct nodo * head){
- FILE *fp;
- char buff[50];
- int stato;
- fp = fopen("UTENTI_PAGANTI.txt", "r");
- int somma=0;
- while(stato!=EOF){
- char nome[MAX],cognome[MAX];
- int prezzo=0, eta=0;
- stato=fscanf(fp,"%s",cognome);
- stato=fscanf(fp,"%s",nome);
- stato=fscanf(fp,"%d",&eta);
- stato=fscanf(fp,"%d",&prezzo);
- if(stato!=EOF){
- somma+=eta;
- add(head,nome,cognome,eta,prezzo);
- i++;
- }
- }
- fclose(fp);
- return somma/i;
- }
- int incasso(struct nodo* head,int somma){
- if(head){
- return incasso(head->link,somma+head->prezzo);
- }
- return somma;
- }
- int main(int argc, const char * argv[]) {
- struct nodo* head =(struct nodo*)malloc(sizeof(struct nodo));
- head->eta=2;
- head->prezzo=2;
- head->link=NULL;
- printf("La media degli anni dei visitatori รจ: %i, mentre il loro numero รจ %d\n",costruisciLista(head),i);
- printf("Infine l'incasso รจ pari ad %i\n",incasso(head->link,0));
- //display(head->link);
- return 0;
- }
- /* create file
- FILE *fp;
- fp = fopen("UTENTI_PAGANTI.txt", "w+");
- fprintf(fp, "Vituviano Ermanno 35 12\n");
- fprintf(fp, "De Rosa Antonia 22 12\n");
- fprintf(fp, "Albano Vittorio 18 6\n");
- fprintf(fp, "Tintori Enry 65 6\n");
- fclose(fp);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement