Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <conio.h>
- #include <fstream>
- #if __has_include(<regex>)
- #define has_regex true
- #include <regex>
- #endif
- using namespace std;
- struct Cars {
- string marka;
- string nomer;
- };
- auto input_cars(Cars *, size_t) -> void;
- auto output_cars(Cars *, size_t) -> void;
- auto search_cars(Cars *, size_t) -> void;
- auto change_car_number(Cars *, size_t) -> void;
- int main() {
- size_t choice, count(0);
- Cars *cars = nullptr;
- do {
- system("cls");
- cout << "Menu: \n\n";
- cout << "[1] Input cars\n";
- cout << "[2] Search in file\n";
- cout << "[3] Change car number\n";
- cout << "[4] Output information from the file\n\n";
- cout << "[0] Exit\n";
- cout << "Your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- cout << "Enter number of cars: ";
- cin >> count;
- if (!cin || !count)
- cout << "Error: invalid number of cars";
- cars = new Cars[count];
- input_cars(cars, count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 2:
- system("cls");
- search_cars(cars, count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 3: {
- system("cls");
- change_car_number(cars, count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- }
- case 4:
- system("cls");
- output_cars(cars, count);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- }
- } while (choice != 0);
- return 0;
- }
- auto input_cars(Cars *cars, size_t count) -> void {
- fstream file("cars.dat", ios::binary | ios::out);
- cin.ignore();
- for (size_t i = 0; i < count; i++) {
- bool isCorrect(false);
- cout << "Enter the brand of car #" << i << " : ";
- getline(cin, cars[i].marka);
- do {
- cout << "Enter car number: ";
- getline(cin, cars[i].nomer);
- #if has_regex
- if (regex_match(cars[i].nomer, static_cast<regex> ("[A-Z]{1} [0-9]{4} [A-Z]{2}")))
- for (size_t j = 0; j < count - 1; j++)
- if (cars[j].nomer == cars[i].nomer && j != i)
- cout << "\n\nThe car number must be 9 characters and UNIQUE!\n\n";
- else
- isCorrect = true;
- #else
- if (cars[i].nomer.length() == 9) {
- for (size_t j = 0; j < count - 1; j++)
- if (cars[j].nomer == cars[i].nomer && j != i)
- isCorrect = true;
- else
- cout << "\n\nThe car number must be 9 characters and UNIQUE!\n\n";
- }
- #endif
- } while (!isCorrect);
- }
- file.write(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
- file.close();
- }
- auto output_cars(Cars *cars, size_t count) -> void {
- fstream file("cars.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
- for (size_t i = 0; i < count; i++)
- cout << "Marka: " + cars[i].marka + " | Nomer: " + cars[i].nomer << endl;
- file.close();
- }
- auto search_cars(Cars *cars, size_t count) -> void {
- fstream file("cars.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
- size_t counter(0);
- for (size_t i = 0; i < count; i++) {
- if (strstr(cars[i].nomer.c_str(), "B 57")) {
- cout << "Marka: " + cars[i].marka + " | Nomer: " + cars[i].nomer << endl;
- }
- else
- counter++;
- }
- if (counter == 0)
- cout << "There are no cars with number that starts with \"B 57\"";
- file.close();
- }
- auto change_car_number(Cars *cars, size_t count) -> void {
- fstream file("cars.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
- file.close();
- file.open("cars.dat", ios::binary | ios::out);
- cin.ignore();
- size_t counter(0);
- string car_number;
- bool isCorrect(false);
- cout << "Enter car number to change: ";
- getline(cin, car_number);
- for (size_t i = 0; i < count; i++) {
- if (car_number == cars[i].nomer) {
- counter++;
- cout << "Current car number: " << cars[i].nomer << " | Car Brand: " << cars[i].marka << "\n\n";
- do {
- cout << "Enter the new car number: ";
- getline(cin, cars[i].nomer);
- #if has_regex
- if (regex_match(cars[i].nomer, static_cast<regex> ("[A-Z]{1} [0-9]{4} [A-Z]{2}")))
- for (size_t j = 0; j < count - 1; j++)
- if (cars[j].nomer == cars[i].nomer && j != i)
- cout << "\n\nThe car number must be 9 characters and UNIQUE!\n\n";
- else
- isCorrect = true;
- #else
- if (friends[i].nomer.length() == 9) {
- for (size_t j = 0; j < count; j++)
- if (cars[j].nomer == cars[i].nomer && j != i)
- isCorrect = true;
- else
- cout << "\n\nThe car number must be 9 characters and UNIQUE!\n\n";
- }
- #endif
- } while (!isCorrect);
- }
- }
- if (counter == 0)
- cout << "There is no car with that number!";
- file.write(reinterpret_cast<char *> (&cars), count * sizeof(Cars));
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement