Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Jack Real Butcher do SysOps / DevOps Polska
- 20 godz. ·
- Siemka ;] szybkie zadanko moze ktos mi pomoze. Ja nigdy nie bylem jakis super dobry z programowania. Chce napisac skrypta oczywiscie bashowego lub jakis kod w C ktory by mi robil to:
- Parametr wejsciowy: cyfra
- wyjściowo plik który będzie miał zawartość zależną od cyfry z wejściowego czyli:
- 1. Dla 1 :
- (A+B)
- 2. Dla 2 :
- (A+B)
- (A+C)
- (B+C)
- ((A+B)+C)
- ((A+C)+B)
- 3. Dla 3 :
- (A+B)
- (A+C)
- (A+D)
- (B+C)
- (B+D)
- (C+D)
- ((A+B)+C)
- ((A+B)+D)
- ((A+C)+D)
- ((B+C)+D)
- ((A+C)+B)
- ((A+D)+B)
- ((A+D)+C)
- ((B+D)+C)
- ((B+C)+A)
- ((B+D)+A)
- ((C+D)+A)
- ((C+D)+B)
- (((A+B)+C)+D)
- (((A+B)+D)+C)
- (((A+C)+B)+D)
- (((A+C)+D)+B)
- (((A+D)+B)+C)
- (((A+D)+C)+B)
- (((B+C)+A)+D)
- (((B+C)+D)+A)
- (((B+D)+A)+C)
- (((B+D)+C)+A)
- (((C+D)+A)+B)
- (((C+D)+B)+A)
- ((A+B)+(C+D))
- ((A+C)+(B+D))
- ((A+D)+(B+C))
- Itp. pewnie da się to prostą listą ogarnąć
- Emotikon smile
- pomożecie?
- Emotikon smile
- Dziękuję
- */
- /*
- Author: Dawid Mocek <[email protected]>
- 2016-01-02
- Nie jest to ani seksi ani zoptymalizowane ale działa... chyba :-)
- */
- #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;
- // MAGIC
- 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;
- }
- /*for (; (*head) != NULL && (*head)->used == 0 ; (*head) = (*head)->next) {
- // pierwszy wolny wyraz
- fprintf(stdout, "(%s+", (*head)->record);
- (*head)->used = 1;
- next = (*head)->next;
- if(next != NULL) {
- // znajdz ostatnio wolny
- for (; next != NULL; next = next->next) {
- if(next->next == NULL) {
- printf("jest null\n");
- fprintf(stdout, "%s)\n", (next->record));
- next->used = 1;
- break;
- } else if (next->used == 0) {
- fprintf(stdout, "%s)\n", (next->record));
- next->used = 1;
- }
- }
- }
- }
- }*/
- /*(*head)->used = 1;
- fprintf(stdout, "(%s+", (*head)->record);
- tmp = (*head);
- // wskocz na ostatnio wolny (used) wyraz w liscie od aktualnej pozycji
- for (; tmp != NULL; tmp = tmp->next)
- fprintf(stdout, "%s)\n", tmp->record);
- tmp->used = 1;
- fprintf(stdout, "\n");*/
- }
- int main(int argc, char **argv) {
- int i, j, k, l, m, offset, c = 5;
- size_t record_size, new_record_size, newest_record_size;
- char new_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));
- // 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];
- for(i = 0; i < (c + 1); i++) {
- for(k = c; k > i; k--) {
- // malloc '(' + ')' + '+' + (ab * 2) + 'null terminator'
- //record_size = (sizeof(char) + sizeof(char) + sizeof(char) + (sizeof(char) * 2) + sizeof(char));
- //record = (char *) malloc(record_size);
- //if(record == NULL) {
- // perror("Malloc");
- // exit(EXIT_FAILURE);
- //}
- // Generujemy wiersz
- //record[0] = chars[0];
- record[1] = ab[i];
- //record[2] = chars[2];
- record[3] = ab[k];
- //record[4] = chars[1];
- //record[5] = (char)'\0';
- // printf("i:%d k:%d %s\n", i, k, record);
- // a taki hackerski trick azeby sensownie poukladac rekordy w liscie.
- // nie mialem na to pomyslu strict algorytmicznego aby wygenerowac dla np n=3
- // ciag znakow w atkiej kolejnosci jak ponizej:
- // ((A+B)+(C+D))
- // ((A+C)+(B+D))
- // ((A+D)+(B+C))
- //
- // wymaga pamieci heap
- 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);
- // doklej nastpne char y za wyjatkiem wykorzystanych powyzej
- for(l = 0; l < (c + 1); l++) {
- if(i != l && l != k) {
- // fprintf(stdout, "%s+%c)", record, ab[l]);
- //new_record_size = sizeof(char) + sizeof(char) + sizeof(char);
- //new_record = (char *)malloc(record_size + new_record_size);
- //memset(new_record, 0, record_size + new_record_size);
- new_glue[1] = ab[l];
- //new_glue[3] = (char)'\0';
- //memcpy(new_record, record, record_size);
- //offset = record_size - sizeof(char);
- //memcpy(new_record + offset, &glue, (sizeof(char) * 3));
- //lpush_front(&list, new_record, record_size + new_record_size);
- fprintf(stdout, "%s%s\n", record, new_glue);
- // doklej resztke
- for(j = 0; j < (c + 1); j++) {
- if((ab[j] != ab[l]) && (ab[j] != ab[i]) && (ab[j] != ab[k])) {
- //newest_record_size = sizeof(char) + sizeof(char) + sizeof(char) + record_size + new_record_size;
- //newest_record = (char *)malloc(newest_record_size);
- // memset(newest_record, 0, newest_record_size);
- //newest_glue[0] = chars[2];
- newest_glue[1] = ab[j];
- //newest_glue[2] = chars[1];
- //newest_glue[3] = (char)'\0';
- //memcpy(newest_record, new_record, record_size + new_record_size);
- // printf("newest_record = %s", newest_record);
- ///offset = record_size + new_record_size - sizeof(char);
- //printf(" , glue = %s\n", glue);
- ///memcpy(newest_record + offset, &glue, (sizeof(char) * 3));
- //printf("finally: %s\n", newest_record);
- ///lpush_front(&list, newest_record, new_record_size + newest_record_size);
- fprintf(stdout, "%s%s%s\n",record,new_glue, newest_glue);
- }
- }
- }
- }
- //free(record);
- // record = NULL;
- }
- }
- if(is_even) {
- lprintp(list, c);
- ldestroy(&list);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement