Announcement

Collapse
No announcement yet.

Lista - Conteggio Parole e Stampe senza ripetizioni

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

  • Lista - Conteggio Parole e Stampe senza ripetizioni

    PHP Code:
    /*
     *
     *
     * input:parole
     * output: lista ordinata per nome senza ripetizioni + conteggio ripetizioni
     *
     *
     */


    #include <st***.h>
    #include <stdlib.h>
    #include <string.h>

    #define WORD_SIZE 100

    typedef struct string {
        
        
    char word;
        
    unsigned int counter;
        
    struct string next;
        
    word_struct;


    word_struct new_word_struct char input_word unsigned int input_counter );
    word_struct add_to_word_struct_list (word_struct nodeword_struct * list );
    void convert_lower_case (char bufferint k);

    //////////////////////////////////////////////////////////////////////////////////////////////////begin

    int main (void ) {
        
        
    char a;
        
    char buff[WORD_SIZE]; // buf temporaneo per le parole da input
        
    int i;
        
    word_struct list_word_struct NULL // Lista vuota di tipo word_struct
        
    word_struct temp_word_struct// Node temporaneo di tipo word_struct
        
        
        
    a=fgetc(stdin);
        
        for (; 
    a!=EOF a=fgetc(stdin) ){
            
            
    ///////////////////////////////////////////////////////////////// parole nel buff
        
            
    for (i=a!= ' ' && a!= '\n' && a!= '\t' i++){
                
                
    buff[i]=a;
                
    a=fgetc(stdin);
                
            }
            
    buff[i]=0;
            
            
    convert_lower_case (buff,i);  

            if (
    buff[0] != 0){
            
                
    temp_word_struct new_word_struct (buff,1); // nuovo elemento di tipo word_struct
                
                
    list_word_struct add_to_word_struct_list (temp_word_structlist_word_struct ); // aggiunta di un elemento word_struct a lista word_struct
            
    }
        
        }
        
        
        
    ///////////////////////////////////////////////////////////////////////////// stampa parole e numero inserimenti
        
        
    printf ("\nParole Inserite e ripetizioni :\n");
        
        for (
    temp_word_struct list_word_struct temp_word_struct != NULLtemp_word_struct = (*temp_word_struct).next){
            
            
    printf("%s , %u\n",(*temp_word_struct).word,(*temp_word_struct).counter);
        }
        
     return 
    0;  
    }





    ///////////////////////////////////////////////////////////////////////////////// functions



    /////////////////////////////////////////////////////////////////////convert to lowercase
    void convert_lower_case (char bufferint k){
        
        
    int i;
        
        for (
    i=0;i<k;i++){
            
            if (
    buffer[i] >= 'A' && buffer[i] <= 'Z'){
                
                
    buffer[i] = buffer[i] + ('a'-'A');
            }
            
        }
        
    }



    //////////////////////////////////////////////////////////////////// new node
    word_struct new_word_struct char input_word unsigned int input_counter ){
        
        
    word_struct pointer;
        
    pointer = ( word_struct * ) malloc sizeof(word_struct));
        
        (*
    pointer).word strdup(input_word);
        (*
    pointer).counter input_counter;
        (*
    pointer).next NULL;
        
        
        return 
    pointer;
        
    }

    ///////////////////////////////////////////////////////////////////////////////// insert node
    word_struct add_to_word_struct_list (word_struct nodeword_struct * list ){
        
          
        
    //////////////////////////////////// I caso : Lista Nulla
        
    if (list == NULL){
            return 
    node;
        }
        
        
        
    //////////////////////////////////// II caso : Inserimento in testa
        
        
    if (strcmp((*node).word,(*list).word) <0){
            
            (*
    node).next = list ;
                return 
    node;
        }
        
        if (
    strcmp((*node).word,(*list).word)==0){
            
                (*list).
    counter += 1;
                
    free((*node).word);
                return list;
        }
        
      
        
    //////////////////////////////////////III caso Inserimento nel mezzo
      
        
    (*list).next add_to_word_struct_list (node, (*list).next );
        return list;
        
      

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

  • #2
    Ricorsivo Ma senza Malloc e free - frammentazione minore della memoria

    PHP Code:
    /*
     *
     *
     * input:parole
     * output: lista ordinata per nome senza ripetizioni + conteggio ripetizioni
     * rev 2 - creazione nodo solo se stringa non presente
     *
     */


    #include <st***.h>
    #include <stdlib.h>
    #include <string.h>

    #define WORD_SIZE 100

    typedef struct string {
        
        
    char word;
        
    unsigned int counter;
        
    struct string next;
        
    word_struct;


    word_struct new_word_struct char input_word unsigned int input_counter );
    word_struct add_to_word_struct_list (word_struct nodeword_struct * list );
    void convert_lower_case (char bufferint k);

    //////////////////////////////////////////////////////////////////////////////////////////////////begin

    int main (void ) {
        
        
    char a;
        
    char buff[WORD_SIZE]; // buf temporaneo per le parole da input
        
    int i;
        
    int compare = -;
        
    word_struct list_word_struct NULL // Lista vuota di tipo word_struct
        
    word_struct temp_word_struct// Node temporaneo di tipo word_struct
        
        
        
    a=fgetc(stdin);
        
        for (; 
    a!=EOF a=fgetc(stdin) ){
            
            
    ///////////////////////////////////////////////////////////////// parole nel buff
        
            
    for (i=a!= ' ' && a!= '\n' && a!= '\t' i++){
                
                
    buff[i]=a;
                
    a=fgetc(stdin);
                
            }
            
    buff[i]=0;
            
            
    convert_lower_case (buff,i);  

            if (
    buff[0] != 0){
                
                for(
    temp_word_struct list_word_structtemp_word_struct!=NULL temp_word_struct=temp_word_struct->next){
                    
                    
    compare strcmp(buff,temp_word_struct-> word);
                    
                    if (
    compare == 0){
                        
                        
    temp_word_struct->counter +=1;
                        
                        break;
                        
                    }
                    
                }
            
                if ( 
    compare != 0) {
                    
                    
    temp_word_struct new_word_struct (buff,1); // nuovo elemento di tipo word_struct
                
                    
    list_word_struct add_to_word_struct_list (temp_word_structlist_word_struct ); // aggiunta di un elemento word_struct a lista word_struct
                
                
    }
            }
        
        }
        
        
        
    ///////////////////////////////////////////////////////////////////////////// stampa parole e numero inserimenti
        
        
    printf ("\nParole Inserite e ripetizioni :\n");
        
        for (
    temp_word_struct list_word_struct temp_word_struct != NULLtemp_word_struct = (*temp_word_struct).next){
            
            
    printf("%s , %u\n",(*temp_word_struct).word,(*temp_word_struct).counter);
        }
        
     return 
    0;  
    }





    ///////////////////////////////////////////////////////////////////////////////// functions



    /////////////////////////////////////////////////////////////////////convert to lowercase
    void convert_lower_case (char bufferint k){
        
        
    int i;
        
        for (
    i=0;i<k;i++){
            
            if (
    buffer[i] >= 'A' && buffer[i] <= 'Z'){
                
                
    buffer[i] = buffer[i] + ('a'-'A');
            }
            
        }
        
    }



    //////////////////////////////////////////////////////////////////// new node
    word_struct new_word_struct char input_word unsigned int input_counter ){
        
        
    word_struct pointer;
        
    pointer = ( word_struct * ) malloc sizeof(word_struct));
        
        (*
    pointer).word strdup(input_word);
        (*
    pointer).counter input_counter;
        (*
    pointer).next NULL;
        
        
        return 
    pointer;
        
    }

    ///////////////////////////////////////////////////////////////////////////////// insert node
    word_struct add_to_word_struct_list (word_struct nodeword_struct * list ){
        
          
        
    //////////////////////////////////// I caso : Lista Nulla
        
    if (list == NULL){
            return 
    node;
        }
        
        
        
    //////////////////////////////////// II caso : Inserimento in testa
        
        
    if (strcmp((*node).word,(*list).word) <0){
            
            (*
    node).next = list ;
                return 
    node;
        }    
      
        
    //////////////////////////////////////III caso Inserimento nel mezzo
      
        
    (*list).next add_to_word_struct_list (node, (*list).next );
        return list;
        
      

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

    Comment


    • #3
      Algoritmo Senza Malloc Preventivo ( NON RICORSIVO )

      PHP Code:
      /*
       *
       *
       * input:parole
       * output: lista ordinata per nome senza ripetizioni + conteggio ripetizioni
       * rev 3 - creazione nodo solo se stringa non presente ( non ricorsivo)
       *
       */


      #include <st***.h>
      #include <stdlib.h>
      #include <string.h>

      #define WORD_SIZE 100

      typedef struct string {
          
          
      char word;
          
      unsigned int counter;
          
      struct string next;
          
      word_struct;


      word_struct new_word_struct char input_word unsigned int input_counter );
      word_struct add_to_word_struct_list (word_struct nodeword_struct * list );
      void convert_lower_case (char bufferint k);

      //////////////////////////////////////////////////////////////////////////////////////////////////begin

      int main (void ) {
          
          
      char a;
          
      char buff[WORD_SIZE]; // buf temporaneo per le parole da input
          
      int i;
          
      int compare = -;
          
      word_struct list_word_struct NULL // Lista vuota di tipo word_struct
          
      word_struct temp_word_struct// Node temporaneo di tipo word_struct
          
          
          
      a=fgetc(stdin);
          
          for (; 
      a!=EOF a=fgetc(stdin) ){
              
              
      ///////////////////////////////////////////////////////////////// parole nel buff
          
              
      for (i=a!= ' ' && a!= '\n' && a!= '\t' i++){
                  
                  
      buff[i]=a;
                  
      a=fgetc(stdin);
                  
              }
              
      buff[i]=0;
              
              
      convert_lower_case (buff,i);  

              if (
      buff[0] != 0){
                  
                  for(
      temp_word_struct list_word_structtemp_word_struct!=NULL temp_word_struct=temp_word_struct->next){
                      
                      
      compare strcmp(buff,temp_word_struct-> word);
                      
                      if (
      compare == 0){
                          
                          
      temp_word_struct->counter +=1;
                          
                          break;
                          
                      }
                      
                  }
              
                  if ( 
      compare != 0) {
                      
                      
      temp_word_struct new_word_struct (buff,1); // nuovo elemento di tipo word_struct
                  
                      
      list_word_struct add_to_word_struct_list (temp_word_structlist_word_struct ); // aggiunta di un elemento word_struct a lista word_struct
                  
                  
      }
              }
          
          }
          
          
          
      ///////////////////////////////////////////////////////////////////////////// stampa parole e numero inserimenti
          
          
      printf ("\nParole Inserite e ripetizioni :\n");
          
          for (
      temp_word_struct list_word_struct temp_word_struct != NULLtemp_word_struct = (*temp_word_struct).next){
              
              
      printf("%s , %u\n",(*temp_word_struct).word,(*temp_word_struct).counter);
          }
          
       return 
      0;  
      }





      ///////////////////////////////////////////////////////////////////////////////// functions



      /////////////////////////////////////////////////////////////////////convert to lowercase
      void convert_lower_case (char bufferint k){
          
          
      int i;
          
          for (
      i=0;i<k;i++){
              
              if (
      buffer[i] >= 'A' && buffer[i] <= 'Z'){
                  
                  
      buffer[i] = buffer[i] + ('a'-'A');
              }
              
          }
          
      }



      //////////////////////////////////////////////////////////////////// new node
      word_struct new_word_struct char input_word unsigned int input_counter ){
          
          
      word_struct pointer;
          
      pointer = ( word_struct * ) malloc sizeof(word_struct));
          
          (*
      pointer).word strdup(input_word);
          (*
      pointer).counter input_counter;
          (*
      pointer).next NULL;
          
          
          return 
      pointer;
          
      }

      ///////////////////////////////////////////////////////////////////////////////// insert node
      word_struct add_to_word_struct_list (word_struct nodeword_struct * list ){
          
          
      word_struct *new_node;
          
      word_struct *old_node;
          
            
          
      //////////////////////////////////// I caso : Lista Nulla
          
      if (list == NULL){
              return 
      node;
          }
          
          
          
      //////////////////////////////////// II caso : Inserimento in testa
          
          
      if (strcmp((*node).word,(*list).word) <0){
              
              (*
      node).next = list ;
                  return 
      node;
          }    
        
          
      //////////////////////////////////////III caso Inserimento nel mezzo
        
          
          
      for (old_node = list, new_node = list->next new_node != NULL old_node new_node,new_node new_node->next){
              
                 if (
      strcmp((*node).word,(*new_node).word) <0){
              
                   (*
      node).next new_node ;
                   (*
      old_node).next node ;
                   return list;
                  
                  }
              
          }
          
          
      old_node->next node;
          return list;
            
        

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

      Comment

      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