1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>
using namespace std;
class FD
{
public:
string left,right;
};
vector <FD> funcdep,tempdep;
string attributes;
string::iterator it2;
string appnd(string s,char c)
{
int len = s.length();
s += c;
return s;
}
bool stringsearch(string str1,string str2)
{
char ch;
int i,len;
len = str2.length();
for(i=0;i<len;i++)
{
ch = str2[i];
if(!strchr(str1.c_str(),ch))
return false;
}
return true;
}
string unique(string str)
{
string temp;
int i,j,p,len,found;
len = str.length();
for(i=0;i<len;i++)
{
found = temp.find(str[i]);
if(found == string::npos)
{
temp = appnd(temp,str[i]);
}
}
return temp;
}
string alphaplus(string str,string res)
{
int i,len,found;
string temp;
vector <FD> :: iterator it;
res += str;
while(res.compare(temp))
{
temp = res;
for(it=funcdep.begin();it!=funcdep.end();it++)
{
found = res.find(it->left);
if(found!=string::npos)
{
found = res.find(it->right);
if(found==string::npos)
{
res.append(it->right);
}
}
}
}
return unique(res);
}
void checkunion()
{
vector <FD> :: iterator it,it2;
for(it=funcdep.begin();it!=funcdep.end();it++)
{
for(it2=funcdep.begin();it2!=funcdep.end();it2++)
{
if(it==it2) continue;
else
{
if(!(it->left).compare(it2->left))
{
(it->right).append(it2->right);
funcdep.erase(it2);
}
}
}
}
}
void extraleft()
{
int len,i,j,k,p;
string y,temp,storing;
vector <FD> :: iterator it;
for(it=funcdep.begin();it!=funcdep.end();it++)
{
len = (it->left).length();
if( len == 1) continue;
for(i=0;i<len;i++)
{
temp = it->left;
it2 = temp.begin()+i;
temp.erase(it2);
storing = alphaplus(temp,"");
if(stringsearch(storing,it->right))
{
(it->left) = temp;
}
}
}
}
int main()
{
int att,fd,i,j;
unsigned int s;
cout << "Number of nodes: ";
cin >> att;
cout << "Number of FDs: ";
cin >> fd;
FD fdinput[100];
cout << "The attributes in the schema: ";
cin >> attributes;
vector <FD> :: iterator it;
for(i=1;i<=fd;i++)
{
cin >> fdinput[i].left;
cin >> fdinput[i].right;
funcdep.push_back(fdinput[i]);
}
s=0;
while(s!=funcdep.size())
{
tempdep = funcdep;
checkunion();
extraleft();
s = tempdep.size();
}
for(it=funcdep.begin();it!=funcdep.end();it++)
{
cout << it->left << "==>" << unique(it->right) << endl;
}
//system("pause");
getch();
return 0;
}
|