Odpowiedź :
Odpowiedź:
#include <iostream>
#include <string>
#include <vector>
struct Product {
std::string name;
int quantity{};
};
struct Received {
std::string person;
Product product;
};
void addToList(std::vector<Product> &list) {
std::string name;
int quantity;
std::cout << "Podaj nazwe produktu\n";
std::cin >> name;
std::cout << "Podaj ilosc " << name << std::endl;
std::cin >> quantity;
Product product = {name, quantity};
list.push_back(product);
std::cout << "Produkt zostal dodany.\n"
<< "Wcisnij 1, aby dodac kolejny produkt, 2 aby przejsc do podsumowania, lub 0, aby zakonczyc program\n";
}
void sumUp(std::vector<Product> &list) {
std::vector<Received> products;
Received locator1;
Received locator2;
Received host;
for (const auto &product: list) {
locator1.person = "Locator1";
locator1.product.quantity = product.quantity / 2;
locator1.product.name = product.name;
locator2.person = "Locator2";
locator2.product.quantity = product.quantity / 2;
locator2.product.name = product.name;
host.person = "Host";
host.product.quantity = product.quantity % 2;
host.product.name = product.name;
products.push_back(locator1);
products.push_back(locator2);
products.push_back(host);
}
for (const auto &product: products) {
std::cout << product.person << " " << product.product.name << " " << product.product.quantity << std::endl;
}
}
void menu(std::vector<Product> &list) {
std::cout << "Wcisnij 1, aby dodac produkt, 2 aby przejsc do podsumowania, lub 0, aby zakonczyc program\n";
int choice = -1;
while (choice != 0) {
std::cin >> choice;
switch (choice) {
case 0:
break;
case 1:
addToList(list);
break;
case 2:
sumUp(list);
break;
default:
std::cout << "Podano nieodpowiedni znak\n";
}
}
}
int main() {
std::vector<Product>list;
menu(list);
return 0;
}
Wyjaśnienie: