Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NO_FILENAMES -1
  5. #define FILE_NOT_EXIST -2
  6. #define FILE_READ_ERROR -3
  7.  
  8. /* структура элемента списка */
  9. typedef struct node node_t;
  10. struct node
  11. {
  12.     int data;
  13.     node_t *next;
  14. };
  15.  
  16.  
  17. /* создать новый элемент списка */
  18. node_t *create_node(int data)
  19. {
  20.     node_t *nd = malloc(sizeof(node_t));
  21.  
  22.     if (nd)
  23.     {
  24.         nd->data = data;
  25.         nd->next = NULL;
  26.         return nd;
  27.     }
  28.     else
  29.         return NULL;
  30. }
  31.  
  32.  
  33. /* добавить элемент в конец */
  34. node_t *add_end(node_t *head, node_t *nd)
  35. {
  36.     if (head == NULL || nd == NULL)
  37.         return NULL;
  38.  
  39.     node_t *current = head;
  40.     if (!head)
  41.         return nd;
  42.  
  43.     for (; current->next; current = current->next);
  44.     current->next = nd;
  45.  
  46.     return head;
  47. }
  48.  
  49. /* освобождение памяти под список */
  50. void free_list(node_t *head)
  51. {
  52.     node_t *temp1, *temp2;
  53.  
  54.     for (temp1 = head; temp1 != NULL; temp1 = temp2)
  55.     {
  56.         temp2 = temp1->next;
  57.         free(temp1);
  58.     }
  59. }
  60.  
  61.  
  62. /* чтение списка из файла */
  63. node_t *read_list(FILE *f)
  64. {
  65.     int buff;
  66.     node_t *head, *new;
  67.  
  68.     int error = fscanf(f, "%d", &buff);
  69.     if (error != 1)
  70.         return NULL;
  71.  
  72.     head = create_node(buff);
  73.  
  74.     error = fscanf(f, "%d", &buff);
  75.     while (error != EOF)
  76.     {
  77.         new = create_node(buff);
  78.         add_end(head, new);
  79.         error = fscanf(f, "%d", &buff);
  80.     }
  81.  
  82.     return head;
  83. }
  84.  
  85.  
  86. /* меняет местами наибольший и наименьший */
  87. void swap_minmax(node_t **head)
  88. {
  89.     node_t *min = *head, *max = *head;
  90.     node_t *pmin = NULL, *pmax = NULL;
  91.     node_t *temp;
  92.  
  93.     for (temp = *head; temp->next != NULL; temp = temp->next)
  94.     {
  95.         if (temp->next->data > max->data)
  96.         {
  97.             pmax = temp;
  98.             max = temp->next;
  99.         }
  100.  
  101.         if (temp->next->data < min->data)
  102.         {
  103.             pmin = temp;
  104.             min = temp->next;
  105.         }
  106.  
  107.     }
  108.  
  109.  
  110.     if (pmin == NULL)
  111.     {
  112.         max->next = min->next;
  113.         *head = max;
  114.     }
  115.  
  116.     else
  117.     {
  118.         temp = pmin->next;
  119.         pmin->next = pmax->next;
  120.         pmax->next = temp;
  121.  
  122.         temp = max->next;
  123.         max->next = min->next;
  124.         min->next = temp;
  125.     }
  126. }
  127.  
  128. /* печать списка в файл */
  129. void print_list(node_t *head, FILE *f)
  130. {
  131.     for (; head != NULL; head = head->next)
  132.         fprintf(f, "%d ", head->data);
  133. }
  134.  
  135.  
  136. /* процесс */
  137. int proc(FILE *f1, FILE *f2)
  138. {
  139.     node_t *head = read_list(f1);
  140.     if (head == NULL)
  141.         return FILE_READ_ERROR;
  142.  
  143.  
  144.     swap_minmax(&head);
  145.     print_list(head, f2);
  146.     free_list(head);
  147.  
  148.     return 0;
  149. }
  150.  
  151.  
  152. int main(int argc, char **argv)
  153. {
  154.     FILE *f1, *f2;
  155.     int error;
  156.  
  157.     if (argc < 2)
  158.     {
  159.         printf("Not enough arguments!\n");
  160.         return NO_FILENAMES;
  161.     }
  162.  
  163.     f1 = fopen(argv[1], "r");
  164.     if (f1 == NULL)
  165.     {
  166.         printf("File %s does not exist!\n", argv[1]);
  167.         return FILE_NOT_EXIST;
  168.     }
  169.  
  170.     f2 = fopen(argv[2], "w");
  171.  
  172.     error = proc(f1, f2);
  173.     if (error == FILE_READ_ERROR)
  174.     {
  175.         printf("File read error!\n");
  176.         return FILE_READ_ERROR;
  177.     }
  178.  
  179.     printf("New file succesfully created\n");
  180.  
  181.     fclose(f1);
  182.     fclose(f2);
  183.  
  184.     return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement