Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char **output;
- int n = 0;
- void sortoutput(char** str,int n,int z){
- int i,j;
- char temp[z];
- for (i = 0; i < n; i++) {
- for (j = 0; j < n - 1; j++) {
- if (strcmp(str[j], str[j + 1]) > 0) {
- strcpy(temp, str[j]);
- strcpy(str[j], str[j + 1]);
- strcpy(str[j + 1], temp);
- }
- }
- }
- }
- void swap(char *x, char *y)
- {
- char temp;
- temp = *x;
- *x = *y;
- *y = temp;
- }
- void permute(char *a, int l, int r)
- {
- int i;
- if (l == r){
- n++;
- strcpy(output[n-1],a);
- }else {
- for (i = l; i <= r; i++)
- {
- swap((a+l), (a+i));
- permute(a, l+1, r);
- swap((a+l), (a+i));
- }
- }
- }
- void bubble(char *i, int n)
- {
- int a, b;
- char t;
- for(a=0; a < n; a++)
- for(b=n-1; b >= a; b--)
- {
- if(i[b-1] > i[b])
- {
- t = i[b-1];
- i[b-1] = i[b];
- i[b] = t;
- }
- }
- }
- long int factorial(long int n){
- if (n == 0 || n == 1) return 1;
- return n * factorial(n - 1);
- }
- int main(int argc, char const *argv[])
- {
- char str[9];
- int len=9;
- int z=0;
- int fact=1;
- while(scanf("%c", &(str[z])) > 0){
- if (z>8) break;
- z++;
- }
- z--;
- str[z]=0;
- fact = (int) factorial(z);
- output = (char**) malloc(sizeof(char*)*fact);
- for (int i = 0; i < fact; i++){
- output[i] = (char*) malloc((z+1)*sizeof(char));
- }
- printf("Delka retezce: %d\n", z);
- printf("Vstupni retezec: \"%s\"\n" , str);
- bubble(str,z);
- printf("Serazeno: \"%s\"\n", str);
- printf("Pocet permutaci: %d\n", fact);
- permute(str, 0, z-1);
- sortoutput(output,fact,z);
- for (int i = 0; i < fact; ++i)
- {
- printf("\"%s\"\n", output[i]);
- }
- for (int i = 0; i < fact; i++){
- free(output[i]);
- }
- free(output);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement