Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <cstdlib>
- #include <string.h>
- #include <cmath>
- using namespace std;
- const int N = 157;
- string T[N];
- int licz = 0;
- int hash(char* slowo)
- {
- int sumaModulo = 0;
- for(int i=0; i<strlen(slowo) ;i++)
- {
- sumaModulo += pow(2,i) * slowo[i];
- }
- return sumaModulo % N;
- } //funkcja haszująca
- char* init(){ //generuje losowy ciąg n-znakowy
- int n =rand()%9+1;
- int i;
- char *t = new char[n+1];
- for(i=0;i<n;i++)
- t[i] = (char)(rand()%28 + 65);
- t[i]='\0';
- return t;
- }
- void dodaj(char* slowo)
- {
- int index = hash(slowo);
- if(T[index]== "")
- {
- T[index] = slowo;
- licz++;
- }
- else
- {
- for(int i = 1; i<N ; i++)
- {
- if(index + i < N)
- {
- if(T[index+i] == "")
- {
- T[index+i]=slowo;
- break;
- }
- }
- else
- {
- for(int x=0; x<index; x++)
- if(T[x]=="")
- {
- T[x]=slowo;
- break;
- }
- break;
- }
- }
- }
- }
- void drukuj()
- {
- for(int i = 0; i< N ; i++)
- {
- cout <<"T["<<i<<"] = "<<T[i]<<endl;
- }
- cout << "Poprawne trafienia za pierwszym razem: "<<licz<<endl;
- cout << endl;
- } //wydruk całej tablicy (pozycja + słowo)
- int main(int argc, char *argv[])
- {
- srand(time(NULL));
- for(int i =0; i< N;i++)
- {
- char *s = init();
- dodaj(s);
- }
- drukuj();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement