#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include #include #include #include #include #include #include typedef struct { int npp; char punkt[60]; int number; char type; int hour; int min; int time; } poezd; void ends(void) { printf("\n\n\n\n\n\n\n\n\nЧтобы выйти в меню нажмите любую клавишу"); getch(); } int check(char* patth) { fflush(stdin); if (strlen(patth) < 250) { printf("Активный файл: "); puts(patth); printf("\n"); } else { printf("Нет активного файла\n"); return -1; } return 0; } void stroka(void) { //printf("__________________________________________________________________________________________________________\n"); printf("==========================================================================================================\n"); } void creat(char* patth, int* size) { fflush(stdin); system("cls"); char path[250]; printf("\t\tСоздание файла\n\n"); int kod = check(patth); printf("Файл создавать где?\n"); fflush(stdin); gets(path); strcpy(patth, path); poezd* rasp; FILE* file = fopen(path, "wb+"); printf("Сколько поездов?\n"); int tmp; scanf("%d%*c", &tmp); *size = tmp; printf("Введите в формате:\n Пункт_назначения Номер_поезда Типа_состава(Э/П/С) Время_отправления(hh:mm) Время_в_пути(мин)\n"); rasp = calloc(tmp, sizeof(poezd)); for (int i = 0; i < tmp; i++) { scanf("%s%*c", &rasp[i].punkt); scanf("%d%*c", &rasp[i].number); while (1) { char tmp = getchar(); if (tmp != ' ') { rasp[i].type = tmp; break; } } scanf("%d%*c%d%*c", &rasp[i].hour, &rasp[i].min); scanf("%d%*c", &rasp[i].time); rasp[i].npp = i + 1; } fwrite(rasp, sizeof(poezd), tmp, file); fclose(file); free(rasp); ends(); } void open(char* patth, int* size) { fflush(stdin); system("cls"); printf("\t\tОткрытие файла\n"); int kod = check(patth); printf("Файл лежит, где?\n"); gets(patth); FILE* file = fopen(patth, "rb+"); while (file == NULL) { printf("Ошибка открытия файла\n"); gets(patth); file = fopen(patth, "rb+"); } fseek(file, 0, SEEK_END); *size = ftell(file) / sizeof(poezd); fclose(file); ends(); } void shapka(void) { stroka(); printf("|| Н/П |"); printf("| Пункт назначения |"); printf("| № поезда |"); printf("| Тип поезда |"); printf("| Время отправления |"); printf("| Время в пути (мин) ||"); printf("\n"); stroka(); } void strochka(poezd* rasp, int i, int f) { if (f == -1) { printf("|| %3d |", rasp[i].npp); } else { printf("|| %3d |", f); } printf("| %16s |", rasp[i].punkt); printf("| %8d |", rasp[i].number); if (rasp[i].type == 'Э' || rasp[i].type == 'э') { printf("| Экспресс |"); } else if (rasp[i].type == 'П' || rasp[i].type == 'п') { printf("| Пассажирский |"); } else if (rasp[i].type == 'С' || rasp[i].type == 'с') { printf("| Скорый |"); } if (rasp[i].hour < 10) { if (rasp[i].min < 10) { printf("| 0%d:0%d |", rasp[i].hour, rasp[i].min); } else { printf("| 0%d:%2d |", rasp[i].hour, rasp[i].min); } } else if (rasp[i].min < 10) { printf("|%16d:0%d |", rasp[i].hour, rasp[i].min); } else { printf("|%16d:%2d |", rasp[i].hour, rasp[i].min); } printf("|%19d ||", rasp[i].time); printf("\n"); } void sort2(char* patth, int size) { fflush(stdin); system("cls"); printf("\t\tПоиск поезд определенного типа, доезжающий до Москвы за наименьшее время\n\n"); int kod = check(patth); if (kod == -1) { ends(); return; } FILE* file = fopen(patth, "rb+"); poezd* rasp; char tip; rasp = calloc(size, sizeof(poezd)); fread(rasp, sizeof(poezd), size, file); printf("Тип поезда?(Э/П/С(Экспресс/Пассажирский/Скорый))\n"); tip = getchar(); getchar(); int min = 99999999, min_i = -1; for (int i = 0; i < size; i++) { if ((min > rasp[i].time) && strcoll(rasp[i].punkt, "МСК") == 0 && rasp[i].type == tip) { min = rasp[i].time; min_i = i; } } if (min_i != -1) { shapka(); strochka(rasp, min_i, -1); stroka(); } else { printf("Таких поездов нету"); } fclose(file); free(rasp); ends(); fflush(stdin); } /* void view(char* patth, int size) { fflush(stdin); system("cls"); printf("\t\tРаспсание\n\n"); int kod = check(patth); if (kod == -1) { ends(); return; } FILE* file = fopen(patth, "rb+"); poezd* rasp; rasp = calloc(size, sizeof(poezd)); fread(rasp, sizeof(poezd), size, file); shapka(); for (int i = 0; i < size; i++) { strochka(rasp, i); stroka(); } free(rasp); fclose(file); ends(); } */ void shapki(int k, char* patth) { int kod; if (k == 1) { printf("\t\tДобавление записи\n"); kod = check(patth); printf("Вам нужно вывести расписание?(Y/N)\n"); } if (k == 2) { printf("\t\tУдаление записи\n"); kod = check(patth); printf("Вам нужно вывести расписание?(Y/N)\n"); } if (k == 3) { printf("\t\tИзменение записи\n"); kod = check(patth); printf("Вам нужно вывести расписание?(Y/N)\n"); } if (k == 4) { printf("\t\tИзменение записи\n"); kod = check(patth); printf("Вам нужно вывести расписание?(Y/N)\n"); } if (k == 0) { printf("\t\tРаспсание\n\n"); kod = check(patth); } } int list(char* patth, int size, int now, int konec, int na_str, int kluch) { system("cls"); shapki(kluch, patth); int f = 0; FILE* file = fopen(patth, "rb+"); poezd* rasp; rasp = calloc(size, sizeof(poezd)); fread(rasp, sizeof(poezd), size, file); for (int i = now; i < now + na_str; i++) { if (i == konec) { break; } if (f == 0) { f++; shapka(); } strochka(rasp, i, -1); stroka(); } printf("\n\n"); if (now + na_str <= size) { printf("Ввыведены элементы %3d -- %3d из %3d", now + 1, now + na_str, size); } else { printf("Ввыведены элементы %3d -- %3d из %3d", now + 1, size, size); } char stat; if (now == 0) { printf("\n\n"); printf("вперед - >"); printf("\n\n"); printf("0 - для выхода"); free(rasp); fclose(file); do { stat = getch(); fflush(stdin); if (stat == '>' || stat == '0') { break; } } while (1); if (stat == '>') { now = now + na_str; return list(patth, size, now, konec, na_str, kluch); } else if (stat == '0') { return -1; } } else if (now + na_str >= size) { printf("\n\n"); printf("< - назад"); printf("\n\n"); printf("0 - для выхода"); free(rasp); fclose(file); do { stat = getch(); fflush(stdin); if (stat == '<' || stat == '0') { break; } } while (1); if (stat == '<') { now = now - na_str; return list(patth, size, now, konec, na_str, kluch); } else if (stat == '0') { return -1; } } else { printf("\n\n"); printf("< - назад вперед - >"); printf("\n\n"); printf("0 - для выхода"); free(rasp); fclose(file); do { stat = getch(); fflush(stdin); if (stat == '<' || stat == '>' || stat == '0') { break; } } while (1); if (stat == '>') { now = now + na_str; return list(patth, size, now, konec, na_str, kluch); } else if (stat == '<') { now = now - na_str; return (patth, size, now, konec, na_str); } else if (stat == '0') { return -1; } } return 0; } void view(char* patth, int size, int kluch) { fflush(stdin); system("cls"); printf("\t\tРаспсание\n\n"); int kod = check(patth); int na_str = 10; if (kod == -1) { ends(); return; } int f = 0; int now = 0; do { f = list(patth, size, now, size, na_str, kluch); if (f == -1) { break; } } while (1); } void edit_add(char* patth, int* size) { fflush(stdin); system("cls"); printf("\t\tДобавление записи\n"); int kod = check(patth); printf("Вам нужно вывести расписание?(Y/N)\n"); char stat = getchar(); fflush(stdin); if (stat == 'Y' || stat == 'y') { view(patth, *size, 1); printf("\n"); } FILE* file = fopen(patth, "ab+"); poezd rasp; printf("Запись:\n"); rasp.npp = *size + 1; scanf("%s%*c", &rasp.punkt); scanf("%d%*c", &rasp.number); while (1) { char tmp = getchar(); if (tmp != ' ') { rasp.type = tmp; break; } } scanf("%d%*c", &rasp.hour); scanf("%d%*c", &rasp.min); scanf("%d%*c", &rasp.time); fwrite(&rasp, sizeof(poezd), 1, file); (*size)++; fclose(file); } void edit_del(char* patth, int* size) { fflush(stdin); system("cls"); printf("\t\tУдаление записи\n"); int num, k = 0; int kod = check(patth); printf("Вам нужно вывести расписание?(Y/N)\n"); char stat = getchar(); fflush(stdin); if (stat == 'Y' || stat == 'y') { view(patth, *size, 2); printf("\n"); } printf("Введите номер записи которую хотите удалить:\n"); do { scanf("%d%*c", &num); if (num>0 && num <= *size) { break; } else { printf("Такой записи нет. Попробуйте еще раз:\n"); } } while (1); FILE* file = fopen(patth, "rb+"); poezd* rasp; rasp = calloc(*size, sizeof(poezd)); int tmp = *size + 1; poezd* tmp_rasp = calloc(tmp, sizeof(poezd)); fread(rasp, sizeof(poezd), *size, file); for (int i = 0; i < *size; i++) { tmp_rasp[k].npp = k + 1; strcpy(tmp_rasp[k].punkt, rasp[i].punkt); tmp_rasp[k].number = rasp[i].number; tmp_rasp[k].type = rasp[i].type; tmp_rasp[k].hour = rasp[i].hour; tmp_rasp[k].min = rasp[i].min; tmp_rasp[k].time = rasp[i].time; if (num - 1 == i) { continue; } k++; } fclose(file); free(rasp); remove(patth); file = fopen(patth, "wb+"); (*size)--; fwrite(tmp_rasp, sizeof(poezd), *size, file); free(tmp_rasp); fclose(file); } void change_menu(void) { fflush(stdin); puts("1. Пункт назначения"); puts("2. Номер поезда"); puts("3. Тип поезда"); puts("4. Время отправления"); puts("5. Время в пути"); puts("6. Всё"); puts("0. Ничего"); } void interf(char* patth, int size, char stat, int num) { printf("\t\tИзменение записи\n"); printf("Вам нужно вывести расписание?(Y/N)\n"); if (stat == 'Y' || stat == 'y') { view(patth, size, 3); printf("\n"); } printf("Введите номер записи которую хотите изменить\n"); printf("%d\n", num); } void edit_change(char* patth, int size) { fflush(stdin); system("cls"); printf("\t\tИзменение записи\n"); int num, k = 0; int kod = check(patth); printf("Вам нужно вывести расписание?(Y/N)\n"); char stat = getch(); fflush(stdin); if (stat == 'Y' || stat == 'y') { view(patth, size, 4); printf("\n"); } printf("Введите номер записи которую хотите изменить\n"); do { scanf("%d%*c", &num); if (num > 0 && num <= size) { break; } else { printf("Такой записи нет. Попробуйте еще раз:\n"); } } while (1); num--; FILE* file = fopen(patth, "rb+"); poezd* rasp; rasp = calloc(size, sizeof(poezd)); fread(rasp, sizeof(poezd), size, file); fclose(file); shapka(); strochka(rasp, num, -1); stroka(); int f = 0; char status; do { if (f != 0) { interf(patth, size, stat, num); } fflush(stdin); change_menu(); status = getch(); switch (status) { case '1': fflush(stdin); printf("Новый пункт назначения: "); scanf("%s%*c", &rasp[num].punkt); continue; case '2': fflush(stdin); printf("Новый номер поезда: "); scanf("%d%*c", &rasp[num].number); continue; case '3': fflush(stdin); getchar(); printf("Новый тип поезда: "); rasp[num].type = getchar(); getchar(); continue; case '4': fflush(stdin); printf("Новое время отправления: "); scanf("%d%*c%d", &rasp[num].hour, &rasp[num].min); continue; case '5': fflush(stdin); printf("Новое время в пути: "); scanf("%d%*c", &rasp[num].time); continue; case '6': fflush(stdin); printf("Новая строка: "); scanf("%s%*c", &rasp[num].punkt); scanf("%d%*c", &rasp[num].number); while (1) { char tmp = getchar(); if (tmp != ' ') { rasp[num].type = tmp; break; } } scanf("%d%*c", &rasp[num].hour); scanf("%d%*c", &rasp[num].min); scanf("%d%*c", &rasp[num].time); continue; case '0': break; } printf("\n"); remove(patth); file = fopen(patth, "wb+"); fwrite(rasp, sizeof(poezd), size, file); fclose(file); system("cls"); } while (status != '0'); free(rasp); } void edit_menu(void) { puts("1. Добавить запись"); puts("2. Удалить запись"); puts("3. Отредактировать запись"); puts("0. Выход"); } void edit(char* patth, int* size) { char status; fflush(stdin); do { system("cls"); printf("\t\tРедактироание\n\n"); int kod = check(patth); if (kod == -1) { ends(); return; } edit_menu(); status = getch(); switch (status) { case '1': edit_add(patth, size); continue; case '2': edit_del(patth, size); continue; case '3': edit_change(patth, *size); continue; case '0': system("cls"); break; } } while (status != '0'); } void sort(char* patth, int size) { fflush(stdin); system("cls"); printf("\t\tCведения о поездах, отправляющихся в Москву в определенный временной период\n\n"); int kod = check(patth); if (kod == -1) { ends(); return; } FILE* file = fopen(patth, "rb+"); poezd* rasp; rasp = calloc(size, sizeof(poezd)); fread(rasp, sizeof(poezd), size, file); int h_d, m_d, h_p, m_p, time_d, time_p; printf("Промежуток времени(hh:mm - hh:mm): \n"); do { scanf("%d%*c%d%d%*c%d%*c", &h_d, &m_d, &h_p, &m_p); if (h_d >= 0 && h_d < 24 && h_p >= 0 && h_p < 24 && m_d >= 0 && m_d < 60 && m_p >= 0 && m_p < 60) { break; } else { printf("Ошибка при вводе времени. Введите еще раз:\n"); } } while(1); int f = 0; time_d = h_d * 60 + m_d; time_p = h_p * 60 + m_p; for (int i = 0; i < size; i++) { int tmp_time = rasp[i].hour * 60 + rasp[i].min; if (strcoll(rasp[i].punkt,"МСК") == 0 && tmp_time <= time_p && tmp_time >= time_d) { f++; if (f == 1) { shapka(); } strochka(rasp, i, f); stroka(); } } if (f == 0) { printf("Поездов в указанный промежуток времени нету"); } fclose(file); free(rasp); ends(); } void menu(void) { fflush(stdin); puts("1. Создание файла"); puts("2. Подключить готовый файл"); puts("3. Ввывод данных"); puts("4. Поезда в МСК(за определенное время)"); puts("5. Поезд определенного типа, доезжающий до Москвы за наименьшее"); puts("6. Редактирование"); puts("0. Выход"); } int main(void) { system("color F0"); SetConsoleCP(1251); SetConsoleOutputCP(1251); char status; char path[250]; int razm = 0; do { fflush(stdin); system("cls"); check(path); menu(); status = getch(); switch (status) { case '1': creat(path, &razm); continue; case '2': open(path, &razm); continue; case '3': view(path, razm, 0); continue; case '4': sort(path, razm); continue; case '5': sort2(path, razm); continue; case '6': edit(path, &razm); continue; case '0': system("cls"); printf("\t\tВыход"); break; } } while (status != '0'); return 0; }