Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. char **output;
  5. int n = 0;
  6. void sortoutput(char** str,int n,int z){
  7. int i,j;
  8. char temp[z];
  9. for (i = 0; i < n; i++) {
  10. for (j = 0; j < n - 1; j++) {
  11. if (strcmp(str[j], str[j + 1]) > 0) {
  12. strcpy(temp, str[j]);
  13. strcpy(str[j], str[j + 1]);
  14. strcpy(str[j + 1], temp);
  15. }
  16. }
  17. }
  18. }
  19. void swap(char *x, char *y)
  20. {
  21. char temp;
  22. temp = *x;
  23. *x = *y;
  24. *y = temp;
  25. }
  26. void permute(char *a, int l, int r)
  27. {
  28. int i;
  29. if (l == r){
  30. n++;
  31. strcpy(output[n-1],a);
  32. }else {
  33. for (i = l; i <= r; i++)
  34. {
  35. swap((a+l), (a+i));
  36. permute(a, l+1, r);
  37. swap((a+l), (a+i));
  38. }
  39. }
  40. }
  41. void bubble(char *i, int n)
  42. {
  43. int a, b;
  44. char t;
  45.  
  46. for(a=0; a < n; a++)
  47. for(b=n-1; b >= a; b--)
  48. {
  49. if(i[b-1] > i[b])
  50. {
  51. t = i[b-1];
  52. i[b-1] = i[b];
  53. i[b] = t;
  54. }
  55. }
  56.  
  57. }
  58. long int factorial(long int n){
  59. if (n == 0 || n == 1) return 1;
  60. return n * factorial(n - 1);
  61. }
  62. int main(int argc, char const *argv[])
  63. {
  64. char str[9];
  65. int len=9;
  66. int z=0;
  67. int fact=1;
  68. while(scanf("%c", &(str[z])) > 0){
  69. if (z>8) break;
  70. z++;
  71. }
  72. z--;
  73. str[z]=0;
  74. fact = (int) factorial(z);
  75. output = (char**) malloc(sizeof(char*)*fact);
  76. for (int i = 0; i < fact; i++){
  77. output[i] = (char*) malloc((z+1)*sizeof(char));
  78. }
  79. printf("Delka retezce: %d\n", z);
  80. printf("Vstupni retezec: \"%s\"\n" , str);
  81. bubble(str,z);
  82. printf("Serazeno: \"%s\"\n", str);
  83. printf("Pocet permutaci: %d\n", fact);
  84. permute(str, 0, z-1);
  85. sortoutput(output,fact,z);
  86. for (int i = 0; i < fact; ++i)
  87. {
  88. printf("\"%s\"\n", output[i]);
  89. }
  90. for (int i = 0; i < fact; i++){
  91. free(output[i]);
  92. }
  93. free(output);
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement