#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cstdio>
using namespace std;
struct date {
int day;
char *month;
int year;
};
struct fio {
char *name;
char *surname;
char *patronymic;
};
struct car {
char *brand;
fio owner;
int engine_power;
int mileage;
date last_service;
};
void add_car(car *cars, int &n);
void print_cars(const car *cars, int n);
void search_cars(const car *cars, int n, date current_date);
int main() {
int n;
cout << "Enter the number of cars: ";
cin >> n;
cin.ignore();
car *cars = new car[n];
add_car(cars, n);
print_cars(cars, n);
time_t now = time(0);
tm *ltm = localtime(&now);
date current_date;
current_date.day = ltm->tm_mday;
current_date.month = new char[20];
strcpy(current_date.month, "xx");
current_date.year = 1900 + ltm->tm_year;
search_cars(cars, n, current_date);
// TODO: load and save data using fread & fwrite
for (int i = 0; i < n; i++) {
delete[] cars[i].brand;
delete[] cars[i].owner.name;
delete[] cars[i].owner.surname;
delete[] cars[i].owner.patronymic;
delete[] cars[i].last_service.month;
}
delete[] cars;
delete[] current_date.month;
return 0;
}
void add_car(car *cars, int &n) {
for (int i = 0; i < n; i++) {
cout << "New car: " << endl;
cout << "Brand: ";
cars[i].brand = new char[20];
gets(cars[i].brand);
cout << "Owner's name: ";
cars[i].owner.name = new char[20];
gets(cars[i].owner.name);
cout << "Owner's surname: ";
cars[i].owner.surname = new char[20];
gets(cars[i].owner.surname);
cout << "Owner's patronymic: ";
cars[i].owner.patronymic = new char[20];
gets(cars[i].owner.patronymic);
cout << "Engine power: ";
cin >> cars[i].engine_power;
cin.ignore();
cout << "Mileage: ";
cin >> cars[i].mileage;
cin.ignore();
cout << "Last service date (day, month, year): ";
int numberOfCharacters;
cars[i].last_service.day = 0;
memset(cars[i].last_service.day, 0, sizeof(cars[i].last_service.day));
cars[i].last_service.month = new char[20];
numberOfCharacters = 0;
gets(cars[i].last_service.month);
cars[i].last_service.year = 0;
memset(cars[i].last_service.year, 0, sizeof(cars[i].last_service.year));
cout << endl;
}
}
void print_cars(const car *cars, int n) {
cout << "Cars information: " << endl;
for (int i = 0; i < n; i++) {
cout << cars[i].brand << " | " << cars[i].owner.name << " | " << cars[i].owner.surname << " | " << cars[i].owner.patronymic
<< " | " << cars[i].engine_power << " | " << cars[i].mileage << " | " << cars[i].last_service.day << " " << cars[i].last_service.month << " " << cars[i].last_service.year << endl;
}
cout << endl;
}
void search_cars(const car *cars, int n, date current_date) {
cout << "Cars that need service: " << endl;
for (int i = 0; i < n; i++) {
int months_since_last_service;
char *month_names[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int current_month_index = 0;
int last_service_month_index = 0;
for (int j = 0; j < 12; j++) {
if (strcmp(current_date.month, month_names[j]) == 0) {
current_month_index = j + 1;
}
if (strcmp(cars[i].last_service.month, month_names[j]) == 0) {
last_service_month_index = j + 1;
}
}
months_since_last_service = (current_date.year - cars[i].last_service.year) * 12 + (current_month_index - last_service_month_index);
if (months_since_last_service > 18) {
cout << cars[i].brand << " | " << cars[i].owner.name << " | " << cars[i].owner.surname << " | " << cars[i].owner.patronymic
<< " | " << cars[i].engine_power << " | " << cars[i].mileage << " | " << cars[i].last_service.day << " " << cars[i].last_service.month << " " << cars[i].last_service.year << endl;
}
}
cout << endl;
}
Обратите внимание, что этот код составлен только для демонстрации логики и может потребовать дополнительных изменений и проверок для гарантии корректной работы программы.