Advertisement
J3st3rs_j0k3

pr7_4 for MKT2

Mar 17th, 2024 (edited)
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //#include <locale.h>
  5. #include <windows.h>
  6.  
  7. #define MAX_CARS 100
  8. #define PAGE_SIZE 10
  9.  
  10. struct Car {
  11.     char brand[50];
  12.     char country[50];
  13.     int year;
  14.     float engineVolume;
  15.     float fuelConsumption;
  16.     float price;
  17.     int quantity;
  18. };
  19.  
  20. void displayMenu() {
  21.     printf("1. Просмотр содержимого файла\n");
  22.     printf("2. Добавление данных\n");
  23.     printf("3. Удаление данных\n");
  24.     printf("4. Корректировка данных\n");
  25.     printf("5. Выполнение запросов\n");
  26.     printf("6. Выход\n");
  27. }
  28.  
  29. void displayCars(struct Car* cars, int count) {
  30.     printf("------------------------------------------------------------------------------------------------------------------------\n");
  31.     printf("| Марка\t\t| Страна\t| Год\t| Объем двиг.\t| Расход\t| Цена\t| Количество |\n");
  32.     printf("------------------------------------------------------------------------------------------------------------------------\n");
  33.  
  34.     for (int i = 0; i < count; i++) {
  35.         printf("| %-15s | %-10s | %-4d | %-12.1f | %-6.1f | %-6.1f | %-10d |\n",
  36.             cars[i].brand, cars[i].country, cars[i].year, cars[i].engineVolume,
  37.             cars[i].fuelConsumption, cars[i].price, cars[i].quantity);
  38.     }
  39.  
  40.     printf("------------------------------------------------------------------------------------------------------------------------\n");
  41. }
  42.  
  43. int main() {
  44.     //setlocale(LC_ALL, "RUS");
  45.     SetConsoleCP(1251);
  46.     SetConsoleOutputCP(1251);
  47.     FILE* file;
  48.     struct Car cars[MAX_CARS];
  49.     int count = 0;
  50.  
  51.     file = fopen("cars.dat", "rb+");
  52.  
  53.     if (!file) {
  54.         file = fopen("cars.dat", "wb+");
  55.         if (!file) {
  56.             printf("Ошибка создания файла.\n");
  57.             return 1;
  58.         }
  59.     }
  60.     else {
  61.         while (fread(&cars[count], sizeof(struct Car), 1, file) == 1) {
  62.             count++;
  63.         }
  64.     }
  65.  
  66.     int choice;
  67.     do {
  68.         displayMenu();
  69.         printf("Выберите действие: ");
  70.         scanf("%d", &choice);
  71.  
  72.         switch (choice) {
  73.         case 1:
  74.             displayCars(cars, count);
  75.             break;
  76.         case 2:
  77.             // Добавление данных
  78.             if (count < MAX_CARS) {
  79.                 printf("Введите данные о машине:\n");
  80.                 printf("Марка: ");
  81.                 scanf("%s", cars[count].brand);
  82.                 printf("Страна: ");
  83.                 scanf("%s", cars[count].country);
  84.                 printf("Год: ");
  85.                 scanf("%d", &cars[count].year);
  86.                 printf("Объем двигателя: ");
  87.                 scanf("%f", &cars[count].engineVolume);
  88.                 printf("Расход топлива: ");
  89.                 scanf("%f", &cars[count].fuelConsumption);
  90.                 printf("Цена: ");
  91.                 scanf("%f", &cars[count].price);
  92.                 printf("Количество: ");
  93.                 scanf("%d", &cars[count].quantity);
  94.  
  95.                 count++;
  96.             }
  97.             else {
  98.                 printf("Достигнуто максимальное количество записей.\n");
  99.             }
  100.             break;
  101.         case 3:
  102.             // Удаление данных
  103.             if (count > 0) {
  104.                 int index;
  105.                 displayCars(cars, count);
  106.                 printf("Введите индекс записи для удаления: ");
  107.                 scanf("%d", &index);
  108.  
  109.                 if (index >= 0 && index < count) {
  110.                     for (int i = index; i < count - 1; i++) {
  111.                         cars[i] = cars[i + 1];
  112.                     }
  113.                     count--;
  114.                     printf("Запись успешно удалена.\n");
  115.                 }
  116.                 else {
  117.                     printf("Некорректный индекс записи.\n");
  118.                 }
  119.             }
  120.             else {
  121.                 printf("Нет данных для удаления.\n");
  122.             }
  123.             break;
  124.         case 4:
  125.             // Корректировка данных
  126.             if (count > 0) {
  127.                 int index;
  128.                 displayCars(cars, count);
  129.                 printf("Введите индекс записи для корректировки: ");
  130.                 scanf("%d", &index);
  131.  
  132.                 if (index >= 0 && index < count) {
  133.                     printf("Введите новые данные о машине:\n");
  134.                     printf("Марка: ");
  135.                     scanf("%s", cars[index].brand);
  136.                     printf("Страна: ");
  137.                     scanf("%s", cars[index].country);
  138.                     printf("Год: ");
  139.                     scanf("%d", &cars[index].year);
  140.                     printf("Объем двигателя: ");
  141.                     scanf("%f", &cars[index].engineVolume);
  142.                     printf("Расход топлива: ");
  143.                     scanf("%f", &cars[index].fuelConsumption);
  144.                     printf("Цена: ");
  145.                     scanf("%f", &cars[index].price);
  146.                     printf("Количество: ");
  147.                     scanf("%d", &cars[index].quantity);
  148.  
  149.                     printf("Запись успешно скорректирована.\n");
  150.                 }
  151.                 else {
  152.                     printf("Некорректный индекс записи.\n");
  153.                 }
  154.             }
  155.             else {
  156.                 printf("Нет данных для корректировки.\n");
  157.             }
  158.             break;
  159.         case 5:
  160.             // Выполнение запросов
  161.             // Пример запроса: вывод всех машин с расходом топлива менее 8 л/100 км
  162.             printf("Машины с расходом топлива менее 8 л/100 км:\n");
  163.             for (int i = 0; i < count; i++) {
  164.                 if (cars[i].fuelConsumption < 8.0) {
  165.                     printf("| %-15s | %-10s | %-4d | %-12.1f | %-6.1f | %-6.1f | %-10d |\n",
  166.                         cars[i].brand, cars[i].country, cars[i].year, cars[i].engineVolume,
  167.                         cars[i].fuelConsumption, cars[i].price, cars[i].quantity);
  168.                 }
  169.             }
  170.             break;
  171.         case 6:
  172.             break;
  173.         default:
  174.             printf("Некорректный выбор. Попробуйте еще раз.\n");
  175.         }
  176.     } while (choice != 6);
  177.  
  178.     fseek(file, 0, SEEK_SET);
  179.     for (int i = 0; i < count; i++) {
  180.         fwrite(&cars[i], sizeof(struct Car), 1, file);
  181.     }
  182.  
  183.     fclose(file);
  184.  
  185.     return 0;
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement