Advertisement
m4ly

C=4

Jan 3rd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.42 KB | None | 0 0
  1. /*
  2. Author: Dawid Mocek <[email protected]>
  3.  
  4. kod dla c = 4
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9.  
  10. #define AB_SIZE 23
  11. static const char ab[] = { 'A', 'B', 'C', 'D', 'E',
  12.                            'F', 'G', 'H', 'I', 'J',
  13.                            'K', 'L', 'M', 'N', 'O',
  14.                            'P', 'R', 'S', 'T', 'U',
  15.                            'W', 'Y', 'Z' };
  16.  
  17. static const char chars[] = { '(', ')', '+' };
  18.  
  19.  
  20. struct node_s {
  21.     char *record;
  22.     size_t record_size;
  23.     struct node_s *next;
  24. };
  25.  
  26.  
  27. /*
  28. * Obsluga erroru alokacji w linedlist
  29. *
  30. */
  31. void error_alloc_node(struct node_s **node, const char *msg) {
  32.     free(*node);
  33.     fprintf(stderr, "struct node* memory allocation error: %s", msg);
  34.     *node = NULL;
  35.     exit(EXIT_FAILURE);
  36. }
  37.  
  38. /*
  39.  * Dodaje noda na początek listy jednokierunkowej
  40.  * Bez sortowania
  41.  * */
  42. void lpush_front(struct node_s **head, char *rec, size_t rec_size) {
  43.  
  44.     // Jeśli początek jest null to glowa = nowy element
  45.     if (*head == NULL) {
  46.         *head = (struct node_s *)malloc(sizeof(struct node_s));
  47.         if (!*head) error_alloc_node(head, "head, insert_front()");
  48.         (*head)->record = rec;
  49.         (*head)->record_size = rec_size;
  50.         (*head)->next = NULL;
  51.     }
  52.     else {
  53.         struct node_s *n = (struct node_s *)malloc(sizeof(struct node_s));
  54.         if (!n) error_alloc_node(&n, "n, insert_front()");
  55.         n->record_size = rec_size;
  56.         n->record = rec;
  57.         n->next = *head;
  58.         *head = n;
  59.     }
  60. }
  61.  
  62. /*
  63.  * Dodaje noda na koniec listy jednokierunkowej
  64.  * Bez sortowania
  65.  */
  66. void lpush_back(struct node_s **head, char *rec, size_t record_size) {
  67.     // Jeśli początek jest null to glowa = nowy element
  68.     if (*head == NULL) {
  69.         *head = (struct node_s *)malloc(sizeof(struct node_s));
  70.         if (!*head) error_alloc_node(head, "nowy, insert_back()");
  71.         (*head)->record = rec;
  72.         (*head)->record_size = record_size;
  73.         (*head)->next = NULL;
  74.     }
  75.     else {
  76.         struct node_s *node = *head;
  77.         for (; node->next != NULL; node = node->next);
  78.         node->next = (struct node_s *)malloc(sizeof(struct node_s));
  79.         if (!node->next) error_alloc_node(&node->next, "node->next, insert_back()");
  80.         node->next->next = NULL;
  81.         node->next->record = rec;
  82.         node->next->record_size = record_size;
  83.       }
  84. }
  85.  
  86. /**
  87.  * Drukuj liste
  88.  */
  89. void lprint(struct node_s *head) {
  90.     for (; head != NULL; head = head->next)
  91.         printf("%s %d | %p -> %p\n", head->record, (int)(head->record_size - sizeof(char)), head, head->next);
  92. }
  93.  
  94.  
  95.  
  96. /**
  97.  * Usuwa liste z pamieci
  98.  *
  99.  */
  100. void ldestroy(struct node_s **head) {
  101.     struct node_s *tmp = NULL;
  102.     for (; tmp != NULL;) {
  103.         tmp = (*head)->next;
  104.         //Zwlania węzeł
  105.         free((*head)->record);
  106.         (*head)->record = NULL;
  107.         free((*head));
  108.         (*head) = NULL;
  109.         *head = tmp;
  110.     }
  111.     *head = NULL;
  112. }
  113.  
  114. /**
  115.  * Drukuj ostatnie permutacje
  116.  */
  117. void lprintp(struct node_s *head, int c) {
  118.     struct node_s *tmp = NULL;
  119.     int i = 0, k = 0, j = 0;
  120.  
  121.     for( i = 0 ; i < c; i++) {
  122.         fprintf(stdout, "(%s", head->record);
  123.         tmp = head;
  124.         k = (2*c - 2*i)-1;
  125.         //printf("k: %d\n", k);
  126.         for ( j = 0; j < k ; j++) {
  127.             tmp = tmp->next;
  128.         }
  129.         if(tmp != NULL)
  130.             fprintf(stdout, "+%s)\n", tmp->record);
  131.         else {
  132.             perror("zla matematyka !");
  133.             ldestroy(&head);
  134.             exit(EXIT_FAILURE);
  135.         }
  136.         if(head->next != NULL)
  137.             head = head->next;
  138.     }
  139. }
  140.  
  141. int main(int argc, char **argv) {
  142.     int i, j, k, l, m,n,o, offset, c = 4;
  143.     size_t record_size, new_record_size, newest_record_size;
  144.     char new_glue[4], newest_glue[4], newest__glue[4], record[6];
  145.     struct node_s *list = NULL;
  146.  
  147.     int is_even = ((c + 1) % 2 == 0) ? 1 : 0;
  148.  
  149.     if((c + 1) >= AB_SIZE) {
  150.         fprintf(stderr, "Za malo literek (w) AlfaBecie");
  151.         exit(EXIT_FAILURE);
  152.     }
  153.  
  154.  
  155.     // wyzerujmey sobie pamiec
  156.     memset(&record, 0, 6 * sizeof(char));
  157.     memset(&new_glue, 0, 4 * sizeof(char));
  158.     memset(&newest_glue, 0, 4 * sizeof(char));
  159.     memset(&newest__glue, 0, 4 * sizeof(char));
  160.  
  161.     // Bob budowniczy..
  162.  
  163.     // ustawimy poczatkowe wartosc - znaki ( ) i +
  164.     record[0] = chars[0];
  165.     record[2] = chars[2];
  166.     record[4] = chars[1];
  167.  
  168.     new_glue[0] = chars[2];
  169.     new_glue[2] = chars[1];
  170.  
  171.     newest_glue[0] = chars[2];
  172.     newest_glue[2] = chars[1];
  173.  
  174.  
  175.     newest__glue[0] = chars[2];
  176.     newest__glue[2] = chars[1];
  177.  
  178.     for(i = 0; i < (c + 1); i++) {
  179.         for(k = c; k > i; k--) {
  180.             record[1] = ab[i];
  181.             record[3] = ab[k];
  182.             if(is_even) {
  183.                 if( k % c  == 0)
  184.                     lpush_back(&list, strdup(record), sizeof(record) * sizeof(char));
  185.                 else if(k % 2 == 0)
  186.                     lpush_back(&list, strdup(record), sizeof(record) * sizeof(char));
  187.                 else
  188.                     lpush_front(&list, strdup(record), sizeof(record) * sizeof(char));
  189.             }
  190.              fprintf(stdout, "%s\n", record);
  191.              int z =0;
  192.             // doklej nastpne  char y za wyjatkiem wykorzystanych powyzej
  193.                 for(l = 0; l < (c + 1); l++) {
  194.                     if((ab[l] != ab[i]) && (ab[l] != ab[k])) {
  195.                         new_glue[1] = ab[l];
  196.                         fprintf(stdout, "%s%s\n", record, new_glue);
  197.                         for(j = 0; j < (c + 1); j++) {
  198.                             if((ab[j] != ab[l]) && (ab[j] != ab[i]) && (ab[j] != ab[k])) {
  199.                                 newest_glue[1] = ab[j];
  200.                                 fprintf(stdout, "%s%s%s\n",record,new_glue, newest_glue);
  201.                                 for(m = 0;  m < (c + 1); m++) {
  202.                                     if((ab[m] != ab[j]) && (ab[m] != ab[l]) && (ab[m] != ab[i]) && (ab[m] != ab[k])) {
  203.                                         newest__glue[1] = ab[m];
  204.                                         fprintf(stdout, "%s%s%s%s\n",record,new_glue, newest_glue, newest__glue);
  205.  
  206.                                     }
  207.                                 }
  208.  
  209.  
  210.                             }
  211.                         }
  212.                     }
  213.  
  214.                 }
  215.         }
  216.    }
  217.    if(is_even) {
  218.         lprintp(list, c);
  219.         ldestroy(&list);
  220.     }
  221.     return EXIT_SUCCESS;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement