Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************/
- /* Расчетно графическая работа */
- /* Вариант 4 */
- /* Выполнил студент гр. 515-А */
- /* Голованов Дмитрий */
- /*************************************************/
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <locale.h>
- #include <string.h>
- struct route
- {
- char departurepoint[20];
- char destinationpoint[20];
- char point[20];
- int routenumber;
- int del;
- } *routes;
- int N = 0, Max = 1;
- char confing_departurepoint[20];
- char departurepoint_BD[20];
- void read_line(char str[], int strLen);
- void insert();
- void print();
- void sCfgFiledeparturepoint(char*);
- void change();
- void del();
- void remove();
- void departurepoint();
- void destinationpoint();
- void size(int);
- void save();
- void donwload();
- void File_oppened();
- void clear();
- void control_cfg();
- int main(int argc, char *argv[])
- {
- int code;
- routes = (route *)malloc(15 * sizeof(route));
- setlocale(LC_CTYPE, "Rus");
- sCfgFiledeparturepoint(argv[0]);
- control_cfg();
- File_oppened();
- printf("\t 1 - Добавление записи\n");
- printf("\t 2 - Удаление записи\n");
- printf("\t 3 - Вывод БД\n");
- printf("\t 4 - Изменение БД\n");
- printf("\t 5 - Поиск по начальному пункту\n");
- printf("\t 6 - Поиск по конечному пункту\n");
- printf("\t 7 - Востановить запись\n");
- printf("\t 8 - Сохранение БД\n");
- printf("\t 9 - Загрузка БД\n");
- printf("\t10 - Очистить БД\n");
- printf("\t11 - Завершение работы\n");
- for (;;)
- {
- printf(" \t Выберите операцию: ");
- scanf("%i", &code);
- while (getchar() != '\n');
- switch (code) {
- case 1: insert(); break;
- case 2: del(); break;
- case 3: print(); break;
- case 4: change(); break;
- case 5: departurepoint(); break;
- case 6: destinationpoint(); break;
- case 7: remove(); break;
- case 8: save();
- File_oppened(); break;
- case 9: donwload();
- File_oppened(); break;
- case 10: clear();
- File_oppened(); break;
- case 11: return 0;
- default: printf("Ошибка\n");
- }
- }
- }
- void size(int M)
- {
- realloc(routes, M*sizeof(route));
- Max += M;
- }
- void read_line(char str[], int strLen) {
- setlocale(0, ".866");
- int i = 0, ch;
- while (isspace(ch = getchar()));
- while (ch != '\n' && ch != EOF)
- {
- if (i < strLen)
- str[i++] = ch;
- ch = getchar();
- }
- str[i] = '\0';
- setlocale(0, "Rus");
- }
- void insert()
- {
- int i;
- for (i = N; i <= N; i++)
- {
- routes[i].del = 0;
- printf("Введите начальный пункт: ");
- read_line(routes[i].departurepoint, 20);
- printf("Введите конечный пункт: ");
- read_line(routes[i].destinationpoint, 20);
- printf("Введите номер маршрута: ");
- scanf("%i", &routes[i].routenumber);
- }
- N++;
- int M = 10;
- size(M);
- }
- void print()
- {
- int i;
- if (N == 0)
- {
- printf("Error\n");
- return;
- }
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- printf("| № | Начальный пункт| Конечный пункт| № Маршрута|\n");
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- for (i = 0; i < N; i++)
- {
- setlocale(0, ".866");
- if (routes[i].del == 0)
- {
- printf("|%4i |%23s|%23s|%15i|\n", i + 1, routes[i].departurepoint, routes[i].destinationpoint, routes[i].routenumber);
- printf("| |\t\t\t |\t\t\t |\t\t |\n");
- }
- else
- {
- setlocale(LC_ALL, "Rus");
- printf("|%5i|%58s|\n", i + 1, "Записи нет");
- }
- }
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- setlocale(LC_ALL, "Rus");
- }
- void change()
- {
- if (N == 0)
- {
- printf("Error\n");
- return;
- }
- int j = 1, code;
- print();
- printf(" Номер записи: ");
- scanf("%i", &j);
- if (j > N || j <= 0)
- printf("Ошибка. Такой записи нет.\n");
- else
- {
- printf("\n1 - Начальный пункт\n2 - Конечный пункт \n3 - Номер маршрута\n");
- printf("Выберите поле: ");
- scanf("%i", &code);
- switch (code)
- {
- case 1:
- {
- printf("Введите новый начальный пункт: ");
- read_line(routes[j - 1].departurepoint, 20);
- break;
- }
- case 2:
- printf("Введите новый конечный пункт: ");
- read_line(routes[j - 1].destinationpoint, 20);
- break;
- case 3:
- printf("Введите новый номер маршрута: ");
- scanf("%i", &routes[j - 1].routenumber);
- break;
- default: printf("Error\n");
- }
- }
- }
- void del()
- {
- int j;
- printf("Удалить запись №-> ");
- scanf("%i", &j);
- if (j > N || j <= 0)
- printf("Error\n");
- else
- {
- if (routes[j - 1].del == 1)
- printf("Запись была удалена\n");
- else
- {
- routes[j - 1].del = 1;
- }
- }
- }
- void remove()
- {
- int j;
- printf("Востановить звпись №-> ");
- scanf("%i", &j);
- if (j > N || j <= 0)
- {
- printf("Error\n");
- }
- else
- {
- routes[j - 1].del = 0;
- }
- }
- void departurepoint()
- {
- if (N == 0)
- {
- printf("Error\n");
- return;
- }
- int i, k=0;
- printf("Введите начальный пункт: ");
- read_line(routes[0].point, 20);
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- printf("| № | Начальный пункт| Конечный пункт| № Маршрута|\n");
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- for(i=0 ; i<N ; i++){
- if(strcmp(routes[0].point,routes[i].departurepoint)==0){
- setlocale(0, ".866");
- printf("|%4i |%23s|%23s|%15i|\n", i + 1, routes[i].departurepoint, routes[i].destinationpoint, routes[i].routenumber);
- printf("| |\t\t\t |\t\t\t |\t\t |\n");
- setlocale(LC_ALL, "Rus");
- k++;
- }
- }
- if(k==0) printf("| |\t\t\t Нет маршрутов из этого пункта.\t\t\t|\n");
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- }
- void destinationpoint()
- {
- if (N == 0)
- {
- printf("Error\n");
- return;
- }
- int i, k=0;
- printf("Введите конечный пункт: ");
- read_line(routes[1].point, 20);
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- printf("| № | Начальный пункт| Конечный пункт| № Маршрута|\n");
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- for(i=0 ; i<N ; i++){
- if(strcmp(routes[1].point,routes[i].destinationpoint)==0){
- setlocale(0, ".866");
- printf("|%4i |%23s|%23s|%15i|\n", i + 1, routes[i].departurepoint, routes[i].destinationpoint, routes[i].routenumber);
- printf("| |\t\t\t |\t\t\t |\t\t |\n");
- setlocale(LC_ALL, "Rus");
- k++;
- }
- }
- if(k==0) printf("| |\t\t\t Нет маршрутов в этот пункт.\t\t\t|\n");
- printf("+-----+-----------------------+-----------------------+---------------+\n");
- }
- void control_cfg()
- {
- FILE *fp;
- int i = 0;
- char len[20] = {};
- char len_1[2][20] = {};
- char *mss;
- if ((fp = fopen(confing_departurepoint, "rb+")) != NULL)
- {
- fread(&len, 20, 1, fp);
- mss = strtok(len, "DB = ");
- while (mss != NULL)
- {
- strcat(len_1[i], mss);
- mss = strtok(NULL, "DB = ");
- }
- strcpy(departurepoint_BD, len_1[0]);
- }
- }
- void File_oppened()
- {
- FILE *fp;
- int i;
- char b_d[64] = {};
- if (strlen(departurepoint_BD) == NULL)
- strcpy(departurepoint_BD, "new_db.dat");
- strncat(b_d, departurepoint_BD, strlen(departurepoint_BD));
- if ((fp = fopen(confing_departurepoint, "r+b")) == NULL)
- {
- fp = fopen(confing_departurepoint, "w+b");
- printf(" %s создан.\n", confing_departurepoint);
- fprintf(fp, b_d);
- fclose(fp);
- printf(" Файл базы данных '%s'\n", departurepoint_BD);
- fp = fopen(departurepoint_BD, "w+b");
- fclose(fp);
- }
- else {
- if ((fp = fopen(departurepoint_BD, "rb")) == NULL)
- {
- printf(" Файл базы данных '%s'\n", departurepoint_BD);
- fp = fopen(departurepoint_BD, "wb");
- }
- fclose(fp);
- fopen(confing_departurepoint, "wb+");
- fprintf(fp, b_d);
- fclose(fp);
- fp = fopen(departurepoint_BD, "rb+");
- for (i = 0; feof(fp) == 0; i++)
- {
- fread(&routes[i], sizeof(route), 1, fp);
- N = i;
- }
- fclose(fp);
- }
- }
- void save()
- {
- int i;
- FILE *fp;
- printf("Сохранение БД.\n");
- printf("Введите имя базы данных: ");
- read_line(departurepoint_BD, 20);
- if (strstr(departurepoint_BD, ".dat") == NULL)
- strcat(departurepoint_BD, ".dat");
- if ((fp = fopen(departurepoint_BD, "r+b")) == NULL){
- fp = fopen(departurepoint_BD, "w+b");
- for (i = 0; i < N; i++)
- {
- fwrite(&routes[i], sizeof(route), 1, fp);
- }
- printf("Данные записаны в файл %s.\n", departurepoint_BD);
- fclose(fp);
- }
- else
- {
- printf("Файл уже существует.\n");
- }
- }
- void sCfgFiledeparturepoint(char* e)
- {
- int i = 0;
- char mas[15][50] = {};
- char *mss;
- mss = strtok(e, "\\");
- while (mss != NULL)
- {
- strcpy(mas[i], mss);
- i++;
- mss = strtok(NULL, "\\");
- }
- strncat(confing_departurepoint, mas[i - 1], strlen(mas[i - 1]) - 4);
- strcat(confing_departurepoint, ".cfg");
- }
- void donwload()
- {
- FILE *fp;
- int i;
- printf("Введите имя базы данных-> ");
- read_line(departurepoint_BD, 20);
- if (strstr(departurepoint_BD, ".dat") == NULL)
- strcat(departurepoint_BD, ".dat");
- if ((fp = fopen(departurepoint_BD, "r+b")) == NULL)
- {
- printf("Файла с таким именем нет.");
- fclose(fp);
- }
- else
- {
- fclose(fp);
- fp = fopen(departurepoint_BD, "r+b");
- for (i = 0; feof(fp) == 0; i++)
- {
- fread(&routes[i], sizeof(route), 1, fp);
- N = i;
- }
- printf("БД '%s' загружена\n", departurepoint_BD);
- }
- fclose(fp);
- }
- void clear()
- {
- FILE *fp;
- if ((fp = fopen(departurepoint_BD, "w+b")) == NULL)
- printf("Файл не открыт\n");
- else
- printf("База данных с именем '%s' очищена\n", departurepoint_BD);
- fclose(fp);
- }
Add Comment
Please, Sign In to add comment