Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using std::cout;
- using std::endl;
- struct Order {
- unsigned int part_id;
- double total_cost;
- char type; // 'c' -- cpu, 'g' -- gpu, 'm' -- motherboard, 'r' -- ram;
- int count;
- };
- void process_orders(const Order* all_orders, unsigned int old_n, Order** processed_orders, unsigned int* new_n) {
- *processed_orders = new Order [old_n];
- uint32_t ind = 0, i = 0;
- while (ind < old_n) {
- if (all_orders[i].type != 'g') {
- for (uint32_t j = 0; j < all_orders[i].count; ++j) {
- processed_orders[ind]->total_cost = all_orders[i].total_cost / all_orders[i].count;
- processed_orders[ind]->count = 1;
- processed_orders[ind]->part_id = all_orders[i].part_id;
- processed_orders[ind]->type = all_orders[i].type;
- ++ind;
- }
- }
- ++i;
- }
- }
- int main()
- {
- Order orders[] = {
- {12270, 500.0, 'r', 4},
- {15644, 1220.0, 'g', 2},
- {2330, 700.0, 'c', 1},
- };
- Order* processed_orders = NULL;
- unsigned int new_n = 0;
- process_orders(orders, 5, &processed_orders, &new_n);
- for (unsigned int i = 0; i < new_n; i++)
- cout << processed_orders[i].part_id << " " << processed_orders[i].total_cost
- << " " << processed_orders[i].type << " " << processed_orders[i].count << endl;
- // ожидаемый результат:
- // 12270 125.0 r 1
- // 12270 125.0 r 1
- // 12270 125.0 r 1
- // 12270 125.0 r 1
- // 2330 700.0 c 1
- delete[] processed_orders;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement