Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include <ctime>
- using namespace std;
- struct Car
- {
- struct Fio
- {
- char *Name;
- char *Surname;
- char *SecondName;
- } Owner;
- struct Date
- {
- int day;
- char *month;
- int year;
- } checkup;
- char *model; //Марка машины
- int enginePower; //Мощность двигателя
- int mileage; //Пробег
- };
- void createNewCar(Car *&cars, int &size);
- void showInfo(Car someCar);
- void push_back(Car *&cars, int &size, Car newCar);
- void showArrayFullInfo(Car *cars, int size);
- void showCarInfo(Car someCar);
- void showCarCatalog(Car *cars, int size);
- void mainMenu(Car *cars, int size);
- void findModel(Car *cars, int size);
- void mileageSort(Car *cars, int size);
- void saveData(Car newCar);
- void loadData(Car *&cars, int &size);
- void findCheckup(Car *cars, int size);
- int main() {
- int size = 0;
- Car *cars = new Car[size];
- loadData(cars, size);
- mainMenu(cars, size);
- return(0);
- }
- //Создаем новый элемент (машину)
- void createNewCar(Car *&cars, int &size) {
- Car newCar;
- int buffSize = 0,
- intBuff = 0;
- char buff[100];
- cout << "Enter car model: " << endl;
- cin.get(buff, 100); //Принимаем строку
- buffSize = strlen(buff) + 1; //Определяем ее длину
- newCar.model = new char[buffSize]; //Выделяем память
- strcpy_s(newCar.model, buffSize, buff); //Копируем строку в элемент структуры
- cin.ignore(numeric_limits<streamsize>::max(), '\n'); // чистим поток
- cout << "Enter owner surname: " << endl;
- cin.get(buff, 100);
- buffSize = strlen(buff) + 1;
- newCar.Owner.Surname = new char[buffSize];
- strcpy_s(newCar.Owner.Surname, buffSize, buff);
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- cout << "Enter owner name: " << endl;
- cin.get(buff, 100);
- buffSize = strlen(buff) + 1;
- newCar.Owner.Name = new char[buffSize];
- strcpy_s(newCar.Owner.Name, buffSize, buff);
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- cout << "Enter owner second name: " << endl;
- cin.get(buff, 100);
- buffSize = strlen(buff) + 1;
- newCar.Owner.SecondName = new char[buffSize];
- strcpy_s(newCar.Owner.SecondName, buffSize, buff);
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- bool bFail = true;
- do {
- cout << "Enter engine power (in h.p.): " << endl;
- cin >> newCar.enginePower;
- bFail = cin.fail();
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- } while (bFail == true);
- do {
- cout << "Enter mileage: " << endl;
- cin >> newCar.mileage;
- bFail = cin.fail();
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- } while (bFail == true);
- cout << "Enter the last checkup date: " << endl;
- do {
- cout << "Enter day: " << endl;
- cin >> newCar.checkup.day;
- bFail = cin.fail();
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- } while (bFail == true);
- cout << "Enter month (January, May, September etc.): " << endl;
- cin.get(buff, 100);
- buffSize = strlen(buff) + 1;
- newCar.checkup.month = new char[buffSize];
- strcpy_s(newCar.checkup.month, buffSize, buff);
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- do {
- cout << "Enter year: " << endl;
- cin >> newCar.checkup.year;
- bFail = cin.fail();
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- } while (bFail == true);
- saveData(newCar);
- push_back(cars, size, newCar);
- }
- void showInfo(Car someCar)
- {
- cout.width(20);
- cout << someCar.model;
- cout.width(20);
- cout << someCar.Owner.Surname;
- cout.width(20);
- cout << someCar.Owner.Name;
- cout.width(20);
- cout << someCar.Owner.SecondName;
- cout.width(20);
- cout << someCar.enginePower;
- cout.width(10);
- cout << someCar.mileage;
- cout.width(6);
- cout << someCar.checkup.day;
- cout << ".";
- cout << someCar.checkup.month << "." << someCar.checkup.year << endl;
- }
- //Функция добавления элемента в динамический массив
- void push_back(Car *&cars, int &size, Car newCar)
- {
- Car *newArray = new Car[size + 1];
- for (int i = 0; i < size; i++)
- {
- newArray[i] = cars[i];
- }
- newArray[size++] = newCar;
- delete[] cars;
- cars = newArray;
- }
- void showArrayFullInfo(Car *cars, int size)
- {
- if (size == 0)
- {
- cout << "There are no cars in database!" << endl;
- }
- else
- {
- cout.width(20);
- cout << "Car model";
- cout.width(20);
- cout << "Owner Surname";
- cout.width(20);
- cout << "Owner name";
- cout.width(20);
- cout << "Owner second name";
- cout.width(20);
- cout << "Engine power (hp)";
- cout.width(10);
- cout << "Mileage";
- cout.width(20);
- cout << "Last checkup date" << endl << endl;
- for (int i = 0; i < size; i++)
- {
- showInfo(cars[i]);
- }
- }
- }
- void showCarInfo(Car someCar)
- {
- cout.width(20);
- cout << "Car model";
- cout.width(20);
- cout << "Owner Surname";
- cout.width(20);
- cout << "Owner name";
- cout.width(20);
- cout << "Owner second name";
- cout.width(20);
- cout << "Engine power (hp)";
- cout.width(10);
- cout << "Mileage";
- cout.width(20);
- cout << "Last checkup date" << endl << endl;
- showInfo(someCar);
- }
- void showCarCatalog(Car *cars, int size)
- {
- if (size == 0)
- {
- cout << "There are no cars in database!" << endl;
- }
- else
- {
- for (int i = 0; i < size; i++)
- {
- cout << i + 1 << ") " << cars[i].model << endl;
- }
- }
- }
- //Основная функция - главное меню
- void mainMenu(Car *cars, int size)
- {
- while (true)
- {
- cout << endl << endl << "------------------------------------------------" << endl << endl;
- cout << "Choose any action: " << endl;
- cout << "1) Show all cars catalog" << endl; //Вывести все машины в каталоге
- cout << "2) Show car info" << endl; //Вывести информацию по определенной машине
- cout << "3) Add a new car to catalog" << endl; // Добавить новую машину в каталог
- cout << "4) Show all cars catalog (FULL INFO)" << endl; //Вывод каталога с полной информацией
- cout << "5) Find an exact model" << endl; //Поиск и вывод введенной модели
- cout << "6) Sort cars with mileage bigger than ... by owners " << endl; //Поиск и вывод отсортированных по алфавиту владельцев машин, у которых пробег большего введеного
- cout << "7) Show cars with checkup made more than 18 months ago" << endl;
- cout << "0) Exit" << endl;
- int answer = -1;
- bool bFail = true;
- while (answer < 0 || answer > 7 || bFail == true)
- {
- cout << "Enter the number: ";
- cin >> answer;
- bFail = cin.fail();
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- cout << "------------------------------------------------" << endl << endl;
- switch (answer)
- {
- case 1:
- showCarCatalog(cars, size);
- break;
- case 2:
- if (size == 0)
- {
- cout << "There are no cars in database!" << endl;
- }
- else
- {
- cout << "Enter car catalog number to get information (or enter 0 to exit): " << endl;
- answer = -1;
- showCarCatalog(cars, size);
- while (answer < 0 || answer > size)
- {
- cout << "Enter the number: ";
- cin >> answer;
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- if (answer == 0)
- {
- break;
- }
- showCarInfo(cars[answer - 1]);
- }
- break;
- case 3:
- createNewCar(cars, size);
- break;
- case 4:
- showArrayFullInfo(cars, size);
- break;
- case 5:
- findModel(cars, size);
- break;
- case 6:
- mileageSort(cars, size);
- break;
- case 7:
- findCheckup(cars, size);
- break;
- case 0:
- exit(0);
- }
- }
- }
- void findModel(Car *cars, int size)
- {
- if (size == 0)
- {
- cout << "There are no cars in database!" << endl;
- }
- else
- {
- char model[100];
- cout << "Enter the interested model: ";
- cin.get(model, 100);
- cout << model << endl;
- for (int i = 0; i < size; i++)
- {
- if (!_stricmp(cars[i].model, model)) // Сравнение независимо от регистра
- {
- showCarInfo(cars[i]);
- }
- }
- }
- }
- void mileageSort(Car *cars, int size)
- {
- if (size == 0)
- {
- cout << "There are no cars in database!" << endl;
- }
- else
- {
- int givenMileage = 0,
- sortedSize = 0;
- bool bFail = true;
- Car *mileageSortCars = new Car[sortedSize]; //создаем массив структур и выделяем под него память
- do {
- cout << "Enter mileage: " << endl;
- cin >> givenMileage;
- bFail = cin.fail();
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- } while (bFail == true);
- for (int i = 0; i < size; i++)
- {
- if (cars[i].mileage > givenMileage)
- {
- push_back(mileageSortCars, sortedSize, cars[i]); //Добавляем подходящие машины в массив на вывод
- }
- }
- //Сортировка массива по алфавиту
- for (int i = 0; i < sortedSize - 1; i++)
- {
- for (int j = i + 1; j < sortedSize; j++)
- {
- if (_stricmp(mileageSortCars[i].Owner.Surname, mileageSortCars[j].Owner.Surname) == 0) // Если фамилии одинаковы, сравниваем по имени
- {
- if (_stricmp(mileageSortCars[i].Owner.Name, mileageSortCars[j].Owner.Name) > 0)
- {
- Car q = mileageSortCars[i];
- mileageSortCars[i] = mileageSortCars[j];
- mileageSortCars[j] = q;
- }
- }
- if (_stricmp(mileageSortCars[i].Owner.Surname, mileageSortCars[j].Owner.Surname) > 0)
- {
- Car q = mileageSortCars[i];
- mileageSortCars[i] = mileageSortCars[j];
- mileageSortCars[j] = q;
- }
- }
- }
- for (int i = 0; i < sortedSize; i++)
- {
- cout << mileageSortCars[i].Owner.Surname << " " << mileageSortCars[i].Owner.Name << endl;
- }
- delete[] mileageSortCars; //Освобождаем память
- mileageSortCars = nullptr;
- }
- }
- void saveData(Car newCar) {
- ofstream fout;
- fout.open("database.txt", ofstream::app);
- if (!fout.is_open())
- {
- cout << "Error opening file" << endl;
- fout.open("database.txt");
- }
- if (!fout.is_open())
- {
- cout << "Error opening file" << endl;
- }
- else
- {
- fout << newCar.model << endl;
- fout << newCar.Owner.Name << endl;
- fout << newCar.Owner.Surname << endl;
- fout << newCar.Owner.SecondName << endl;
- fout << newCar.enginePower << endl;
- fout << newCar.mileage << endl;
- fout << newCar.checkup.day << endl;
- fout << newCar.checkup.month << endl;
- fout << newCar.checkup.year << endl;
- cout << "data was written" << endl;
- }
- fout.close();
- }
- void loadData(Car *&cars, int &size) {
- ifstream fin;
- fin.open("database.txt");
- if (!fin.is_open())
- {
- cout << "Can't download database!" << endl;
- }
- else
- {
- cout << "Database is opened!" << endl;
- int buffSize = 0,
- intBuff = 0,
- num = 0;
- char buff[100];
- while (!fin.eof())
- {
- Car newCar;
- fin.getline(buff, 100);
- if (strlen(buff) == 0)
- {
- break;
- }
- buffSize = strlen(buff) + 1; //Определяем ее длину
- newCar.model = new char[buffSize]; //Выделяем память
- strcpy_s(newCar.model, buffSize, buff); //Копируем строку в элемент структуры
- fin.getline(buff, 100);
- buffSize = strlen(buff) + 1; //Определяем ее длину
- newCar.Owner.Surname = new char[buffSize]; //Выделяем память
- strcpy_s(newCar.Owner.Surname, buffSize, buff); //Копируем строку в элемент структуры
- fin.getline(buff, 100);
- buffSize = strlen(buff) + 1; //Определяем ее длину
- newCar.Owner.Name = new char[buffSize]; //Выделяем память
- strcpy_s(newCar.Owner.Name, buffSize, buff); //Копируем строку в элемент структуры
- fin.getline(buff, 100);
- buffSize = strlen(buff) + 1; //Определяем ее длину
- newCar.Owner.SecondName = new char[buffSize]; //Выделяем память
- strcpy_s(newCar.Owner.SecondName, buffSize, buff); //Копируем строку в элемент структуры
- fin.getline(buff, 100);
- num = atoi(buff);
- newCar.enginePower = num;
- fin.getline(buff, 100);
- num = atoi(buff);
- newCar.mileage = num;
- fin.getline(buff, 100);
- num = atoi(buff);
- newCar.checkup.day = num;
- fin.getline(buff, 100);
- buffSize = strlen(buff) + 1; //Определяем ее длину
- newCar.checkup.month = new char[buffSize]; //Выделяем память
- strcpy_s(newCar.checkup.month, buffSize, buff); //Копируем строку в элемент структуры
- fin.getline(buff, 100);
- num = atoi(buff);
- newCar.checkup.year = num;
- push_back(cars, size, newCar);
- };
- }
- }
- void findCheckup(Car *cars, int size) {
- if (size == 0)
- {
- cout << "There are no cars in database!" << endl;
- }
- else
- {
- int sortedSize = 0;
- Car *checkupSortCars = new Car[sortedSize];
- time_t now;
- struct tm nowLocal;
- now = time(NULL);
- localtime_s(&nowLocal, &now);
- int currentMonthAmount = (nowLocal.tm_year + 1900) * 12 + nowLocal.tm_mon;
- for (int i = 0; i < size; i++)
- {
- int month = 0;
- if (_stricmp(cars[i].checkup.month, "January") == 0) {
- month = 0;
- };
- if (_stricmp(cars[i].checkup.month, "February") == 0) {
- month = 1;
- };
- if (_stricmp(cars[i].checkup.month, "March") == 0) {
- month = 2;
- };
- if (_stricmp(cars[i].checkup.month, "April") == 0) {
- month = 3;
- };
- if (_stricmp(cars[i].checkup.month, "May") == 0) {
- month = 4;
- };
- if (_stricmp(cars[i].checkup.month, "June") == 0) {
- month = 5;
- };
- if (_stricmp(cars[i].checkup.month, "July") == 0) {
- month = 6;
- };
- if (_stricmp(cars[i].checkup.month, "August") == 0) {
- month = 7;
- };
- if (_stricmp(cars[i].checkup.month, "September") == 0) {
- month = 8;
- };
- if (_stricmp(cars[i].checkup.month, "October") == 0) {
- month = 9;
- };
- if (_stricmp(cars[i].checkup.month, "November") == 0) {
- month = 10;
- };
- if (_stricmp(cars[i].checkup.month, "December") == 0) {
- month = 11;
- };
- int monthAmount = cars[i].checkup.year * 12 + month;
- if (currentMonthAmount - monthAmount > 18)
- {
- push_back(checkupSortCars, sortedSize, cars[i]);
- }
- for (int i = 0; i < sortedSize; i++)
- {
- cout << "This cars had checkup more than 18 months ago:" << endl;
- cout << checkupSortCars[i].model << " " << checkupSortCars[i].Owner.Surname << " " << checkupSortCars[i].Owner.Name << endl;
- }
- delete[] checkupSortCars; //Освобождаем память
- checkupSortCars = nullptr;
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement