Advertisement
BladeMechanics

perm

Mar 13th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include "cranium.h"
  2.  
  3. int int_limit(int floor, int celing, char message[])
  4. {
  5.     int input;
  6.     printf("%s\n? ", message);
  7.     do
  8.     {
  9.         scanf_s("%d", &input);
  10.         if (input >= floor&&input <= celing) break;
  11.         else printf("Invalid input. Please enter a %d < number < %d.\n?", floor, celing);
  12.     } while (YES);
  13.     return input;
  14. }
  15.  
  16. int factorial(int num)
  17. {
  18.     if (num == 1) return YES;
  19.     else return num*factorial(num - 1);
  20. }
  21.  
  22. int countComb(int N, int R)
  23. {
  24.     if (N == R) return YES;
  25.     else return factorial(N) / (factorial(R)*factorial(N - R));
  26. }
  27.  
  28. int countPerm(int N, int R)
  29. {
  30.     if (N == R) return YES;
  31.     else return factorial(N) / factorial(N - R);
  32. }
  33.  
  34. void swapElement(char *x, char *y)
  35. {
  36.     char temp;
  37.     temp = *x;
  38.     *x = *y;
  39.     *y = temp;
  40. }
  41.  
  42. int printNew(char base[], char input[])
  43. {
  44.     int similar = strlen(base);
  45.     for (int i = 0; (unsigned)i < strlen(base); i++)
  46.         if (base[i] == input[i]) similar--;
  47.     if (similar==0) return NO;
  48.     else return YES;
  49. }
  50.  
  51.  
  52. void list_permute(char *input, char *output, int startindex, int endindex, int setsize)
  53. {
  54.     int i; char temp[MAXARRAY] = { '0' };
  55.     if (startindex == endindex)
  56.     {
  57.         strncpy(temp, input, setsize);
  58.         if (printNew(output, temp))
  59.         {
  60.             printf("%s\n", temp);
  61.             strcpy(output, temp);
  62.         }
  63.     }
  64.     else
  65.     {
  66.         for (i = startindex; i <= endindex; i++)
  67.         {
  68.             swapElement((input + startindex), (input + i));
  69.             list_permute(input, output, startindex + 1, endindex, setsize);
  70.             swapElement((input + startindex), (input + i));
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76. void list_comb(char input[], int n, int r)
  77. {
  78.     char data[MAXARRAY];
  79.     comb_switch(input, data, 0, n - 1, 0, r);
  80. }
  81.  
  82. void comb_switch(char input[], char data[], int start, int end, int index, int r)
  83. {
  84.     if (index == r)
  85.     {
  86.         for (int j = 0; j<r; j++)
  87.             printf("%c", data[j]);
  88.         printf("\n");
  89.         return;
  90.     }
  91.     for (int i = start; i <= end && end - i + 1 >= r - index; i++)
  92.     {
  93.         data[index] = input[i];
  94.         comb_switch(input, data, i + 1, end, index + 1, r);
  95.     }
  96. }
  97.  
  98.  
  99.  
  100. void list_lexi(char *input, char *output, int startindex, int endindex, int setsize)
  101. {
  102.     int i;
  103.     char temp[MAXARRAY] = {'0'};
  104.     if (startindex == endindex)
  105.     {
  106.         strncpy(temp, input, setsize);
  107.         if (printNew(output, temp))
  108.         {
  109.             if(lexi(temp, setsize)) printf("%s\n", temp);
  110.             strcpy(output, temp);
  111.         }
  112.     }
  113.     else
  114.     {
  115.         for (i = startindex; i <= endindex; i++)
  116.         {
  117.             swapElement((input + startindex), (input + i));
  118.             list_lexi(input, output, startindex + 1, endindex, setsize);
  119.             swapElement((input + startindex), (input + i));
  120.         }
  121.     }
  122. }
  123.  
  124. int lexi(char input[], int length)
  125. {
  126.     int base, compare, pass;
  127.     for (int i = 0; i<length; i++)
  128.     {
  129.         if (i + 1 == length)
  130.         {
  131.             break; return pass;
  132.         }
  133.         else
  134.         {
  135.             base = input[i];
  136.             compare = input[i + 1];
  137.         }
  138.         if (base <= compare) pass = YES;
  139.         else
  140.         {
  141.             pass = NO;
  142.             break;
  143.         }
  144.     }
  145.     return pass;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement