golovanovd

rgr2

May 23rd, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.96 KB | None | 0 0
  1. /*************************************************/
  2. /*           Расчетно графическая работа         */
  3. /*                     Вариант 4                */
  4. /*         Выполнил студент гр. 515-А            */
  5. /*                 Голованов Дмитрий             */
  6. /*************************************************/
  7.  
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <iostream>
  12. #include <locale.h>
  13. #include <string.h>
  14. struct route
  15. {
  16.     char departurepoint[20];
  17.     char destinationpoint[20];
  18.     char point[20];
  19.     int routenumber;
  20.     int del;
  21. } *routes;
  22. int N = 0, Max = 1;
  23. char confing_departurepoint[20];
  24. char departurepoint_BD[20];
  25. void read_line(char str[], int strLen);
  26. void insert();
  27. void print();
  28. void sCfgFiledeparturepoint(char*);
  29. void change();
  30. void del();
  31. void remove();
  32. void departurepoint();
  33. void destinationpoint();
  34. void size(int);
  35. void save();
  36. void donwload();
  37. void File_oppened();
  38. void clear();
  39. void control_cfg();
  40.  
  41. int main(int argc, char *argv[])
  42. {
  43.     int code;
  44.     routes = (route *)malloc(15 * sizeof(route));
  45.     setlocale(LC_CTYPE, "Rus");
  46.  
  47.     sCfgFiledeparturepoint(argv[0]);
  48.     control_cfg();
  49.     File_oppened();
  50.  
  51.     printf("\t 1 - Добавление записи\n");
  52.     printf("\t 2 - Удаление записи\n");
  53.     printf("\t 3 - Вывод БД\n");
  54.     printf("\t 4 - Изменение БД\n");
  55.     printf("\t 5 - Поиск по начальному пункту\n");
  56.     printf("\t 6 - Поиск по конечному пункту\n");
  57.     printf("\t 7 - Востановить запись\n");
  58.     printf("\t 8 - Сохранение БД\n");
  59.     printf("\t 9 - Загрузка БД\n");
  60.     printf("\t10 - Очистить БД\n");
  61.     printf("\t11 - Завершение работы\n");
  62.  
  63.     for (;;)
  64.     {
  65.         printf(" \t  Выберите операцию: ");
  66.         scanf("%i", &code);
  67.         while (getchar() != '\n');
  68.         switch (code) {
  69.         case 1: insert(); break;
  70.         case 2: del(); break;
  71.         case 3: print(); break;
  72.         case 4: change(); break;
  73.         case 5: departurepoint(); break;
  74.         case 6: destinationpoint(); break;
  75.         case 7: remove(); break;
  76.         case 8: save();
  77.             File_oppened(); break;
  78.         case 9: donwload();
  79.             File_oppened(); break;
  80.         case 10: clear();
  81.             File_oppened(); break;
  82.         case 11: return 0;
  83.         default: printf("Ошибка\n");
  84.         }
  85.     }
  86. }
  87.  
  88. void size(int M)
  89. {
  90.     realloc(routes, M*sizeof(route));
  91.     Max += M;
  92. }
  93.  
  94. void read_line(char str[], int strLen) {
  95.     setlocale(0, ".866");
  96.     int i = 0, ch;
  97.     while (isspace(ch = getchar()));
  98.     while (ch != '\n' && ch != EOF)
  99.     {
  100.         if (i < strLen)
  101.             str[i++] = ch;
  102.         ch = getchar();
  103.     }
  104.     str[i] = '\0';
  105.     setlocale(0, "Rus");
  106. }
  107.  
  108. void insert()
  109. {
  110.     int i;
  111.     for (i = N; i <= N; i++)
  112.     {
  113.         routes[i].del = 0;
  114.         printf("Введите начальный пункт: ");
  115.         read_line(routes[i].departurepoint, 20);
  116.         printf("Введите конечный пункт: ");
  117.         read_line(routes[i].destinationpoint, 20);
  118.         printf("Введите номер маршрута: ");
  119.         scanf("%i", &routes[i].routenumber);
  120.  
  121.  
  122.     }
  123.     N++;
  124.     int M = 10;
  125.     size(M);
  126. }
  127. void print()
  128. {
  129.     int  i;
  130.     if (N == 0)
  131.     {
  132.         printf("Error\n");
  133.         return;
  134.     }
  135.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  136.     printf("|   № |        Начальный пункт|         Конечный пункт|     № Маршрута|\n");
  137.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  138.     for (i = 0; i < N; i++)
  139.     {
  140.         setlocale(0, ".866");
  141.         if (routes[i].del == 0)
  142.         {
  143.  
  144.             printf("|%4i |%23s|%23s|%15i|\n", i + 1, routes[i].departurepoint, routes[i].destinationpoint, routes[i].routenumber);
  145.             printf("|     |\t\t\t      |\t\t\t      |\t\t      |\n");
  146.         }
  147.         else
  148.         {
  149.             setlocale(LC_ALL, "Rus");
  150.             printf("|%5i|%58s|\n", i + 1, "Записи нет");
  151.         }
  152.     }
  153.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  154.     setlocale(LC_ALL, "Rus");
  155. }
  156. void change()
  157. {
  158.     if (N == 0)
  159.     {
  160.         printf("Error\n");
  161.         return;
  162.     }
  163.  
  164.     int j = 1, code;
  165.     print();
  166.     printf(" Номер записи: ");
  167.     scanf("%i", &j);
  168.     if (j > N || j <= 0)
  169.         printf("Ошибка. Такой записи нет.\n");
  170.     else
  171.     {
  172.         printf("\n1 - Начальный пункт\n2 - Конечный пункт \n3 - Номер маршрута\n");
  173.         printf("Выберите поле: ");
  174.         scanf("%i", &code);
  175.         switch (code)
  176.         {
  177.         case 1:
  178.         {
  179.                 printf("Введите новый начальный пункт: ");
  180.                 read_line(routes[j - 1].departurepoint, 20);
  181.                 break;
  182.         }
  183.  
  184.         case 2:
  185.                 printf("Введите новый конечный пункт: ");
  186.                 read_line(routes[j - 1].destinationpoint, 20);
  187.                 break;
  188.         case 3:
  189.             printf("Введите новый номер маршрута: ");
  190.             scanf("%i", &routes[j - 1].routenumber);
  191.             break;
  192.         default: printf("Error\n");
  193.         }
  194.     }
  195. }
  196. void del()
  197. {
  198.     int j;
  199.     printf("Удалить запись №-> ");
  200.     scanf("%i", &j);
  201.     if (j > N || j <= 0)
  202.         printf("Error\n");
  203.     else
  204.     {
  205.         if (routes[j - 1].del == 1)
  206.             printf("Запись была удалена\n");
  207.         else
  208.         {
  209.             routes[j - 1].del = 1;
  210.         }
  211.     }
  212. }
  213. void remove()
  214. {
  215.     int j;
  216.     printf("Востановить звпись №-> ");
  217.     scanf("%i", &j);
  218.     if (j > N || j <= 0)
  219.     {
  220.         printf("Error\n");
  221.     }
  222.     else
  223.     {
  224.         routes[j - 1].del = 0;
  225.     }
  226. }
  227.  
  228. void departurepoint()
  229. {
  230.     if (N == 0)
  231.     {
  232.         printf("Error\n");
  233.         return;
  234.     }
  235.     int i, k=0;
  236.  
  237.     printf("Введите начальный пункт: ");
  238.     read_line(routes[0].point, 20);
  239.  
  240.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  241.     printf("|   № |        Начальный пункт|         Конечный пункт|     № Маршрута|\n");
  242.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  243.     for(i=0 ; i<N ; i++){
  244.  
  245.         if(strcmp(routes[0].point,routes[i].departurepoint)==0){
  246.             setlocale(0, ".866");
  247.             printf("|%4i |%23s|%23s|%15i|\n", i + 1, routes[i].departurepoint, routes[i].destinationpoint, routes[i].routenumber);
  248.             printf("|     |\t\t\t      |\t\t\t      |\t\t      |\n");
  249.  
  250.             setlocale(LC_ALL, "Rus");
  251.             k++;
  252.         }
  253.     }
  254.  
  255.     if(k==0) printf("|     |\t\t\t Нет маршрутов из этого пункта.\t\t\t|\n");
  256.  
  257.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  258. }
  259.  
  260. void destinationpoint()
  261. {
  262.     if (N == 0)
  263.     {
  264.         printf("Error\n");
  265.         return;
  266.     }
  267.     int i, k=0;
  268.  
  269.     printf("Введите конечный пункт: ");
  270.     read_line(routes[1].point, 20);
  271.  
  272.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  273.     printf("|   № |        Начальный пункт|         Конечный пункт|     № Маршрута|\n");
  274.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  275.     for(i=0 ; i<N ; i++){
  276.  
  277.         if(strcmp(routes[1].point,routes[i].destinationpoint)==0){
  278.             setlocale(0, ".866");
  279.             printf("|%4i |%23s|%23s|%15i|\n", i + 1, routes[i].departurepoint, routes[i].destinationpoint, routes[i].routenumber);
  280.             printf("|     |\t\t\t      |\t\t\t      |\t\t      |\n");
  281.  
  282.             setlocale(LC_ALL, "Rus");
  283.             k++;
  284.         }
  285.     }
  286.  
  287.     if(k==0) printf("|     |\t\t\t Нет маршрутов в этот пункт.\t\t\t|\n");
  288.  
  289.     printf("+-----+-----------------------+-----------------------+---------------+\n");
  290. }
  291.  
  292. void control_cfg()
  293. {
  294.     FILE *fp;
  295.     int i = 0;
  296.     char len[20] = {};
  297.     char len_1[2][20] = {};
  298.     char *mss;
  299.     if ((fp = fopen(confing_departurepoint, "rb+")) != NULL)
  300.     {
  301.         fread(&len, 20, 1, fp);
  302.         mss = strtok(len, "DB = ");
  303.         while (mss != NULL)
  304.         {
  305.             strcat(len_1[i], mss);
  306.             mss = strtok(NULL, "DB = ");
  307.         }
  308.         strcpy(departurepoint_BD, len_1[0]);
  309.     }
  310. }
  311. void File_oppened()
  312. {
  313.     FILE *fp;
  314.     int i;
  315.     char b_d[64] = {};
  316.     if (strlen(departurepoint_BD) == NULL)
  317.         strcpy(departurepoint_BD, "new_db.dat");
  318.     strncat(b_d, departurepoint_BD, strlen(departurepoint_BD));
  319.     if ((fp = fopen(confing_departurepoint, "r+b")) == NULL)
  320.     {
  321.         fp = fopen(confing_departurepoint, "w+b");
  322.         printf(" %s создан.\n", confing_departurepoint);
  323.         fprintf(fp, b_d);
  324.         fclose(fp);
  325.         printf(" Файл базы данных '%s'\n", departurepoint_BD);
  326.         fp = fopen(departurepoint_BD, "w+b");
  327.         fclose(fp);
  328.     }
  329.     else {
  330.         if ((fp = fopen(departurepoint_BD, "rb")) == NULL)
  331.         {
  332.             printf(" Файл базы данных '%s'\n", departurepoint_BD);
  333.             fp = fopen(departurepoint_BD, "wb");
  334.         }
  335.         fclose(fp);
  336.         fopen(confing_departurepoint, "wb+");
  337.         fprintf(fp, b_d);
  338.         fclose(fp);
  339.         fp = fopen(departurepoint_BD, "rb+");
  340.         for (i = 0; feof(fp) == 0; i++)
  341.         {
  342.             fread(&routes[i], sizeof(route), 1, fp);
  343.             N = i;
  344.         }
  345.         fclose(fp);
  346.     }
  347. }
  348. void save()
  349. {
  350.     int i;
  351.     FILE *fp;
  352.     printf("Сохранение БД.\n");
  353.     printf("Введите имя базы данных: ");
  354.     read_line(departurepoint_BD, 20);
  355.     if (strstr(departurepoint_BD, ".dat") == NULL)
  356.         strcat(departurepoint_BD, ".dat");
  357.     if ((fp = fopen(departurepoint_BD, "r+b")) == NULL){
  358.         fp = fopen(departurepoint_BD, "w+b");
  359.         for (i = 0; i < N; i++)
  360.         {
  361.             fwrite(&routes[i], sizeof(route), 1, fp);
  362.         }
  363.         printf("Данные записаны в файл %s.\n", departurepoint_BD);
  364.         fclose(fp);
  365.     }
  366.     else
  367.     {
  368.         printf("Файл уже существует.\n");
  369.     }
  370. }
  371. void sCfgFiledeparturepoint(char* e)
  372. {
  373.     int i = 0;
  374.     char mas[15][50] = {};
  375.     char *mss;
  376.     mss = strtok(e, "\\");
  377.     while (mss != NULL)
  378.     {
  379.         strcpy(mas[i], mss);
  380.         i++;
  381.         mss = strtok(NULL, "\\");
  382.     }
  383.     strncat(confing_departurepoint, mas[i - 1], strlen(mas[i - 1]) - 4);
  384.     strcat(confing_departurepoint, ".cfg");
  385. }
  386. void donwload()
  387. {
  388.     FILE *fp;
  389.     int i;
  390.     printf("Введите имя базы данных-> ");
  391.     read_line(departurepoint_BD, 20);
  392.     if (strstr(departurepoint_BD, ".dat") == NULL)
  393.         strcat(departurepoint_BD, ".dat");
  394.     if ((fp = fopen(departurepoint_BD, "r+b")) == NULL)
  395.     {
  396.         printf("Файла с таким именем нет.");
  397.         fclose(fp);
  398.     }
  399.     else
  400.     {
  401.         fclose(fp);
  402.         fp = fopen(departurepoint_BD, "r+b");
  403.         for (i = 0; feof(fp) == 0; i++)
  404.         {
  405.             fread(&routes[i], sizeof(route), 1, fp);
  406.             N = i;
  407.         }
  408.         printf("БД '%s' загружена\n", departurepoint_BD);
  409.     }
  410.     fclose(fp);
  411. }
  412. void clear()
  413. {
  414.     FILE *fp;
  415.     if ((fp = fopen(departurepoint_BD, "w+b")) == NULL)
  416.         printf("Файл не открыт\n");
  417.     else
  418.         printf("База данных с именем '%s' очищена\n", departurepoint_BD);
  419.     fclose(fp);
  420. }
Add Comment
Please, Sign In to add comment