Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //#include <locale.h>
- #include <windows.h>
- #define MAX_CARS 100
- #define PAGE_SIZE 10
- struct Car {
- char brand[50];
- char country[50];
- int year;
- float engineVolume;
- float fuelConsumption;
- float price;
- int quantity;
- };
- void displayMenu() {
- printf("1. Просмотр содержимого файла\n");
- printf("2. Добавление данных\n");
- printf("3. Удаление данных\n");
- printf("4. Корректировка данных\n");
- printf("5. Выполнение запросов\n");
- printf("6. Выход\n");
- }
- void displayCars(struct Car* cars, int count) {
- printf("------------------------------------------------------------------------------------------------------------------------\n");
- printf("| Марка\t\t| Страна\t| Год\t| Объем двиг.\t| Расход\t| Цена\t| Количество |\n");
- printf("------------------------------------------------------------------------------------------------------------------------\n");
- for (int i = 0; i < count; i++) {
- printf("| %-15s | %-10s | %-4d | %-12.1f | %-6.1f | %-6.1f | %-10d |\n",
- cars[i].brand, cars[i].country, cars[i].year, cars[i].engineVolume,
- cars[i].fuelConsumption, cars[i].price, cars[i].quantity);
- }
- printf("------------------------------------------------------------------------------------------------------------------------\n");
- }
- int main() {
- //setlocale(LC_ALL, "RUS");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- FILE* file;
- struct Car cars[MAX_CARS];
- int count = 0;
- file = fopen("cars.dat", "rb+");
- if (!file) {
- file = fopen("cars.dat", "wb+");
- if (!file) {
- printf("Ошибка создания файла.\n");
- return 1;
- }
- }
- else {
- while (fread(&cars[count], sizeof(struct Car), 1, file) == 1) {
- count++;
- }
- }
- int choice;
- do {
- displayMenu();
- printf("Выберите действие: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- displayCars(cars, count);
- break;
- case 2:
- // Добавление данных
- if (count < MAX_CARS) {
- printf("Введите данные о машине:\n");
- printf("Марка: ");
- scanf("%s", cars[count].brand);
- printf("Страна: ");
- scanf("%s", cars[count].country);
- printf("Год: ");
- scanf("%d", &cars[count].year);
- printf("Объем двигателя: ");
- scanf("%f", &cars[count].engineVolume);
- printf("Расход топлива: ");
- scanf("%f", &cars[count].fuelConsumption);
- printf("Цена: ");
- scanf("%f", &cars[count].price);
- printf("Количество: ");
- scanf("%d", &cars[count].quantity);
- count++;
- }
- else {
- printf("Достигнуто максимальное количество записей.\n");
- }
- break;
- case 3:
- // Удаление данных
- if (count > 0) {
- int index;
- displayCars(cars, count);
- printf("Введите индекс записи для удаления: ");
- scanf("%d", &index);
- if (index >= 0 && index < count) {
- for (int i = index; i < count - 1; i++) {
- cars[i] = cars[i + 1];
- }
- count--;
- printf("Запись успешно удалена.\n");
- }
- else {
- printf("Некорректный индекс записи.\n");
- }
- }
- else {
- printf("Нет данных для удаления.\n");
- }
- break;
- case 4:
- // Корректировка данных
- if (count > 0) {
- int index;
- displayCars(cars, count);
- printf("Введите индекс записи для корректировки: ");
- scanf("%d", &index);
- if (index >= 0 && index < count) {
- printf("Введите новые данные о машине:\n");
- printf("Марка: ");
- scanf("%s", cars[index].brand);
- printf("Страна: ");
- scanf("%s", cars[index].country);
- printf("Год: ");
- scanf("%d", &cars[index].year);
- printf("Объем двигателя: ");
- scanf("%f", &cars[index].engineVolume);
- printf("Расход топлива: ");
- scanf("%f", &cars[index].fuelConsumption);
- printf("Цена: ");
- scanf("%f", &cars[index].price);
- printf("Количество: ");
- scanf("%d", &cars[index].quantity);
- printf("Запись успешно скорректирована.\n");
- }
- else {
- printf("Некорректный индекс записи.\n");
- }
- }
- else {
- printf("Нет данных для корректировки.\n");
- }
- break;
- case 5:
- // Выполнение запросов
- // Пример запроса: вывод всех машин с расходом топлива менее 8 л/100 км
- printf("Машины с расходом топлива менее 8 л/100 км:\n");
- for (int i = 0; i < count; i++) {
- if (cars[i].fuelConsumption < 8.0) {
- printf("| %-15s | %-10s | %-4d | %-12.1f | %-6.1f | %-6.1f | %-10d |\n",
- cars[i].brand, cars[i].country, cars[i].year, cars[i].engineVolume,
- cars[i].fuelConsumption, cars[i].price, cars[i].quantity);
- }
- }
- break;
- case 6:
- break;
- default:
- printf("Некорректный выбор. Попробуйте еще раз.\n");
- }
- } while (choice != 6);
- fseek(file, 0, SEEK_SET);
- for (int i = 0; i < count; i++) {
- fwrite(&cars[i], sizeof(struct Car), 1, file);
- }
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement