Advertisement
Guest User

Untitled

a guest
May 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1.  
  2. /*
  3. * File: main.c
  4. * Author: Duy
  5. *
  6. * Created on 2. května 2016, 19:53
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. /*
  14. *
  15. */
  16. int n;
  17. int koef = 1;
  18. char input[20];
  19.  
  20. int factorial(int n) {
  21. int i;
  22. int fact = 1;
  23. for (i = 1; i <= n; i++) {
  24. fact = fact*i;
  25. }
  26. return fact;
  27. }
  28.  
  29. void checkRepetition(int zacatek, int konec, int index, char *pole) {
  30. int i;
  31. int counter = 0;
  32. for (i = zacatek; i < konec; i++) {
  33. if (pole[i] == pole[index]) {
  34. counter++;
  35. // printf("ANO %d\n", counter);
  36.  
  37. } else {
  38. // printf("NE %d\n", counter);
  39. // printf(" nerovnam se index %d pozice %d\n", index, i);
  40. koef = koef * (factorial(counter));
  41. checkRepetition(i, konec, i, pole);
  42. break;
  43. }
  44.  
  45. if (i == konec - 1) {
  46. koef = koef * (factorial(counter));
  47. }
  48. }
  49. // printf(" dosel jsem na konec index %d\n", index);
  50. //koef = koef * (factorial(counter));
  51. }
  52.  
  53. void swap(char* i, char* j) {
  54. char tmp = *i;
  55. *i = *j;
  56. *j = tmp;
  57. //implement code
  58. }
  59.  
  60. void adaptedBubbleSort(char *pole, int velikost) {
  61. char *tmppole = pole;
  62. int i, j;
  63. for (i = 0; i < velikost; i++) {
  64.  
  65. int sorted = 0;
  66. for (j = 0; j < velikost - 1; j++) {
  67. if (tmppole[j] > tmppole[j + 1]) {
  68. sorted = 1;
  69. swap(&tmppole[j], &tmppole[ j + 1]);
  70. }
  71. }
  72. if (sorted == 0) {
  73. break;
  74. }
  75. }
  76. }
  77.  
  78. int next_permutation(char *pole, int velikost) {// algoritmus inspirovany https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
  79. int tmp;
  80.  
  81.  
  82. int i;
  83. if (velikost <= 0)
  84. return 0;
  85. i = velikost - 1;
  86. while (i > 0 && pole[i - 1] >= pole[i])
  87. i--;
  88. if (i == 0)
  89. return 0;
  90.  
  91.  
  92. int j;
  93. j = velikost - 1;
  94. while (pole[j] <= pole[i - 1])
  95. j--;
  96. tmp = pole[i - 1];
  97. pole[i - 1] = pole[j];
  98. pole[j] = tmp;
  99.  
  100.  
  101. j = velikost - 1;
  102. while (i < j) {
  103. tmp = pole[i];
  104. pole[i] = pole[j];
  105. pole[j] = tmp;
  106. i++;
  107. j--;
  108. }
  109. return 1;
  110. }
  111.  
  112. int main(int argc, char** argv) {
  113. // printf("zadej retezec\n");
  114. scanf("%s", input); // pole se stejne predava jako reference, ale radsi si napisu & abych si pamatoval ze se ma predavat adresa
  115. //printf("velikost retezce je %d\n", (strlen(input)));
  116. // printf("retezec pred ukoncenim %s\n", input);
  117. input[8] = '\0';
  118. // printf("Delka retezce: %d\n", (strlen(input)));
  119. n = strlen(input);
  120. printf("Delka retezce: %d\n", n);
  121. printf("Vstupni retezec: \"%s\"\n", input);
  122. /*if (input[0] > input[1])
  123. printf("prvni znak je vetsi nez druhy");
  124. else
  125. printf("neni vetsi");*/
  126. int velikost = ((sizeof (char)) * n + 1);
  127. char *pointernapole = (char*) malloc(velikost);
  128. //printf("velikost alokovane pameti je %d\n", velikost);
  129. if (pointernapole == NULL) {
  130. printf("Chyba pri alokaci\n");
  131. return 1;
  132. }
  133. int i;
  134. for (i = 0; i < n + 1; i++) {
  135. pointernapole[i] = input[i];
  136. }
  137. //pointernapole[n] = '\0';
  138. //printf(" dynamicky alokovane pole %s\n", pointernapole);
  139. adaptedBubbleSort(pointernapole, n);
  140. checkRepetition(0, n, 0, pointernapole);
  141. //printf("koeficient je %d\n", koef);
  142.  
  143. //printf("tisknu permutace\n");
  144. printf("Serazeno: \"%s\"\n", pointernapole);
  145. printf("Pocet permutaci: %d\n", factorial(n));
  146.  
  147. for (i = 0; i < koef; i++) {
  148. printf("\"%s\"\n", pointernapole);
  149. }
  150.  
  151. while (next_permutation(pointernapole, n)) {
  152. for (i = 0; i < koef; i++) {
  153. printf("\"%s\"\n", pointernapole);
  154. }
  155. }
  156.  
  157.  
  158. free(pointernapole);
  159.  
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement