Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cranium.h"
- int int_limit(int floor, int celing, char message[])
- {
- int input;
- printf("%s\n? ", message);
- do
- {
- scanf_s("%d", &input);
- if (input >= floor&&input <= celing) break;
- else printf("Invalid input. Please enter a %d < number < %d.\n?", floor, celing);
- } while (YES);
- return input;
- }
- int factorial(int num)
- {
- if (num == 1) return YES;
- else return num*factorial(num - 1);
- }
- int countComb(int N, int R)
- {
- if (N == R) return YES;
- else return factorial(N) / (factorial(R)*factorial(N - R));
- }
- int countPerm(int N, int R)
- {
- if (N == R) return YES;
- else return factorial(N) / factorial(N - R);
- }
- void swapElement(char *x, char *y)
- {
- char temp;
- temp = *x;
- *x = *y;
- *y = temp;
- }
- int printNew(char base[], char input[])
- {
- int similar = strlen(base);
- for (int i = 0; (unsigned)i < strlen(base); i++)
- if (base[i] == input[i]) similar--;
- if (similar==0) return NO;
- else return YES;
- }
- void list_permute(char *input, char *output, int startindex, int endindex, int setsize)
- {
- int i; char temp[MAXARRAY] = { '0' };
- if (startindex == endindex)
- {
- strncpy(temp, input, setsize);
- if (printNew(output, temp))
- {
- printf("%s\n", temp);
- strcpy(output, temp);
- }
- }
- else
- {
- for (i = startindex; i <= endindex; i++)
- {
- swapElement((input + startindex), (input + i));
- list_permute(input, output, startindex + 1, endindex, setsize);
- swapElement((input + startindex), (input + i));
- }
- }
- }
- void list_comb(char input[], int n, int r)
- {
- char data[MAXARRAY];
- comb_switch(input, data, 0, n - 1, 0, r);
- }
- void comb_switch(char input[], char data[], int start, int end, int index, int r)
- {
- if (index == r)
- {
- for (int j = 0; j<r; j++)
- printf("%c", data[j]);
- printf("\n");
- return;
- }
- for (int i = start; i <= end && end - i + 1 >= r - index; i++)
- {
- data[index] = input[i];
- comb_switch(input, data, i + 1, end, index + 1, r);
- }
- }
- void list_lexi(char *input, char *output, int startindex, int endindex, int setsize)
- {
- int i;
- char temp[MAXARRAY] = {'0'};
- if (startindex == endindex)
- {
- strncpy(temp, input, setsize);
- if (printNew(output, temp))
- {
- if(lexi(temp, setsize)) printf("%s\n", temp);
- strcpy(output, temp);
- }
- }
- else
- {
- for (i = startindex; i <= endindex; i++)
- {
- swapElement((input + startindex), (input + i));
- list_lexi(input, output, startindex + 1, endindex, setsize);
- swapElement((input + startindex), (input + i));
- }
- }
- }
- int lexi(char input[], int length)
- {
- int base, compare, pass;
- for (int i = 0; i<length; i++)
- {
- if (i + 1 == length)
- {
- break; return pass;
- }
- else
- {
- base = input[i];
- compare = input[i + 1];
- }
- if (base <= compare) pass = YES;
- else
- {
- pass = NO;
- break;
- }
- }
- return pass;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement