Advertisement
Zennoma

laba17

Apr 18th, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <string.h>
  6.  
  7.  
  8. struct list
  9. {
  10.     char* string= new char[128];
  11.     list *next;
  12.     list *prev;
  13. };
  14.  
  15. FILE fill(FILE* file)
  16. {
  17.     rewind(file);
  18.     char* string;
  19.     string = new char[128];
  20.     gets_s(string, 128);
  21.     while (gets_s(string, 128), strlen(string) != 0)
  22.     {
  23.         fputs(string, file);
  24.         fputs("\n", file);
  25.     }
  26.     delete[] string;
  27.     return (*file);
  28. }
  29.  
  30. FILE show(FILE* file)
  31. {
  32.     rewind(file);
  33.     char* string;
  34.     string = new char[128];
  35.     while (fgets(string, 128, file) > 0)
  36.         puts(string);
  37.     delete[] string;
  38.     return(*file);
  39. }
  40.  
  41.  
  42.  
  43. void listprint(list* first)
  44. {
  45.    
  46.     while (first != NULL)
  47.     {
  48.         puts(first->string);
  49.         first = first->next;
  50.     }
  51. }
  52. void list_delete(list *& head)
  53. {
  54.     list* q = head;
  55.     while (head != NULL)
  56.     {
  57.         head = head->next;
  58.         delete q;
  59.         q = head;
  60.  
  61.     }
  62.  
  63. }
  64. void fromfiletolist(FILE*file,list*&head,list*&tail)
  65. {
  66.     char* strfile;
  67.     strfile = new char[128];
  68.     rewind(file);
  69.     list * now,*p;
  70.     head = new list;
  71.     fgets(strfile, 128, file);
  72.     strcpy(head->string, strfile);
  73.     head->next = NULL; // указатель на следующий узел
  74.     head->prev = NULL; // указатель на предыдущий узел
  75.     p = head;
  76.     while (fgets(strfile, 128, file) > 0)
  77.     {
  78.         now = new list;
  79.         strcpy(now->string, strfile);
  80.         p->next = now;
  81.         now->prev = p;
  82.         p = p->next;
  83.         p->next = NULL;
  84.     }
  85.     tail = p;
  86.    
  87. }
  88. void sort(FILE* file, list*& head, list*& tail)
  89. {
  90.     rewind(file);
  91.     int fl;
  92.     list* n,*p,*q;
  93.     char* strfile;
  94.     strfile = new char[128];
  95.     while (fgets(strfile, 128, file) > 0)
  96.     {
  97.             if (head == NULL) //формирование
  98.             {
  99.                 head = new list;
  100.                 strcpy(head->string, strfile);
  101.                 head->next = NULL;
  102.                 head->prev = NULL;
  103.                 tail = head;
  104.             }
  105.             else
  106.             {
  107.                 n = new list;
  108.                 strcpy(n->string, strfile);
  109.                 if (strlen(n->string) > strlen(head->string)) //вставка в начало списка
  110.                 {
  111.                     n->next = head;
  112.                     head->prev = n;
  113.                     head = n;
  114.                 }
  115.                 else
  116.                 {
  117.                     fl = 0;
  118.                     p = head;
  119.                     q = head;
  120.                     while ((q = q->next) != NULL && !fl)
  121.                     {
  122.                         if (strlen(n->string) > strlen(q->string))//вставка n перед q
  123.                         {
  124.                             n->next = p->next;
  125.                             q->prev = n->prev;
  126.                             p->next = n;
  127.                             q->prev = n;
  128.                             fl = 1;
  129.                         }
  130.                         else p = q; //н после q
  131.                     }
  132.                     if (!fl)
  133.                     {
  134.                         n->next = NULL;
  135.                         p->next = n;
  136.                         n->prev = n;
  137.                         tail = n;
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.    
  143. }
  144.  
  145. void task(list*& head, list*& tail)
  146. {
  147.     list* q;
  148.     q = head->next;
  149.     delete head;
  150.     head = q;
  151.     head->prev = NULL;
  152.     q = tail->prev;
  153.     delete tail;
  154.     tail = q;
  155.     tail->next = NULL;
  156. }
  157.        
  158.  
  159. int main()
  160. {
  161.  
  162.     FILE* file;
  163.     char* filename;
  164.     filename = new char[30];
  165.     puts("Input filename");
  166.     gets_s(filename, 30);
  167.     fopen_s(&file, filename, "r+");
  168.  
  169.     if (file == NULL)
  170.     {
  171.         int f;
  172.         puts("file not found. Create new? 1-yes 0-no");
  173.         scanf_s("%d", &f);
  174.         if (f == 0)
  175.             return(0);
  176.         else
  177.         {
  178.             fopen_s(&file, filename, "w+");
  179.             puts("input strings into the file");
  180.             fill(file);
  181.         }
  182.     }
  183.     puts("=====content of the file=====");
  184.     show(file);//файл сформирован или прочитан
  185.     list* tail=NULL;
  186.     list* head = NULL;
  187.     //fromfiletolist(file,head,tail);
  188.     sort(file, head, tail);
  189.     puts("=====formed and sorted list =====");
  190.     listprint(head);
  191.     task(head, tail);
  192.     puts("=====new list =====");
  193.     listprint(head);
  194.     list_delete(head);
  195.     puts("=====memory clear =====");
  196.     listprint(head);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement