Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #define SIZE 3
- using namespace std;
- enum month {
- jan = 1, feb, mar, apr, may, june, jul, aug, sep, oct, nov, dec
- };
- typedef struct Date {
- int day;
- month mon;
- int year;
- }date;
- struct Tovar
- {
- int section;
- char name[30];
- double price;
- int number;
- int countMon;
- date dt;
- Tovar() {}
- Tovar(int section, char n[30], double price, int number, int day, month mon, int year, int countMon)
- {
- this->section = section;
- memcpy(this->name, n, sizeof(n));
- this->price = price;
- this->number = number;
- this->dt.day = day;
- this->dt.mon = mon;
- this->dt.year = year;
- this->countMon = countMon;
- }
- };
- //Функция для сравнения
- 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 save(Tovar* t)
- {
- ofstream file;
- file.open("data.txt");
- for (int i = 0; i < SIZE; i++)
- {
- file << t[i].name << " " << t[i].price << " " << t[i].section << " " << t[i].number
- << " " << t[i].dt.day << " " << t[i].dt.mon << " " << t[i].dt.year << " " << t[i].countMon << endl;
- }
- file.close();
- }
- //Ф-я для загрузки результатов с файла
- void load(Tovar* t)
- {
- char name[30];
- int section = 23;
- double price;
- int number = 1;
- int day, year;
- int mon;
- int countMon;
- ifstream file("data.txt");
- if (file.is_open())
- {
- for (int i = 0; i < SIZE; i++)
- {
- file >> name >> price >> section >> number >> day >> mon >> year >> countMon;
- memcpy(t[i].name, name, sizeof(name));
- t[i].price = price;
- t[i].section = section;
- t[i].number = number;
- t[i].dt.day = day;
- t[i].dt.mon = (month)mon;
- t[i].dt.year = year;
- t[i].countMon = countMon;
- }
- file.close();
- }
- else
- {
- cout << "Unable to open file" << endl;
- }
- }
- //Ф-я заполнения массива студентов
- void init_products(Tovar* t)
- {
- char name[30];
- int section = 23;
- double price;
- int number = 1;
- int day, year;
- int mon;
- int countMon;
- int position = 0;
- int i = 0;
- while (i < SIZE)
- {
- cout << "Enter the name of product, price and date of creation d/m/y and shelf life in month" << endl;
- cin >> name >> price >> day >> mon >> year >> countMon;
- 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;
- t[i].dt.day = day;
- t[i].dt.mon = (month)mon;
- t[i].dt.year = year;
- t[i].countMon = countMon;
- memcpy(t[i].name, name, sizeof(name));
- isAdded = false;
- i++;
- }
- }
- save(t);
- }
- //Функция вывода студентов
- 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;
- }
- //Функция для находения просроченных товаров
- void findOutOfDate(Tovar* t, int day, int mon, int year)
- {
- vector<char*> products;
- month end;
- for (int i = 0; i < SIZE; i++)
- {
- end = (month)((t[i].dt.mon + t[i].countMon) % 12);
- if (t[i].dt.year < year)
- {
- products.push_back(t[i].name);
- }
- else
- {
- if (end < mon)
- {
- products.push_back(t[i].name);
- }
- else
- {
- if (t[i].dt.day < day && t[i].dt.year < year && t[i].dt.mon < mon)
- {
- products.push_back(t[i].name);
- }
- }
- }
- }
- for (auto x : products)
- cout << x << endl;
- }
- int main()
- {
- Tovar* t = new Tovar[SIZE];
- init_products(t);//функция для заполнения массива с клавы
- //load(t);//Функция для заполнения масива из файла
- show(t);
- //findOutOfDate(t, 2, 1, 2018);
- //findByNumber(t, 2);
- //findBySection(t, 24);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement