Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <locale.h>
- #include <stdio.h>
- #include <string.h>
- struct station
- {
- unsigned int train_id; // Номер поезда
- char stop_name[100]; // Пункт назначения (название)
- unsigned int days_to_ride; // Дни следования
- char arrival_time[6]; // Время прибытия
- char stop_time[6]; // Время стоянки
- };
- struct station stations[30];
- struct station empty;
- unsigned int stationsCount = 0;
- unsigned int selectedOption;
- int main();
- void inputIntoArray();
- void sortArray();
- void findByField();
- void changeElement();
- void deleteElement();
- void printArray();
- void loadFromFile();
- void saveToFile();
- void printStruct(struct station *station);
- int isStringsEquals(const char *string1, const char *string2);
- int main(void) {
- setlocale(LC_ALL, "rus");
- while (1)
- {
- printf("Выберите функцию для выполнения:\n");
- printf("1. Ввод массива структур\n");
- printf("2. Сортировка массива структур\n");
- printf("3. Поиск в массиве структур по заданному параметру\n");
- printf("4. Изменение заданной структуры\n");
- printf("5. Удаление структуры из массива\n");
- printf("6. Вывод на экран массива структур\n");
- printf("7. Ввод данных из файла\n");
- printf("8. Сохранение данных в файл\n");
- printf("9. Выход\n");
- scanf_s("%d", &selectedOption);
- switch (selectedOption)
- {
- case 1:
- inputIntoArray();
- break;
- case 2:
- sortArray();
- break;
- case 3:
- findByField();
- break;
- case 4:
- changeElement();
- break;
- case 5:
- deleteElement();
- break;
- case 6:
- printArray();
- break;
- case 7:
- loadFromFile();
- break;
- case 8:
- saveToFile();
- break;
- case 9:
- return 0;
- default:
- printf("Такой функции не существует!\n\n");
- }
- }
- }
- void inputIntoArray()
- {
- printf("Введите количество добавляемых структур: ");
- scanf_s("%d", &stationsCount);
- for (int i = 0; i < stationsCount; i++)
- {
- printf("Вокзал %d:\n", i + 1);
- printf("Номер поезда? ");
- scanf_s("%d", &stations[i].train_id);
- printf("Пункт назначения? ");
- fflush(stdin);
- gets_s(stations[i].stop_name);
- printf("Дни следования? ");
- scanf_s("%d", &stations[i].days_to_ride);
- printf("Время прибытия? ");
- fflush(stdin);
- gets_s(stations[i].arrival_time);
- printf("Время стоянки? ");
- fflush(stdin);
- gets_s(stations[i].stop_time);
- }
- printf("Записано %d структур!\n\n", stationsCount);
- }
- void sortArray()
- {
- int sortableItem;
- while (1)
- {
- printf("По какому параметру отсортировать массив?\n");
- printf("1. Номер поезда\n");
- printf("2. Дни следования\n");
- scanf_s("%d", &sortableItem);
- struct station temp;
- switch (sortableItem)
- {
- case 1:
- for (int i = 0; i < stationsCount - 1; i++)
- for (int j = 0; j < stationsCount - i - 1; j++)
- if (stations[j].train_id > stations[j + 1].train_id)
- {
- temp = stations[j];
- stations[j] = stations[j + 1];
- stations[j + 1] = temp;
- }
- printf("Массив отсортирован по номеру поездов!\n\n");
- return;
- case 2:
- for (int i = 0; i < stationsCount - 1; i++)
- for (int j = 0; j < stationsCount - i - 1; j++)
- if (stations[j].days_to_ride > stations[j + 1].days_to_ride)
- {
- temp = stations[j];
- stations[j] = stations[j + 1];
- stations[j + 1] = temp;
- }
- printf("Массив отсортирован по дням следования!\n\n");
- return;
- default:
- printf("Такого параметра не существует!\n");
- }
- }
- }
- void printArray()
- {
- printf("\n");
- printf("В массиве находится %d структур!\n\n", stationsCount);
- for (int i = 0; i < stationsCount; i++)
- {
- printf("Вокзал %d:\n", i + 1);
- printStruct(&stations[i]);
- printf("\n");
- }
- }
- void printStruct(struct station *station)
- {
- printf("- Номер поезда: %d\n", station->train_id);
- if (station->stop_name != "")
- printf("- Пункт назначения: %s\n", station->stop_name);
- if (station->days_to_ride != 0)
- printf("- Дни следования: %d\n", station->days_to_ride);
- if (station->arrival_time != "")
- printf("- Время прибытия: %s\n", station->arrival_time);
- if (station->stop_time != "")
- printf("- Время стоянки: %s\n", station->stop_time);
- }
- void findByField()
- {
- int option, records = 0;
- printf("\n");
- while (1)
- {
- option = 0;
- records = 0;
- printf("Выберите параметр для создания фильтра:\n");
- printf("1. Номер поезда\n");
- printf("2. Пункт назначения\n");
- printf("3. Дни следования\n");
- printf("4. Время прибытия\n");
- printf("5. Время стоянки\n");
- printf("6. Выход\n");
- scanf_s("%d", &option);
- printf("\n");
- switch (option)
- {
- case 1:
- {
- int required;
- printf("Введите номер поезда для поиска: ");
- scanf_s("%d", &required);
- for (int i = 0; i < stationsCount; i++)
- if (stations[i].train_id == required)
- {
- printf("Найденная запись #%d\n", records + 1);
- printStruct(&stations[i]);
- records++;
- printf("\n");
- }
- }
- break;
- case 2:
- {
- char required[100];
- printf("Введите пункт назначения для поиска: ");
- scanf_s("%[^\n]", &required);
- for (int i = 0; i < stationsCount; i++)
- if (isStringsEquals(stations[i].stop_name, required))
- {
- printf("Найденная запись #%d\n", records + 1);
- printStruct(&stations[i]);
- records++;
- printf("\n");
- }
- }
- break;
- case 3:
- {
- int required;
- printf("Введите дни следования для поиска: ");
- scanf_s("%d", &required);
- for (int i = 0; i < stationsCount; i++)
- if (stations[i].days_to_ride == required)
- {
- printf("Найденная запись #%d\n", records + 1);
- printStruct(&stations[i]);
- records++;
- printf("\n");
- }
- }
- break;
- case 4:
- {
- char required[6];
- printf("Введите время прибытия для поиска: ");
- scanf_s("%s", &required);
- for (int i = 0; i < stationsCount; i++)
- if (isStringsEquals(stations[i].arrival_time, required))
- {
- printf("Найденная запись #%d\n", records + 1);
- printStruct(&stations[i]);
- records++;
- printf("\n");
- }
- }
- break;
- case 5:
- {
- char required[6];
- printf("Введите время стоянки для поиска: ");
- scanf_s("%s", &required);
- for (int i = 0; i < stationsCount; i++)
- if (isStringsEquals(stations[i].stop_time, required))
- {
- printf("Найденная запись #%d\n", records + 1);
- printStruct(&stations[i]);
- records++;
- printf("\n");
- }
- }
- break;
- case 6:
- return;
- default:
- printf("Такого поля не существует!\n");
- }
- if (option >= 1 && option <= 5)
- printf("Найдено записей: %d\n\n", records);
- }
- }
- void deleteElement()
- {
- int elementIndex;
- printf("Введите номер элемента для удаления: ");
- scanf_s("%d", &elementIndex);
- elementIndex--;
- stations[elementIndex] = empty;
- for (int i = elementIndex; i < stationsCount - 1; i++)
- stations[i] = stations[i + 1];
- stationsCount--;
- printf("Запись удалена успешно!\n\n");
- }
- void changeElement()
- {
- int elementIndex, option;
- printf("Введите номер элемента для изменения: ");
- scanf_s("%d", &elementIndex);
- elementIndex--;
- printf("\nВыбранный элемент:\n");
- printStruct(&stations[elementIndex]);
- while (1)
- {
- printf("\nВыберите поле для изменения:\n");
- printf("1. Номер поезда\n");
- printf("2. Пункт назначения\n");
- printf("3. Дни следования\n");
- printf("4. Время прибытия\n");
- printf("5. Время стоянки\n");
- printf("6. Выход\n");
- scanf_s("%d", &option);
- switch (option)
- {
- case 1:
- printf("Введите новый номер поезда: ");
- scanf_s("%d", &stations[elementIndex].train_id);
- printf("Изменённая запись:\n");
- printStruct(&stations[elementIndex]);
- break;
- case 2:
- printf("Введите новый пункт назначения: ");
- fflush(stdin);
- gets_s(stations[elementIndex].stop_name);
- printf("Изменённая запись:\n");
- printStruct(&stations[elementIndex]);
- break;
- case 3:
- printf("Введите новое количество дней следования: ");
- scanf_s("%d", &stations[elementIndex].days_to_ride);
- printf("Изменённая запись:\n");
- printStruct(&stations[elementIndex]);
- break;
- case 4:
- printf("Введите новое время прибытия: ");
- scanf_s("%s", &stations[elementIndex].arrival_time);
- printf("Изменённая запись:\n");
- printStruct(&stations[elementIndex]);
- break;
- case 5:
- printf("Введите новое время стоянки: ");
- scanf_s("%s", &stations[elementIndex].stop_time);
- printf("Изменённая запись:\n");
- printStruct(&stations[elementIndex]);
- break;
- case 6:
- return;
- default:
- printf("Этого поля не существует!\n");
- }
- }
- }
- void loadFromFile()
- {
- FILE *file;
- char filePath[255];
- while (1)
- {
- printf("Введите путь до файла: ");
- scanf_s("%s", filePath);
- if (fopen_s(&file, filePath, "r") == 0)
- printf("Этот файл недоступен!\n");
- else
- break;
- }
- stationsCount = 0;
- while (fscanf_s(file, "- Номер поезда: %d\n- Пункт назначения: %[^\n]\n- Дни следования: %d\n- Время прибытия: %s\n- Время стоянки: %s\n\n",
- &stations[stationsCount].train_id, &stations[stationsCount].stop_name, &stations[stationsCount].days_to_ride, &stations[stationsCount].arrival_time,
- &stations[stationsCount].stop_time) != EOF)
- stationsCount++;
- fclose(file);
- printf("Прочитано %d записей!\n\n", stationsCount);
- }
- void saveToFile()
- {
- FILE *file;
- char filePath[255];
- while (1)
- {
- printf("Введите путь до файла: ");
- scanf_s("%s", filePath);
- if (fopen_s(&file, filePath, "w") == 0)
- printf("Этот файл недоступен!\n");
- else
- break;
- }
- for (int i = 0; i < stationsCount; i++)
- {
- fprintf(file, "- Номер поезда: %d\n", stations[i].train_id);
- fprintf(file, "- Пункт назначения: %s\n", stations[i].stop_name);
- fprintf(file, "- Дни следования: %d\n", stations[i].days_to_ride);
- fprintf(file, "- Время прибытия: %s\n", stations[i].arrival_time);
- fprintf(file, "- Время стоянки: %s\n\n", stations[i].stop_time);
- }
- fclose(file);
- printf("Записано %d записей!\n\n", stationsCount);
- }
- int isStringsEquals(const char *string1, const char *string2)
- {
- if (string1 == NULL || string2 == NULL)
- return 0;
- if (strlen(string1) != strlen(string2))
- return 0;
- for (int i = 0; i < strlen(string1); i++)
- {
- if (string1[i] != string2[i])
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment