Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <math.h>
- #include <io.h>
- #include <stdio.h>
- using namespace std;
- struct car
- {
- char name[100];
- int speed;
- int year;
- };
- void generate_arr(FILE *, int, char*);
- void h_add(car, int, car *, int *);
- int h_seach(int, int, car *, int *);
- int main()
- {
- FILE *fl;
- char * fname = "lab17.txt";
- int n, i, m,k;
- cout << "Enter hash" << endl;
- cin >> m;
- cout << "Enter amount of cars:";
- cin >> n;
- if (n > m)
- {
- cout << "hash should not be more than the amount of cars" << endl;
- system("pause");
- exit(1);
- }
- if ((fl = fopen(fname, "rb")) == NULL)
- {
- generate_arr(fl, m, fname);
- }
- k = filelength(fileno(fl)) / sizeof(int);
- int *arr = new int[k];
- fread(arr, sizeof(int), k, fl);
- car *Hash = new car[m];
- car *rac = new car[n];
- for (i = 0; i < n; i++)
- {
- cout << "Enter name of car: ";
- cin >> rac[i].name;
- cout << "Enter speed: ";
- cin >> rac[i].speed;
- cout << "Enter year: ";
- cin >> rac[i].speed;
- cout << endl;
- }
- for (i = 0; i < m; i++) Hash[i].year = -1;
- for (i = 0; i < n; i++) h_add(rac[i], m, Hash,arr);
- cout << endl;
- for (i = 0; i < m; i++) cout << "Hash[" << i << "].year " << Hash[i].year << endl;
- int key, x;
- cout << "Enter key" << endl;
- cin >> key;
- x = h_seach(key, m, Hash,arr);
- if (x == -1)cout << "No element" << endl;
- else
- {
- cout << "elment x=" << x << endl;
- cout << "name of car: " << Hash[x].name << endl;
- cout << "speed: " << Hash[x].speed << endl;
- cout << "year: " << Hash[x].year << endl;
- }
- delete[]arr;
- delete[]rac;
- delete[]Hash;
- fclose(fl);
- system("pause");
- return 0;
- }
- void h_add(car rac, int m, car *Hash, int *arr)
- {
- int i = rac.year % m;
- int p = 0;
- if (Hash[i].year != -1)
- {
- while (Hash[i].year != -1)
- {
- p++;
- i += arr[p];
- if (i >= m)
- {
- i = 0;
- break;
- }
- }
- while (Hash[i].year != -1) i++;
- }
- Hash[i] = rac;
- }
- int h_seach(int inf, int m, car *Hash, int *arr)
- {
- int i = inf % m;
- int p = 0;
- if (Hash[i].year == inf) return i;
- while (Hash[i].year != -1)
- {
- p++;
- i += arr[p];
- if (i >= m)
- {
- i = 0; break;
- }
- if (Hash[i].year == inf) return i;
- }
- while (Hash[i].year != -1)
- {
- if (Hash[i].year == inf) return i;
- i++;
- }
- return -1;
- }
- void generate_arr(FILE *fl, int m, char*fname)
- {
- int r;
- srand(time_t(NULL));
- if ((fl = fopen(fname, "wb")) == NULL)
- {
- cout << "error";
- system("pause");
- exit(1);
- }
- for (int i = 0; i < m; i++)
- {
- r= rand() % 5;
- fwrite(&r, sizeof(r), 1, fl);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement