Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Dawid Mocek <[email protected]>
- kod dla c = 4
- */
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #define AB_SIZE 23
- static const char ab[] = { 'A', 'B', 'C', 'D', 'E',
- 'F', 'G', 'H', 'I', 'J',
- 'K', 'L', 'M', 'N', 'O',
- 'P', 'R', 'S', 'T', 'U',
- 'W', 'Y', 'Z' };
- static const char chars[] = { '(', ')', '+' };
- struct node_s {
- char *record;
- size_t record_size;
- struct node_s *next;
- };
- /*
- * Obsluga erroru alokacji w linedlist
- *
- */
- void error_alloc_node(struct node_s **node, const char *msg) {
- free(*node);
- fprintf(stderr, "struct node* memory allocation error: %s", msg);
- *node = NULL;
- exit(EXIT_FAILURE);
- }
- /*
- * Dodaje noda na początek listy jednokierunkowej
- * Bez sortowania
- * */
- void lpush_front(struct node_s **head, char *rec, size_t rec_size) {
- // Jeśli początek jest null to glowa = nowy element
- if (*head == NULL) {
- *head = (struct node_s *)malloc(sizeof(struct node_s));
- if (!*head) error_alloc_node(head, "head, insert_front()");
- (*head)->record = rec;
- (*head)->record_size = rec_size;
- (*head)->next = NULL;
- }
- else {
- struct node_s *n = (struct node_s *)malloc(sizeof(struct node_s));
- if (!n) error_alloc_node(&n, "n, insert_front()");
- n->record_size = rec_size;
- n->record = rec;
- n->next = *head;
- *head = n;
- }
- }
- /*
- * Dodaje noda na koniec listy jednokierunkowej
- * Bez sortowania
- */
- void lpush_back(struct node_s **head, char *rec, size_t record_size) {
- // Jeśli początek jest null to glowa = nowy element
- if (*head == NULL) {
- *head = (struct node_s *)malloc(sizeof(struct node_s));
- if (!*head) error_alloc_node(head, "nowy, insert_back()");
- (*head)->record = rec;
- (*head)->record_size = record_size;
- (*head)->next = NULL;
- }
- else {
- struct node_s *node = *head;
- for (; node->next != NULL; node = node->next);
- node->next = (struct node_s *)malloc(sizeof(struct node_s));
- if (!node->next) error_alloc_node(&node->next, "node->next, insert_back()");
- node->next->next = NULL;
- node->next->record = rec;
- node->next->record_size = record_size;
- }
- }
- /**
- * Drukuj liste
- */
- void lprint(struct node_s *head) {
- for (; head != NULL; head = head->next)
- printf("%s %d | %p -> %p\n", head->record, (int)(head->record_size - sizeof(char)), head, head->next);
- }
- /**
- * Usuwa liste z pamieci
- *
- */
- void ldestroy(struct node_s **head) {
- struct node_s *tmp = NULL;
- for (; tmp != NULL;) {
- tmp = (*head)->next;
- //Zwlania węzeł
- free((*head)->record);
- (*head)->record = NULL;
- free((*head));
- (*head) = NULL;
- *head = tmp;
- }
- *head = NULL;
- }
- /**
- * Drukuj ostatnie permutacje
- */
- void lprintp(struct node_s *head, int c) {
- struct node_s *tmp = NULL;
- int i = 0, k = 0, j = 0;
- for( i = 0 ; i < c; i++) {
- fprintf(stdout, "(%s", head->record);
- tmp = head;
- k = (2*c - 2*i)-1;
- //printf("k: %d\n", k);
- for ( j = 0; j < k ; j++) {
- tmp = tmp->next;
- }
- if(tmp != NULL)
- fprintf(stdout, "+%s)\n", tmp->record);
- else {
- perror("zla matematyka !");
- ldestroy(&head);
- exit(EXIT_FAILURE);
- }
- if(head->next != NULL)
- head = head->next;
- }
- }
- int main(int argc, char **argv) {
- int i, j, k, l, m,n,o, offset, c = 4;
- size_t record_size, new_record_size, newest_record_size;
- char new_glue[4], newest_glue[4], newest__glue[4], record[6];
- struct node_s *list = NULL;
- int is_even = ((c + 1) % 2 == 0) ? 1 : 0;
- if((c + 1) >= AB_SIZE) {
- fprintf(stderr, "Za malo literek (w) AlfaBecie");
- exit(EXIT_FAILURE);
- }
- // wyzerujmey sobie pamiec
- memset(&record, 0, 6 * sizeof(char));
- memset(&new_glue, 0, 4 * sizeof(char));
- memset(&newest_glue, 0, 4 * sizeof(char));
- memset(&newest__glue, 0, 4 * sizeof(char));
- // Bob budowniczy..
- // ustawimy poczatkowe wartosc - znaki ( ) i +
- record[0] = chars[0];
- record[2] = chars[2];
- record[4] = chars[1];
- new_glue[0] = chars[2];
- new_glue[2] = chars[1];
- newest_glue[0] = chars[2];
- newest_glue[2] = chars[1];
- newest__glue[0] = chars[2];
- newest__glue[2] = chars[1];
- for(i = 0; i < (c + 1); i++) {
- for(k = c; k > i; k--) {
- record[1] = ab[i];
- record[3] = ab[k];
- if(is_even) {
- if( k % c == 0)
- lpush_back(&list, strdup(record), sizeof(record) * sizeof(char));
- else if(k % 2 == 0)
- lpush_back(&list, strdup(record), sizeof(record) * sizeof(char));
- else
- lpush_front(&list, strdup(record), sizeof(record) * sizeof(char));
- }
- fprintf(stdout, "%s\n", record);
- int z =0;
- // doklej nastpne char y za wyjatkiem wykorzystanych powyzej
- for(l = 0; l < (c + 1); l++) {
- if((ab[l] != ab[i]) && (ab[l] != ab[k])) {
- new_glue[1] = ab[l];
- fprintf(stdout, "%s%s\n", record, new_glue);
- for(j = 0; j < (c + 1); j++) {
- if((ab[j] != ab[l]) && (ab[j] != ab[i]) && (ab[j] != ab[k])) {
- newest_glue[1] = ab[j];
- fprintf(stdout, "%s%s%s\n",record,new_glue, newest_glue);
- for(m = 0; m < (c + 1); m++) {
- if((ab[m] != ab[j]) && (ab[m] != ab[l]) && (ab[m] != ab[i]) && (ab[m] != ab[k])) {
- newest__glue[1] = ab[m];
- fprintf(stdout, "%s%s%s%s\n",record,new_glue, newest_glue, newest__glue);
- }
- }
- }
- }
- }
- }
- }
- }
- if(is_even) {
- lprintp(list, c);
- ldestroy(&list);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement