Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #define SIZE 3
- using namespace std;
- struct Tovar
- {
- int section;
- char name[30];
- double price;
- int number;
- Tovar() {}
- Tovar(int section, char n[30], double price, int number)
- {
- this->section = section;
- memcpy(this->name, n, sizeof(n));
- this->price = price;
- this->number = number;
- }
- };
- bool compare(Tovar t, char name[30], int price)
- {
- for (int i = 0; name[i] != '\0'; i++)
- {
- if (name[i] != t.name[i])
- return false;
- }
- return true;
- }
- void add(Tovar* t)
- {
- char name[30];
- int section = 23;
- double price;
- int number = 1;
- int position = 0;
- int i = 0;
- while (i < SIZE)
- {
- cout << "Enter the name of product and price" << endl;
- cin >> name >> price;
- bool isAdded = false;
- for (int j = 0; j < SIZE; j++)
- {
- if (compare(t[j], name, price))
- {
- t[j].number++;
- isAdded = true;
- break;
- }
- }
- if (!isAdded)
- {
- t[i].price = price;
- t[i].section = 24;
- t[i].number = number;
- memcpy(t[i].name, name, sizeof(name));
- isAdded = false;
- i++;
- }
- }
- }
- void show(Tovar* t)
- {
- for (int i = 0; i < SIZE; i++)
- {
- cout << "________________________" << endl;
- cout << "Name: " << t[i].name << endl;
- cout << "Price: " << t[i].price << endl;
- cout << "Number: " << t[i].number << endl;
- cout << "________________________" << endl;
- }
- }
- bool comparisonFunc(const char *c1, const char *c2)
- {
- return strcmp(c1, c2) < 0;
- }
- void findBySection(Tovar* t, int section)
- {
- vector<char*> names;
- for (int i = 0; i < SIZE; i++)
- {
- if (t[i].section == section)
- names.push_back(t[i].name);
- }
- sort(names.begin(), names.end(), comparisonFunc);
- cout << "The products of section number " << section << endl;
- for (auto x : names)
- cout << x << endl;
- }
- void findByNumber(Tovar* t, int number)
- {
- vector<char*> products;
- for (int i = 0; i < SIZE; i++)
- {
- if (t[i].number < number)
- products.push_back(t[i].name);
- }
- for (auto x : products)
- cout << x << endl;
- }
- int main()
- {
- Tovar* t = new Tovar[SIZE];
- add(t);
- show(t);
- findByNumber(t, 2);
- //findBySection(t, 24);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement