Announcement

Collapse
No announcement yet.

Strutture ordinate

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strutture ordinate

    PHP Code:
    #include <st***.h>
    #include <stdlib.h>
    #include <string.h>

    #define ELEMENTS(array) (sizeof(array)/sizeof(array[0]))
    #define NUMBER 2


    typedef struct {
     
        
    char nome;
        
    char cognome;
        
    unsigned int esami;
        
    float media;
        
    studente_struct;

    int compara_struct_nome (const void , const void b);
    void stampa_struct studente_struct a);



    int main void ) {
        
    char buffer[50];
    int i;

    studente_struct studente[NUMBER];

    for (
    i=0;i<NUMBER;i++){

        
    printf ("Nome\n");
        
    scanf ("%49s",buffer);
        (*(
    studente +i)).nomestrdup(buffer);
        
        
    printf ("Cognome\n");
        
    scanf ("%49s",buffer);
        (*(
    studente +i)).cognomestrdup(buffer);
        
        
    printf ("Esami\n");
        
    scanf ("%u",&((*(studente+i)).esami) );
        
        
    printf ("Media\n");
        
    scanf ("%f",&((*(studente+i)).media) );
        
    }

    ////////////////////////////// ordina per nome
        
        
    qsort(studente ELEMENTS(studente) , sizeof(studente[0]), compara_struct_nome);
        
    stampa_struct (studente);

    /////////////////////////////////

     
    return 0;
    }


    int compara_struct_nome (const void , const void b){
        
        
    studente_struct dat1 = ( studente_struct *) a;
        
    studente_struct dat2 = (studente_struct *) b;
        
        return 
    strcmp ( (*(dat1)).nome , (*(dat2)).nome );
        
    }

    void stampa_struct studente_struct a){
        
        
    int i;
        
        for(
    i=0;i<NUMBER;i++){
            
            
    printf ("%s\n", (*(i)).nome);
            
    printf ("%s\n", (*(i)).cognome);
            
    printf ("%u\n", (*(i)).esami);
            
    printf ("%f\n", (*(i)).media);
            
        }
        

    | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio
Working...
X

Google Profile


My name is Giovanni Di Grezia, but people call me xgiovio.

Here is my homepage:.

I'm a VFX Artist and Software Developer.

Giovanni Di Grezia