Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <time.h>
- #include <string>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- string names[10] = {
- "oil", "gas", "metal", "wheat", "cars",
- "chemicals", "cloth", "meat", "material", "food"
- };
- string list[5] = {"Russia", "Ukraine", "USA", "Germany", "Kanada"};
- struct product {
- string country;
- string name;
- int count;
- };
- void gen(struct product* elem) {
- int a = rand() % 5, b = rand() % 10, c = rand() % 10000 + 1;
- elem->country = list[a];
- elem->name = names[b];
- elem->count = c;
- }
- void mysort(struct product* arr, int len) {
- for (int i = 0; i < len; ++ i) {
- for (int j = i; j > 0; -- j)
- if (arr[j].country < arr[j - 1].country)
- swap(arr[j], arr[j - 1]);
- }
- }
- string* append(string *arr, int &len, string elem) {
- string *old = arr;
- arr = new string[len + 1];
- for (int i = 0; i < len; ++ i)
- arr[i] = old[i];
- arr[len] = elem;
- ++ len;
- delete [] old;
- return arr;
- }
- bool find_elem(string *arr, int len, string elem) {
- bool ret = false;
- for (int i = 0; i < len; ++ i)
- if (arr[i] == elem)
- ret = true;
- return ret;
- }
- const int N = 20;
- int main(void) {
- srand(time(NULL));
- struct product arr[N];
- for (int i = 0; i < N; ++ i)
- gen(&(arr[i]));
- mysort(arr, N);
- cout << "old:\n";
- for (int i = 0; i < N; ++ i)
- cout << arr[i].country << ", " << arr[i].name << ", " << arr[i].count << "\n";
- cout << "\n";
- for (int i = 0; i < N; ++ i) {
- string a = arr[i].country;
- string *prods = new string[0];
- int len = 0;
- int j = i;
- for (; j < N && arr[j].country == a; ++ j) {
- if (!find_elem(prods, len, arr[j].name))
- prods = append(prods, len, arr[j].name);
- }
- if (len > 5) {
- for (int k = i; k < j; ++ k) {
- arr[k].count += arr[k].count / 10;
- }
- }
- cout << a << ":";
- for (int i = 0; i < len; ++ i)
- cout << " " << prods[i];
- cout << endl;
- delete [] prods;
- i = j - 1;
- }
- cout << "\n\n\nnew:\n";
- for (int i = 0; i < N; ++ i)
- cout << arr[i].country << ", " << arr[i].name << ", " << arr[i].count << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement