Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct studente {
  6.     int matricola;
  7.     char cognome[20];
  8.     char nome[20];
  9.     char cod_id[6];
  10.     float media;
  11.     struct studente *successivo;
  12. } studente;
  13.  
  14.  
  15. void leggi_file (FILE *input, studente **testa1);
  16. void change_head (studente **l1);
  17. void new_head (studente *l1, studente **l2, char *dato);
  18. void stampa (studente *testa1, FILE *output);
  19.  
  20. int main (int argc, char **argv)
  21. {
  22.     FILE *input, *output;
  23.     studente *testa1, *testa2;
  24.     char *dato="nappi";
  25.    
  26.     input=fopen(argv[1], "r");
  27.     output=fopen(argv[2], "w");
  28.     leggi_file (input, &testa1);
  29.     new_head (testa1, &testa2, dato);
  30.     change_head (&testa1);
  31.     stampa (testa1, output);
  32.     return 0;
  33. }
  34.  
  35.  
  36. void leggi_file(FILE * input1, studente ** testa)
  37. {
  38.     studente *prec, *act;
  39.    
  40.     int matricola;
  41.    
  42.     if (fscanf(input1, "%d", &matricola)!= EOF)
  43.     {
  44.         prec = act = *testa = malloc(sizeof(studente));
  45.         act->matricola = matricola;
  46.         fscanf(input1, "%s %s %s %f", act->cognome, act->nome, act->cod_id, &act->media);
  47.     }
  48.    
  49.     while(fscanf(input1, "%d", &matricola)!= EOF)
  50.     {
  51.         prec = act;
  52.         act = malloc(sizeof(studente));
  53.         act->matricola = matricola;
  54.         fscanf(input1, "%s %s %s %f", act->cognome, act->nome, act->cod_id, &act->media);
  55.         prec->successivo = act;
  56.     }
  57.    
  58. }
  59.  
  60. void change_head(studente **l1)
  61. {
  62.  
  63.     studente *coda, *testa, *act, *prev;
  64.     testa = act = *l1;
  65.     while(act)
  66.     {
  67.         /*1*/prev = coda;
  68.         coda = act;
  69.         act = act->successivo;
  70.     }
  71.     *l1 = coda;
  72.     /*2*/prev->successivo = NULL;
  73.     coda->successivo = testa->successivo;
  74.     act = *l1;
  75.     while(act)
  76.     {
  77.         coda = act;
  78.         act = act->successivo;
  79.     }
  80.     coda->successivo = testa;
  81.     testa->successivo = NULL;
  82.    
  83. }
  84.  
  85. void new_head(studente * l1, studente **l2, char *dato)
  86. {
  87.     studente * prec, *act, *to_add;
  88.    
  89.     prec = act = l1;
  90.    
  91.     while(act)
  92.     {
  93.         if(strcmp(act->cognome, dato) == 0)
  94.         {
  95.             to_add = act->successivo;
  96.             prec = act;
  97.             break;
  98.         }
  99.         prec = act;
  100.         act = act->successivo;
  101.     }
  102.     prec->successivo = NULL;
  103.     if(to_add)
  104.     {
  105.         *l2 = to_add;
  106.     }
  107. }
  108.  
  109. void stampa (studente *testa, FILE *output)
  110. {
  111.     while(testa)
  112.     {
  113.         if(testa->media > 27){
  114.             printf( "%d %s %s %s %f\n", testa->matricola, testa->cognome, testa->nome, testa->cod_id, testa->media);
  115.         }
  116.         testa = testa->successivo;
  117.     }    
  118.    
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement