<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Dependency</title>
</head>

<body>



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Dependency(reader)</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This is the first edition of my &quot;dependency&quot; and actually it is a simple &quot;reader&quot;. </b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>&quot;Function dependency&quot; is a 
very important issue in database theory. And what's more, it is the essence of 
all.</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>Because I think it is the 
key of knowledge: the world is described by relations and relations of 
relations.</b></font></span></p>
<div align="left">
  <p ALIGN="LEFT">　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>The reader only reads all relations and 
  its attributes. The syntax is like following:</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>R(A,B,C,D);<br>
  AB -&gt; C;<br>
  C-&gt;D,D-&gt;A;</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>The first line gives the name of relation 
  &quot;R&quot;. Inside parentheses there are attributes. Then comes the function</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>dependencies, separated by &quot;,&quot; or &quot;;&quot;.&nbsp; 
  The only outstanding feature of my reader is that I try to recognize the</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>attributes according to the principle of 
  the &quot;longest&quot;. i.e. attribute &quot;Ax&quot; and &quot;A&quot; both starts with an &quot;A&quot;.</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>If in a relation such as &quot;AB -&gt; AxB&quot;. My 
  reader should recognize the LHS is &quot;A&quot; and &quot;B&quot;, while the RHS should be</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>&quot;Ax&quot; and &quot;B&quot;. How to do this? When 
  searching the first character &quot;A&quot;, it returns two results and my program </b>
  </font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>should continue to search for next 
  character until only one result returned.</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>When I read more about the book.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>1<span lang="en-ca">. reader.h </span></b></font></pre>
  <pre><span lang="en-ca"><font size="3"><b>2</b></font></span><font size="3"><b><span lang="en-ca">. reader.</span></b></font><span lang="en-ca"><font size="3"><b>cpp</b></font></span></pre>
  <pre><span lang="en-ca"><font size="3"><b>3</b></font></span><font size="3"><b><span lang="en-ca">. main.cpp (main)</span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: reader.h</b></font></span></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;bitset&gt;

using namespace std;

//make it simple, the first line must list all variables or attributes
//&quot;relation&quot;(A,B,C...)
const int MaxNameLength=5;
const int MaxAttrNumber=20;
const int MaxRuleNumber=100;

int extractNames(FILE* stream, char nameBuffers[][MaxNameLength+1], char* delimeters, 
				 char endChar='\n');


class Rule
{
private:
	bitset&lt;MaxAttrNumber&gt; lhs;
	bitset&lt;MaxAttrNumber&gt; rhs;
public:
	Rule();
	bool test(int pos, bool isLeft);
	void lhsSet(int pos);
	void rhsSet(int pos);
	void set(int pos, bool isLeft);
};


class Reader
{
private:
	char attributes[MaxAttrNumber][MaxNameLength+1];
	int attrCount;
	int attrFound[MaxAttrNumber];
	int numberFound;
	int searchByChar(char ch, int step);
	void ruleReading(FILE* stream);
	Rule rules[MaxRuleNumber];
	int ruleNumber;
	void displayRule(int index);
public:
	void readFile(const char* fileName);
	void display();
};</pre>
<pre>　</pre>
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: reader.cpp</b></font></span></pre>
<pre>#include &quot;reader.h&quot;


void Reader::display()
{
	cout&lt;&lt;&quot;\nnow display\n&quot;;
	cout&lt;&lt;attributes[0]&lt;&lt;&quot;(&quot;;
	for (int i=1; i&lt;=attrCount; i++)
	{
		cout&lt;&lt;attributes[i];
		if (i!=attrCount)
		{
			cout&lt;&lt;&quot;,&quot;;
		}
		else
		{
			cout&lt;&lt;&quot;)\n&quot;;
		}
	}
	for (i=0; i&lt;ruleNumber; i++)
	{
		displayRule(i);
	}
}

bool Rule::test(int pos, bool isLeft)
{
	if (isLeft)
	{
		return lhs.test(pos);
	}
	else
	{
		return rhs.test(pos);
	}
}


void Reader::displayRule(int index)
{
	for (int i=1; i&lt;=attrCount; i++)
	{
		if (rules[index].test(i, true))
		{
			cout&lt;&lt;attributes[i];
		}
	}
	cout&lt;&lt;&quot; -&gt; &quot;;
	for (i=1; i&lt;=attrCount; i++)
	{
		if (rules[index].test(i, false))
		{
			cout&lt;&lt;attributes[i];
		}
	}
	cout&lt;&lt;&quot;;\n&quot;;
}


void Rule::set(int pos, bool isLeft)
{
	if (isLeft)
	{
		lhsSet(pos);
	}
	else
	{
		rhsSet(pos);
	}
}

void Rule::rhsSet(int pos)
{
	rhs.set(pos);
}

void Rule::lhsSet(int pos)
{
	lhs.set(pos);
}

Rule::Rule()
{
	lhs.reset();
	rhs.reset();
	
}


void Reader::readFile(const char* fileName)
{
	FILE* stream;
	stream=fopen(fileName, &quot;r&quot;);
	attrCount=extractNames(stream, attributes, &quot;=,()&quot;, ';')-1;//ignore the first relation name
	ruleReading(stream);

}

void Reader::ruleReading(FILE* stream)
{
	char ch;
	int step=0;
	ruleNumber=0;//initialize
	bool isLeft=true;
	while (!feof(stream))
	{		
		ch=fgetc(stream);
		if (ch==';'||ch==',')
		{
			ruleNumber++;		
			isLeft=true;
		}
		else
		{
			if (ch=='-'||ch=='&gt;')
			{
				isLeft=false;
			}
			else
			{
				if (isalpha(ch))
				{
					searchByChar(ch, step);
					if (numberFound!=1)
					{
						if (step&lt;MaxNameLength)
						{
							step++;
						}
						else
						{
							cout&lt;&lt;&quot;illegal attribute stated!\n&quot;;
							exit(1);
						}
					}
					else
					{
						step=0;
						rules[ruleNumber].set(attrFound[0], isLeft);
					}
				}
			}
		}
	}
}


int Reader::searchByChar(char ch, int step)
{
	if (step==0)//this is first time, and all attributes are searched
	{
		numberFound=0;
		for (int i=1; i&lt;=attrCount; i++)//because the #1 is relation
		{
			if (ch==attributes[i][step])
			{
				attrFound[numberFound]=i;
				numberFound++;
			}
		}
	}
	else
	{
		int number=0;//new 'attrFound' re-counting
		for (int i=0; i&lt;numberFound; i++)
		{
			if (ch==attributes[attrFound[i]][step])
			{
				attrFound[number]=i;
				number++;
			}
		}
		numberFound=number;
	}
	return numberFound;
}





int findChar(char ch, char* str)
{
	int index=0;
	while (str[index]!='\0')
	{
		if (str[index]==ch)
		{
			return index;
		}
		index++;
	}
	return -1;
}

//extract names from a line delimetered by various chars, like ',','(',')'....
int extractNames(FILE* stream, char nameBuffers[][MaxNameLength+1], char* delimeters, char endChar)
{
	int findChar(char ch, char* str);
	char ch;
	int nameIndex=0;
	char buffer[MaxNameLength+1];
	char* ptr=buffer;

	while (!feof(stream))
	{
		ch=getc(stream);
		if (ch==endChar)
		{
			if (ptr!=buffer)
			{
				*ptr='\0';
				strcpy(nameBuffers[nameIndex], buffer);
			}
			break;
		}

		if (findChar(ch, delimeters)&gt;0)//deli reached
		{
			//skip the consecutive deli
			if (ptr!=buffer)
			{
				*ptr='\0';				
				strcpy(nameBuffers[nameIndex], buffer);
				nameIndex++;
				ptr=buffer;//restart
			}
		}
		else
		{
			*ptr=ch;
			ptr++;
		}
	}
	return nameIndex;
}		


	
</pre>
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: main.cpp (main)</b></font></span></pre>
<pre>#include &quot;reader.h&quot;

int main()
{
	Reader R;
	R.readFile(&quot;d:\\rules.txt&quot;);
	R.display();

	return 0;
}</pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="game24.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>