Home Forums C Programming SOS!!!

Viewing 4 reply threads
  • Author
    Posts
    • #2127
      GeoffreyBeeler
      Participant

      pls help me…
      i don’t know what to do…
      who can send source code…
       
      Phone book sorter
      The objective is to create an application pbsorter that sorts phonebook entries in ascending order. A phonebook entry is made of a word and a number separated by a comma. Each entry is separated by a newline. An example of unsorted phonebook entries:

      Villar,90912345
      Arroyo,91855661
      Lacson,91681233

      pbsorter can be executed in 2 ways. Suppose the above entries are stored in a file pbook.csv, if we want to sort by name:
      ./pbsorter -a pbook.csv
      The new pbook.csv entries will be:

      Arroyo,91855661
      Lacson,91681233
      Villar,90912345

      If we want to sort by number:
      ./pbsorter -n pbook.csv
      The new pbook.csv entries will be:

      Villar,90912345
      Lacson,91681233
      Arroyo,91855661

      Use getopt to parse the option. Use available sorting functions in the library to perform sorting.

    • #3422
      Humayan
      Participant

      What type of data structure are you supposed to enter the items into ( a linked list , array ) ? What type of sort are you to use?

    • #3423
      GeoffreyBeeler
      Participant

      linked list, any kind of sorting but preferrably binary search tree…

      can you do it asap,,? i need this tomorrow….

      if only….

      thanks..
    • #3424
      Humayan
      Participant

      I have this binary seach tree ( hope this helps ) :
       

      /*
      * Název programu:                ADT Binarni strom
      * Autor:                        Petr Bobek
      * Studijní skupina:                IT-01
      * Email autora:                    [email protected]
      * Datum vypracování:            26.10.2005
      * Stručný popis řeÅ¡ení:            Je uveden dale v programu
      */
      #include "iostream"
      #include "stdio.h"
      #include "conio.h"
      #include "malloc.h"
      #include "process.h"
      #include "stdlib.h"
      #include "string.h" //standardní knihovny
      using namespace std;
      typedef char tStr;
      struct uzel
      {
         int item;
         struct uzel *right;
         struct uzel *left;
      };
      typedef struct uzel * BST;
      typedef struct uzel uzel;
      bool Empty ( BST t )
      {
         if( t != NULL )
             return false;
         else
          return true;
      }
      void Initialize ( BST * t )
      {
         * t = NULL;
        
      }
      void DeleteTree ( BST t )
      {
         if ( t != NULL )
         {
             DeleteTree ( t-> left );
             DeleteTree ( t->right );
             free ( t );
         }
      }
      BST Search ( BST t, int x )
      {
         if( t == NULL )
         {
             cout << "item was not found!" << endl;
             return NULL;
         }
         if( x < t -> item )
         {
             return Search(  t -> left  , x );
         }
         else if( x > t -> item )
             return Search (  t -> right  , x );
         else
             return t;
        
      }
      BST Insert ( BST * t, int x )
      {
         if( * t == NULL )
         {
             * t = (struct uzel * ) malloc ( sizeof ( struct uzel ) );
             (* t ) -> item = x;
             ( * t ) -> left = ( * t ) -> right = NULL;
             return * t;
         }

         if( ( *t ) -> item > x)
         Insert (  & ( ( * t ) -> left  )  , x );
         else
         Insert (  & ( ( * t ) -> right  ) , x );
           
        
      }
      BST findmin ( BST t )
      {
       
       while ( t -> left )
        t = t -> left;
       return t;
       
      }
      BST findmax ( BST t )
      {
         while ( t -> right )
         t = t -> right;
         return t;
        
      }
      void removeMin ( BST * t )
      {
          while ( ( * t ) -> left )
        ( * t ) = ( * t ) -> left;
       
       BST  temp =  * t;
        * t  =   ( ( * t ) -> right );
       free (  temp );

      }
      //THIS DOESN'T LOOK RIGHT
      BST Delete ( BST * t, int x )
      {
         BST pom_bunka;
         if( t == NULL )
         {
             cout << " Cannot delete, bin. tree is empty !"<< endl;
             return NULL;
         }
         if ( x < ( * t ) -> item )
            Delete ( & ( ( * t ) -> left ) , x );
         else if ( x > ( * t ) -> item )
            Delete ( & ( ( * t ) -> right ) , x );   
         else if ( ( * t ) -> right != NULL && ( * t ) -> left  != NULL )
         {
             BST temp = findmin ( ( * t ) -> right );
             ( * t ) -> item = temp -> item;
             removeMin ( & (( * t ) -> right ) );
         }
         else
         {
          BST  temp =   * t ;
          if ( ( * t ) -> left != NULL )
         * t = ( * t ) -> left;
          else
           * t = ( * t ) -> right;
          free ( temp  );
       }
      }
      void Preorder ( BST t )
      {
         if ( t != NULL )
         {
             cout << t -> item << endl;
             Preorder ( t -> left );
             Preorder ( t -> right ) ;
         }
       
        
      }
      void Inorder ( BST t )
      {
         if ( t != NULL )
         {
             Inorder ( t -> left );
             cout << t -> item << endl;
             Inorder ( t -> right );
         }
      }
      void Postorder ( BST t )
      {
         if ( t != NULL )
         {
             Postorder ( t -> left );
             Postorder ( t -> right );
             cout << t -> item << endl;
         }
      }
      int getData ( void )
      {
         
          cout << "Get data: ";
          int data;
          cin >> data;
          return data;
      }
      /*
      void VypisSeznam(BST t){
          tStr data;
          cout << "Obsah seznamu:";
          if (Empty(t)==true){
             cout<<" NULL"<     }
          else{
             data = t->item;
             cout << data << ", ";
          }
      }
      */
      void Menu ( void )//vypis menu
      {
          cout << "nGet number 0-7: nn";
          cout << "0: Inicializen";
          cout << "1: Insertn";
          cout << "2: Searchn";
          cout << "3: Preordern";
          cout << "4: Inordern";
          cout << "5: Postordern";
          cout << "6: Deleten";
          cout << "7: Delete treen";
          cout << "M: Menun";
          cout << "E: Endn";
      }
      int main()
      {
        
         char znak;
         BST strom = 0 ; //root ?
         int data;
         //tStr data;
         do {
             Menu();
          cout << "nnEnter Choice ! >> ";
             cin >> znak;
             switch ( toupper ( znak ) )
             {
                    case '0' :  cout << "Inicialize - inicializuje stromn";
                                Initialize ( & strom );
                                //VypisSeznam(strom);
                         break;
                    case '1' :  cout << "Insert - vlozi do stromu novy uzeln";
                                cout << "Get data: ";
              cin >> data;
                                Insert( & strom , data );
                                //VypisSeznam(strom);
                         break;
                    case '2' :  cout << "Search - vyhleda uzeln";
                                Search(strom, getData());
                                cout<                    break;
                    case '3' :  cout << "Preorder - projde strom pruchodem preordern";
                                Preorder(strom);
                         break;
                    case '4' :  cout << "Inorder - projde strom pruchodem inordern";
                                Inorder(strom);
                         break;
                    case '5' :  cout << "Postorder - projde strom pruchodem postordern";
                                Postorder(strom);
                         break;
                    case '6' :  cout << "Delete - smaze ze stromu uzeln";
                                Delete( & strom, getData());
                         break;
                    case '7' :  cout << "Delete Tree - smaze cely stromn";
                                DeleteTree(strom);
                         break;
                    case 'M' : Menu();
                         break;
                    case 'E' :
                         break;
                    default : cout << "Zadali jste nespravny znak!!!n";
             }
         } while (toupper(znak) != 'E');
         return 0;
      }
    • #3425
      GeoffreyBeeler
      Participant

      thanks..
      that helped a lot…
      hoping for replies again next time…
      thank you very much….

Viewing 4 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.