#include <iostream> #include <fstream> using namespace std; ofstream outfile("C://Users//yanfeng//Desktop//1.txt"); int num = 6; int const numTable = 2;//表的个数 int const numCol = 6;//表的列数 int const numRow = 6;//表的行数 int keyArray[numCol];//用于存储每张表每次产生的key int keyTem[numRow][numCol]; int keyMap[numTable][numRow][numCol];//用于存储每张表的keys int x[6] = {1,2,3,4,5,6}; int a[6][6] = { {1, 2, 3, 4, 5, 6}, {2, 3, 4, 5, 6, 7}, {3, 4, 5, 6, 7, 8}, {4, 5, 6, 7, 8, 9}, {5, 6, 7, 8, 9, 10}, {6, 7, 8, 9, 10,11}, }; int b[6][6] = { {8, 2, 3, 4, 5, 6}, {7, 3, 4, 5, 6, 7}, {6, 4, 5, 6, 7, 8}, {5, 5, 6, 7, 8, 9}, {4, 6, 7, 8, 9, 10}, {3, 7, 8, 9, 10,11}, }; int c[2][6][6] = { { {1, 2, 3, 4, 5, 6}, {2, 3, 4, 5, 6, 7}, {3, 4, 5, 6, 7, 8}, {4, 5, 6, 7, 8, 9}, {5, 6, 7, 8, 9, 10}, {6, 7, 8, 9, 10,11} }, { {8, 2, 3, 4, 5, 6}, {7, 3, 4, 5, 6, 7}, {6, 4, 5, 6, 7, 8}, {5, 5, 6, 7, 8, 9}, {4, 6, 7, 8, 9, 10}, {3, 7, 8, 9, 10,11}, } }; void print1DArray(int num, int array0[6]) { for(int i = 0; i < num; i++) { cout << array0[i]; //for debug outfile << array0[i]; } } void print2DArray(int row, int col, int array1[][6]) { int r = row; int c = col; for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) { cout << array1[i][j] << "/t"; if(j == (c) - 1) { cout << "/n"; } } } void print3DArray(int height, int row, int col, int array2[][6][6]) { int h = height; int r = row; int c = col; for(int k = 0; k < h; k++) { for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { cout << array2[k][i][j] << "/t"; if(j == c - 1) { cout << "/n"; } } if(i == r - 1) { cout << "/n"; } } } } void ReverseEngineeringProcess(int numTable) { /*for debug to compute the number of combinations int count = 0;*/ for(int tableCount = 0; tableCount < numTable; tableCount++) { //求表的列属性的超集 for(int colCount = 0; colCount < (1 << numCol); colCount++) { for(int digitCount = 0; digitCount < numCol; digitCount++) { if( (colCount & (1 << digitCount)) == 0) { keyArray[digitCount] = 0; } else { keyArray[digitCount] = 1; } } /*for debug to count the number of combinations print1DArray(num, keyArray); cout << "/n"; outfile << endl; count++;*/ //判断是否为键 //1.1首先求得比较的属性数量是多少 int attributeNum = 0; for (colCount = 0; colCount < numCol; colCount++) { if (keyArray[colCount] != 0) //0011 1000 { attributeNum++; } } //1.2找出比较属性的在原表中下标 int* position = new int[attributeNum]; int k = 0; for (colCount = 0; colCount < numCol; colCount++) { if (keyArray[colCount] != 0) { position[k++] = colCount; } } int rowCount; int rowCount1; int attributeCount; //1.3开始比较 bool keyFlag = true; for (rowCount = 0; rowCount < numRow; rowCount++) { for (rowCount1 = rowCount + 1; rowCount1 < numCol; rowCount1++) { bool flag = true; for (attributeCount = 0; attributeCount < attributeNum; attributeCount++) { if (c[tableCount][rowCount][position[attributeCount]] != c[tableCount][rowCount1][position[attributeCount]]) flag = false; } //Not key if (flag == true) { keyFlag = false; break; } } } //判断刚求得的键是否为超键 //如果是超键的话不做任何处理。 //如果不是超键的话则插入到keyTem中 if (keyFlag == true) { bool flag = true; //insert into keyTem; //subsetBitMapOfEachTable; for(rowCount = 0; rowCount < numRow; rowCount++) { for(colCount = 0; colCount < numCol; colCount++) { if( keyTem[rowCount][colCount] != (keyTem[rowCount][colCount] & keyArray[colCount]) ) { flag = false; break; } } if(flag == false) { break; } } int static keyMapRowCount = 0; int static keyTemRowCount = 0; if(flag == true)//是主键,不是超键则把keyTem[][]插入到keyMap[tableCount][][] { for(colCount = 0; colCount < numCol; colCount++) { keyTem[keyTemRowCount][colCount] = keyArray[colCount]; keyMap[tableCount][keyMapRowCount][colCount] = keyArray[colCount]; } keyMapRowCount++; keyTemRowCount++; } } } } /*for debug to count the number of combinations cout << count;*/ } int main() { /*Test the arrays print2DArray(6,6,a); cout << "/n"; print2DArray(6,6,b); print3DArray(2,6,6,c); print2DArray(numCol, numRow, keyTem); */ /*Test whether it can produce the right combinations. ReverseEngineeringProcess(1);*/ ReverseEngineeringProcess(1); //print2DArray(numCol, numRow, keyTem); return 0; }