Skip to content

Instantly share code, notes, and snippets.

@ruyut
Last active June 13, 2019 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruyut/cf2792f63bc2a983beae7d7a61d6e6ee to your computer and use it in GitHub Desktop.
Save ruyut/cf2792f63bc2a983beae7d7a61d6e6ee to your computer and use it in GitHub Desktop.
#include "pch.h"
#include <iostream>
#include "stdlib.h"
#include "time.h"
#include "string.h"
#define SIZE 7
#define DIM 21
// A,B,C,D,E,F,ERD
char ERD[SIZE][SIZE] = { {0,1,0,0,0,0,0},//A
{1,0,2,0,4,0,0},//B
{0,2,0,3,0,5,6},//C
{0,0,3,0,0,0,9},//D
{0,4,0,0,0,7,0},//E
{0,0,5,0,7,0,8},//F
{0,0,6,9,0,8,0} };//ERD
char COL[DIM] = {0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6};
char SEL[SIZE] = {0,0,0,0,0,0,0};
char FROM[SIZE];
char level[SIZE] = {0,1,0,0,0,0,0};
char ptr[SIZE] = {0,0,0,0,0,0,0};
bool cover()
{
for (int i = 0; i < SIZE; i++) if (SEL[i] && level[i] == 0)return false;
return true;
}
void span(int n)
{
for (int j = 0; j < SIZE; j++)if (level[j] == 0)
{
int m = 0;
for (int i = 0; i < SIZE; i++)if (ERD[i][j] && level[i] == n)
{
level[j] = n + 1;
if (rand() % (++m) == 0)ptr[j] = i + 1;
}
}
}
int main()
{
srand(time(NULL)); rand();
char input[30];
gets_s(input);
for (int i = 0; i < strlen(input); i++)SEL[i] = 0;
// SELECT
for (int i = 0; i < strlen(input); i++)if (input[i] >= 'A'&&input[i] < ('A' + DIM))SEL[COL[input[i] - 'A']] = 1;
printf("\nSELECT\t"); for (int i = 0; i < strlen(input); i++)if (input[i] >= 'A'&&input[i] < ('A' + DIM))printf("T%d.%c\t", COL[input[i] - 'A'], input[i]);
printf("\n"); for (int i = 0; i < SIZE; i++) { FROM[i] = level[i] = ptr[i] = 0; FROM[i] = SEL[i]; };
level[0] = 1;
int n = 1;
while (!cover())span(n++);
while (n-- > 1)for (int i = 0; i < SIZE; i++)if (level[i] == (n + 1) && FROM[i])FROM[ptr[i] - 1] = 1;
printf("FROM\t"); for (int i = 0; i < SIZE; i++)if (FROM[i])printf("T%d\t",i);
printf("\n\nWHERE\t"); for (int i = 0; i < strlen(input); i++)if (input[i] >= 'a'&& input[i] < ('a' + DIM))printf("T%d.%c = %c\t", COL[input[i] - 32 - 'A'], input[i] - 32, input[i]);
printf("\n"); for (int j = 0; j < SIZE; j++)if (level[j] > 1 && FROM[j])printf("\t& T%d.x = T%d.y\n", ptr[j] - 1, j);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment