Advertisement
palenda21

Lab17B

Jun 7th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <math.h>
  4. #include <io.h>
  5. #include <stdio.h>
  6. using namespace std;
  7. struct car
  8. {
  9.     char name[100];
  10.     int speed;
  11.     int year;
  12. };
  13. void generate_arr(FILE *, int, char*);
  14. void h_add(car, int, car *, int *);
  15. int h_seach(int, int, car *, int *);
  16. int main()
  17. {
  18.     FILE *fl;
  19.     char * fname = "lab17.txt";
  20.     int n, i, m,k;
  21.     cout << "Enter hash" << endl;
  22.     cin >> m;
  23.     cout << "Enter amount of cars:";
  24.     cin >> n;
  25.     if (n > m)
  26.     {
  27.         cout << "hash should not be more than the amount of cars" << endl;
  28.         system("pause");
  29.         exit(1);
  30.     }
  31.     if ((fl = fopen(fname, "rb")) == NULL)
  32.     {
  33.         generate_arr(fl, m, fname);
  34.     }
  35.     k = filelength(fileno(fl)) / sizeof(int);
  36.     int *arr = new int[k];
  37.     fread(arr, sizeof(int), k, fl);
  38.     car *Hash = new car[m];
  39.     car *rac = new car[n];
  40.    
  41.     for (i = 0; i < n; i++)
  42.     {
  43.         cout << "Enter name of car: ";
  44.         cin >> rac[i].name;
  45.         cout << "Enter speed: ";
  46.         cin >> rac[i].speed;
  47.         cout << "Enter year: ";
  48.         cin >> rac[i].speed;
  49.         cout << endl;
  50.     }
  51.     for (i = 0; i < m; i++) Hash[i].year = -1;
  52.     for (i = 0; i < n; i++) h_add(rac[i], m, Hash,arr);
  53.     cout << endl;
  54.     for (i = 0; i < m; i++) cout << "Hash[" << i << "].year " << Hash[i].year << endl;
  55.     int key, x;
  56.     cout << "Enter key" << endl;
  57.     cin >> key;
  58.     x = h_seach(key, m, Hash,arr);
  59.     if (x == -1)cout << "No element" << endl;
  60.     else
  61.     {
  62.         cout << "elment x=" << x << endl;
  63.         cout << "name of car: " << Hash[x].name << endl;
  64.         cout << "speed: " << Hash[x].speed << endl;
  65.         cout << "year: " << Hash[x].year << endl;
  66.     }
  67.     delete[]arr;
  68.     delete[]rac;
  69.     delete[]Hash;
  70.     fclose(fl);
  71.     system("pause");
  72.     return 0;
  73. }
  74. void h_add(car rac, int m, car *Hash, int *arr)
  75. {
  76.     int i = rac.year % m;
  77.     int p = 0;
  78.  
  79.     if (Hash[i].year != -1)
  80.     {
  81.         while (Hash[i].year != -1)
  82.         {
  83.             p++;
  84.             i += arr[p];
  85.             if (i >= m)
  86.             {
  87.                 i = 0;
  88.                 break;
  89.             }
  90.         }
  91.         while (Hash[i].year != -1) i++;
  92.     }
  93.     Hash[i] = rac;
  94. }
  95. int h_seach(int inf, int m, car *Hash, int *arr)
  96. {
  97.     int i = inf % m;
  98.     int p = 0;
  99.     if (Hash[i].year == inf) return i;
  100.     while (Hash[i].year != -1)
  101.     {
  102.         p++;
  103.         i += arr[p];
  104.         if (i >= m)
  105.         {
  106.             i = 0; break;
  107.         }
  108.         if (Hash[i].year == inf) return i;
  109.     }
  110.     while (Hash[i].year != -1)
  111.     {
  112.         if (Hash[i].year == inf) return i;
  113.         i++;
  114.     }
  115.     return -1;
  116. }
  117. void generate_arr(FILE *fl, int m, char*fname)
  118. {
  119.     int r;
  120.     srand(time_t(NULL));
  121.    
  122.     if ((fl = fopen(fname, "wb")) == NULL)
  123.     {
  124.         cout << "error";
  125.         system("pause");
  126.         exit(1);
  127.     }
  128.     for (int i = 0; i < m; i++)
  129.     {
  130.         r= rand() % 5;
  131.         fwrite(&r, sizeof(r), 1, fl);
  132.     }
  133.    
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement