int main()
{
    Hashtable hs;
    int k;
    int a[]={12,67,56,16,25,37,22,29,15,47,48,34};
    for(int i=0;i!=12;i++)
        hs.Insert(a[i]);
        hs.Show();
        k=28;
    cout<<"search for "<<k<<":"<<endl;
    cout<<hs.Search(k)<<endl;
    return 0;
}
/* task3 */
void task3 ( LinkedList<Produs>& listaProduse, LinkedList<Palet>& listaPaleti ){
	/* nrProduse = numarul de produse din listaProduse / citite din fisier */
	Node<Produs> *numarare = listaProduse.front();
	int nrProduse = 0;

	while( numarare != NULL ){
		numarare = numarare->getNext();
		nrProduse++;
	}
	
	/* hashmap - hashtable cu nrProduse bucketuri, avand cheia de tip int si valoarea de tip Pereche (simuleaza un vector de frecventa, de fapt)*/
	Hashtable<int,Pereche> hashmap = Hashtable<int,Pereche>( nrProduse );
	Hashtable<int,int> hashCantitati = Hashtable<int,int>( nrProduse );

	/* slots - vector de NR_SLOTS LinkedList-uri */
	//ArrayList<Palet> slots = ArrayList<Palet>( NR_SLOTS );

	/* contorIndex - retine indexul curent al fiecarui Slot */
	int contorIndex [ NR_SLOTS + 1 ] = { 0 };
	
	/* parcurgere - pointer la inceputul listei de paleti */
	Node<Palet> *parcurgere = listaPaleti.front();
	/* pair - structura definita in Hashtable.h */
	Pereche pair;

	/* Se parcurge lista de paleti, atat timp cat exista elemente */
	while( parcurgere != NULL ) {

		/* pair.idSlot - Retine valoarea idSlot-ului elementului curent din lista */
		pair.idSlot = parcurgere->getValue().getIdSlot();
		/* Se insereaza in vectorul slots, la indexul pair.idSlot, elementul din lista curenta */ 
		//slots.push( pair.idSlot, parcurgere->getValue() );
		/* Se incrementeaza indexul curent aferent slotului pe care se lucreaza acum, semn ca s-a mai introdus un palet in slotul curent */
		contorIndex[ pair.idSlot ]++; 
		/* Se retine si in elementul curent, indexul la care a fost inserat */
		pair.indexSlot = contorIndex[ pair.idSlot ];
		/* Se insereaza in hashtable, in bucketul "parcurgere->getValue().getProdType()", elementul "pair" */
		hashCantitati.Insert( parcurgere->getValue().getProdType(), parcurgere->getValue().getNumberOfItems() );
		hashmap.Insert( parcurgere->getValue().getProdType(), pair );
		/* Mutam pointerul asupra elementului urmator din lista */
		parcurgere = parcurgere->getNext();
	}

/*	
	task3_1( listaProduse, hashmap, "bicycle" );

	task3_2( listaProduse, hashmap, contorIndex, "bicycle" );
	hashCantitati.printTable();
int macarena;
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
		hashCantitati.getLast( 57, macarena );
	hashCantitati.getLast( 57, macarena );
	hashCantitati.printTable();
	hashmap.printTable();
*/

	/* De aicisa am adaugat eu. :-s */
	LinkedList<Tranzactie> listaTranzactii;
	citireTranzactii( listaTranzactii );	

	LinkedList<Bon> listaBonuri;
	citireBonuri( listaBonuri );

	task3_3( listaTranzactii, listaBonuri, nrProduse, hashCantitati, listaProduse );

}
Beispiel #3
0
/**********************************************************************
                            main
***********************************************************************
Author: Katherine MacMillan
***********************************************************************
The main function creates an instance of the hashtable class, and then
uses the second commannd line argument to open a text file.
Once the file has been verified as opened, main reads in all of the
lines in the file, converting them to lowercase, and then parsing it
into individual word tokens.

The word tokens are stored in a vector of strings, and then as they are
inserted into the hashtable, they are counted to keep track of the
total number of words read in.

After all of the words are inserted into the hashtable all of the 
entries in the hashtable are copied into an array of integer-string
pairs. This array is then sorted, first by integer value and then
alphabetically.

After the list is sorted two files, a .wrd and a .cvs file are written
outputting the words and their frequency count.

The main function times the process of reading in, hashing, sorting,
and outputting the data. This time is displayed to the user before 
the program exits.

A return of 0 indicates that the program ran correctly.
A return of 1 indicates that an incorrect number of commandline 
	arguments was entered by the user.
A return of 2 indicates that the file indicated by the user could
	not be opened.
**********************************************************************/
int main( int argc, char** argv )
{

	Hashtable table;
	vector<string> tokens;
	string temp;

	// track longest string count and total word count
    int maxStrLen = 0;
    int wordCount = 0;

    // must be two arguments
    if ( argc != 2 )
    {
		cout << "Wrong number of arguments\nUsage: zipf <file.txt>" << endl;
		return 1;
    }

	// make sure file opens
    ifstream infile( argv[1] );
    if ( !infile )
	{
		cout << "Unable to open the file " << argv[1] << endl;
		return 2;
	}

	// time process
    clock_t t = clock();

	while ( !infile.eof() )
	{
		// read in line, make all characters lowercase and tokenize into words
		getline( infile, temp );
		transform( temp.begin(), temp.end(), temp.begin(), ::tolower );
		Tokenize( temp, tokens, VALID );
	}

	for ( auto t: tokens ) 
	{
		// insert each token, count total words, and check string length
		table.Insert( t );
		wordCount++;
		if ( t.length() > (unsigned) maxStrLen )
			maxStrLen = t.length();			
	}

	// retrieve total entry count and table size
	int count = table.GetEntryCount();
	int size = table.GetSize();

    // copy table data to array of  pairs
	tableEntry* wordList = new pair<int, string> [count+1];
    int j = 0;
    for ( int i = 0; i < size; i++ )
    {
    	int c = table.GetCount( i );
    	if ( c != 0 )
    	{
    		wordList[j].first = c;
    		wordList[j].second = table.GetKey( i );
    		j++;
    	}
    }

    // sort list and write data to file
    qsort( wordList, count, sizeof( tableEntry ), TableComparator );
    WriteFiles( argv[1],wordList, wordCount, maxStrLen, count );

    // display program runtime
    t = clock() - t;
    cout << "Time to read in, hash, sort and write out data: " << (float) t / CLOCKS_PER_SEC << " seconds" << endl;

	return 0;
}