srtgguy

Untitled

Apr 12th, 2021
510
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4.  
  5. struct Order {
  6.     unsigned int part_id;
  7.     double total_cost;
  8.     char type; // 'c' -- cpu, 'g' -- gpu, 'm' -- motherboard, 'r' -- ram;
  9.     int count;
  10.  
  11. };
  12. void process_orders(const Order* all_orders, unsigned int old_n, Order** processed_orders, unsigned int* new_n) {
  13.     *processed_orders = new Order [old_n];
  14.     uint32_t ind = 0, i = 0;
  15.     while (ind < old_n) {
  16.         if (all_orders[i].type != 'g') {
  17.             for (uint32_t j = 0; j < all_orders[i].count; ++j) {
  18.                 processed_orders[ind]->total_cost = all_orders[i].total_cost / all_orders[i].count;
  19.                 processed_orders[ind]->count = 1;
  20.                 processed_orders[ind]->part_id = all_orders[i].part_id;
  21.                 processed_orders[ind]->type = all_orders[i].type;
  22.                 ++ind;
  23.             }
  24.         }
  25.         ++i;
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31.  
  32.  
  33.     Order orders[] = {
  34.   {12270,  500.0, 'r', 4},
  35.   {15644,  1220.0, 'g', 2},
  36.   {2330,  700.0, 'c', 1},
  37.     };
  38.  
  39.     Order* processed_orders = NULL;
  40.     unsigned int new_n = 0;
  41.     process_orders(orders, 5, &processed_orders, &new_n);
  42.  
  43.     for (unsigned int i = 0; i < new_n; i++)
  44.         cout << processed_orders[i].part_id << " " << processed_orders[i].total_cost
  45.         << " " << processed_orders[i].type << " " << processed_orders[i].count << endl;
  46.  
  47.     // ожидаемый результат:
  48.     // 12270 125.0 r 1
  49.     // 12270 125.0 r 1
  50.     // 12270 125.0 r 1
  51.     // 12270 125.0 r 1
  52.     // 2330 700.0 c 1
  53.  
  54.     delete[] processed_orders;
  55.     return 0;
  56. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×