This is the mail archive of the cygwin@sources.redhat.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re:


i have problem the txt doesn't seem to be link with
the program can you help me
--- henry junior <gs01han@yahoo.com> wrote:
> * Talib Talib   *
>  * Assignment: 4 */
> 
> #include <iostream.h>
> #include <fstream.h>
> 
> #define EMPTY  0
> #define ACTIVE  1
> #define DELETED  2
> #define ITEM_NOT_FOUND -1 
> 
> typedef struct {
>     char name[20];
>     int ssn, mid, final;
>     char grade;
>     int info;
> } Entry;
>     
> class HashTable{
> 
>     Entry *array;
>     int tableSize;
>     int currentSize;
>     public:
>         HashTable(int size){
>             array = new Entry[size]; tableSize =
> size;
> makeEmpty();
>         }
> 
>         int insert(Entry x){
>                 // Insert x as active
>             int currentPos = findPos( x );
>             if( isActive( currentPos ) )
>                 return 1;
>             array[ currentPos ] = x;
> 
>                 // Rehash; see Section 5.5
>             if( ++currentSize > tableSize / 2 )
>                 rehash( );
> 			return 0;
>         }
>         
>         bool isActive( int currentPos )
>         {
>             return array[ currentPos ].info ==
> ACTIVE;
>         }
> 
>         int hash( Entry key, int tableSize )
>         {
>             int hashVal = 0;
> 
>             for( int i = 0; key.name[i] != '\0'; i++
> )
>                 hashVal = 37 * hashVal + key.name[ i
> ];
> 
>             hashVal %= tableSize;
>             if( hashVal < 0 )
>                 hashVal += tableSize;
>             return hashVal;
>         }
>     
>         void rehash( ) {
> 			int i;
>             Entry *oldArray = new Entry[tableSize];
>             for(i=0; i<tableSize; i++)
>                 oldArray[i] = array[i];
> 
>             // Create new double-sized, empty table
>             array = new Entry[2*tableSize];
>             for( i = 0; i < tableSize; i++ )
>                 array[ i ].info = EMPTY;
> 
>             // Copy table over
>             currentSize = 0;
>             for( i = 0; i < tableSize; i++ )
>                 if( oldArray[ i ].info == ACTIVE )
>                     insert( oldArray[ i ] );
>             tableSize *= 2;
>         }
>         
>         int findPos( const Entry & x ) 
>         {
> /* 1*/      int collisionNum = 0;
> /* 2*/      int currentPos = hash( x, tableSize );
> 
> /* 3*/      while( array[ currentPos ].info != EMPTY
> &&
>                    array[ currentPos ].ssn != x.ssn
> )
>             {
> /* 4*/          currentPos += 2 * ++collisionNum -
> 1; 
> // Compute ith probe
> /* 5*/          if( currentPos >= tableSize )
> /* 6*/              currentPos -= tableSize;
>             }
> 
> /* 7*/      return currentPos;
>         }
>         
>         void remove( const Entry & x )
>         {
>             int currentPos = findPos( x );
>             if( isActive( currentPos ) )
>                 array[ currentPos ].info = DELETED;
>         }
>         
>         int find( const Entry & x ) 
>         {
>             int currentPos = findPos( x );
>             return isActive( currentPos ) ?
> currentPos
> : ITEM_NOT_FOUND;
>         }
> 
>         void makeEmpty( )
>         {
>             currentSize = 0;
>             for( int i = 0; i < tableSize; i++ )
>                 array[ i ].info = EMPTY;
>         }
> 
> 	int printEntry(int index){
> 		if(index >= tableSize) 
> 			return -1;
> 		cout << array[index].name << " " <<
> array[index].ssn
> << " " << array[index].mid << " " <<
> array[index].final << " " << array[index].grade <<
> "\n";
> 		return 1;
> 	}
> };    
> 
> int main(int argc, char *argv[]){
>     int n, i, index;
>     Entry tmp;
>     ifstream fin("input.txt");
>     fin >> n;
>     HashTable table = HashTable(2*n+1);
>     for(i=0; i<n; i++){
>         fin >> tmp.name;
>         fin >> tmp.ssn;
>         fin >> tmp.mid;
>         fin >> tmp.final;
>         fin >> tmp.grade;
> 		tmp.info = ACTIVE;
>         table.insert(tmp);
>     }
> 
>     cout << "Give the entry to be found in
> hashtable(Give name and ssn)\n";
>     cin >> tmp.name;
>     cin >> tmp.ssn;
>     index = table.find(tmp);
>     if(index == ITEM_NOT_FOUND) 
>         cout << "Item not found\n";
>     else{
>         cout << "Index of given name is: " << index
> <<
> "\n";
> 	table.printEntry(index);
>     }
>     return 1;
> }    
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Thousands of Stores.  Millions of Products.  All in
> one Place.
> http://shopping.yahoo.com/


__________________________________________________
Do You Yahoo!?
Yahoo! Shopping - Thousands of Stores. Millions of Products.
http://shopping.yahoo.com/

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]