Advertisement
StoneHaos

misha19

Apr 30th, 2020
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. string names[10] = {
  10.     "oil", "gas", "metal", "wheat", "cars",
  11.     "chemicals", "cloth", "meat", "material", "food"
  12. };
  13. string list[5] = {"Russia", "Ukraine", "USA", "Germany", "Kanada"};
  14.  
  15. struct product {
  16.     string country;
  17.     string name;
  18.     int count;
  19. };
  20.  
  21. void gen(struct product* elem) {
  22.     int a = rand() % 5, b = rand() % 10, c = rand() % 10000 + 1;
  23.     elem->country = list[a];
  24.     elem->name = names[b];
  25.     elem->count = c;
  26. }
  27.  
  28. void mysort(struct product* arr, int len) {
  29.     for (int i = 0; i < len; ++ i) {
  30.         for (int j = i; j > 0; -- j)
  31.             if (arr[j].country < arr[j - 1].country)
  32.                 swap(arr[j], arr[j - 1]);
  33.     }
  34. }
  35.  
  36. string* append(string *arr, int &len, string elem) {
  37.     string *old = arr;
  38.     arr = new string[len + 1];
  39.     for (int i = 0; i < len; ++ i)
  40.         arr[i] = old[i];
  41.     arr[len] = elem;
  42.     ++ len;
  43.     delete [] old;
  44.     return arr;
  45. }
  46.  
  47. bool find_elem(string *arr, int len, string elem) {
  48.     bool ret = false;
  49.     for (int i = 0; i < len; ++ i)
  50.         if (arr[i] == elem)
  51.             ret = true;
  52.     return ret;
  53. }
  54.  
  55. const int N = 20;
  56.  
  57. int main(void) {
  58.     srand(time(NULL));
  59.     struct product arr[N];
  60.     for (int i = 0; i < N; ++ i)
  61.         gen(&(arr[i]));
  62.  
  63.     mysort(arr, N);
  64.     cout << "old:\n";
  65.     for (int i = 0; i < N; ++ i)
  66.         cout << arr[i].country << ", " << arr[i].name << ", " << arr[i].count << "\n";
  67.  
  68.     cout << "\n";
  69.  
  70.     for (int i = 0; i < N; ++ i) {
  71.         string a = arr[i].country;
  72.         string *prods = new string[0];
  73.         int len = 0;
  74.         int j = i;
  75.         for (; j < N && arr[j].country == a; ++ j) {
  76.             if (!find_elem(prods, len, arr[j].name))
  77.                 prods = append(prods, len, arr[j].name);
  78.         }
  79.         if (len > 5) {
  80.             for (int k = i; k < j; ++ k) {
  81.                 arr[k].count += arr[k].count / 10;
  82.             }
  83.         }
  84.         cout << a << ":";
  85.         for (int i = 0; i < len; ++ i)
  86.             cout << " " << prods[i];
  87.         cout << endl;
  88.         delete [] prods;
  89.         i = j - 1;
  90.     }
  91.  
  92.     cout << "\n\n\nnew:\n";
  93.     for (int i = 0; i < N; ++ i)
  94.         cout << arr[i].country << ", " << arr[i].name << ", " << arr[i].count << "\n";
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement