Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- #include<cmath>
- using namespace std;
- struct element
- {
- unsigned int klucz;
- char wartosc[21];
- };
- #define SIZE 5000
- element* tab[SIZE];
- unsigned int hashuj(unsigned int klucz)
- {
- return (5000 * klucz + 64) % 5000;
- }
- void dodaj(int klucz, char wartosc[21])
- {
- unsigned int hash = hashuj(klucz);
- element* obiekt=(element*)malloc(sizeof(element));
- if(tab[hash] == NULL)
- {
- obiekt->klucz = klucz;
- strcpy(obiekt->wartosc, wartosc);
- tab[hash] = obiekt;
- return;
- }else
- {
- for(int i=1; i<50; i++)
- {
- if(tab[hash+(i^2)] == NULL)
- {
- tab[hash+(i^2)] = obiekt;
- return;
- }
- else if(tab[hash-(i^2)] == NULL)
- {
- tab[hash-(i^2)] = obiekt;
- return;
- }
- }
- }
- }
- void szukaj(unsigned int klucz)
- {
- unsigned int hash = hashuj(klucz);
- if(tab[hash] == NULL)
- {
- cout<<"-";
- return;
- }
- if(tab[hash]->klucz == klucz)
- {
- cout<<tab[hash]->wartosc;
- return;
- }
- else
- {
- for(int i=1; i<50; i++)
- {
- if(tab[hash+(i^2)]->klucz == klucz)
- {
- cout<<tab[hash+(i^2)]->wartosc;
- return;
- }else if(tab[hash+(i^2)] == NULL)
- cout<<"-";
- else if(tab[hash-(i^2)]->klucz == klucz)
- {
- cout<<tab[hash-(i^2)]->wartosc;
- return;
- }else if(tab[hash-(i^2)] == NULL)
- cout<<"-";
- }
- }
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("insert.in","r",stdin);
- freopen("out2.txt","w",stdout);
- #endif
- unsigned int klucz;
- char instrukcja = 'a', wartosc[21];
- //struct element obiekt;
- for(int i=0; instrukcja!=EOF; i++)
- {
- cin>>instrukcja;
- if(instrukcja == NULL)
- return 0;
- if(instrukcja == 'I')
- {
- scanf("%d", &klucz);
- scanf("%s", &wartosc);
- dodaj(klucz, wartosc);
- }else if(instrukcja == 'S')
- {
- cin>>klucz;
- szukaj(klucz);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement