<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>CFGReader</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>Layout 
Manager</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b><font size="3">This is first<span lang="en-ca"> edition of my Layout Manager. And it is quite hard to imagine its usefulness.</span></font></b></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>
<div align="left">
  <b>In Comp354 we need a <span lang="en-ca">kind of text mode displayer to show 
  the board, score bar etc. And is it </span> </b></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>convenient if we can have a kind of display layout 
  manager to universally handle all displaying </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>job? </b></span></div>
<div 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">
  <b><span lang="en-ca">In text mode to display something it is a tedious job as 
  you have to calculate the cell on screen.</span></b></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>My idea is that if we can have a sort of manager to draw 
  the whole screen for a set of objects which</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <b><span lang="en-ca">are all rectangle ones and the manager will at each 
  rectangle starting coordinate call the </span> </b></div>
<div align="left">
  　</div>
<div align="left">
  <b><span lang="en-ca">rectangle's displaying method, say &quot;displayRow()&quot;. Then 
  we can simply place all rectangle at any </span> </b></div>
<div align="left">
  　</div>
<div align="left">
  <b><span lang="en-ca">place we want. And for each rectangle, it is easy to 
  implement a line-by-line display method. </span> </b></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>Each rectangle should inherited from an abstract 
  class---&quot;Display&quot; which defined two pure virtual</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>method:&quot;display()&quot; and &quot;displayRow()&quot;. The displayRow 
  will return an integer to indicate how many</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>line left to finish display the rectangle.</b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>Layout class is just a kind of scheduler who draw the 
  background of screen and check if its cursor </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>enters one of &quot;rectangle&quot; where he should call the 
  rectangle's &quot;displayRow&quot; method. After exiting </b></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><b>the area of rectangle, Layout should continue to draw 
  the background.</b></span></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><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><b><font size="3"><span lang="en-ca">1. layout.h</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>2. layout.</span>cpp<span lang="en-ca">  </span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>3. main.cpp (main)</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: layout.h</span></b></font></pre>
</div>
<pre>const int BoardSize=10;
const int ScreenWidth=70;
const int ScreenHeight=40;
const int MaxDisplayCount=5;
const char DefaultChar='*';

struct Coord
{
	int x;
	int y;
};

class Display
{
protected:
	Coord topLeft;
	Coord bottomRight;
	void setTop(int row, int col);
	void setBottom(int row, int col);
	int rowLeft;
public:
	
	virtual int displayRow()=0;
	virtual void display()=0;
	const Coord&amp; getTop(){ return topLeft;}
	const Coord&amp; getBottom(){ return bottomRight;}
};

class Board: public Display
{
protected:
	char board[BoardSize][BoardSize];
	void initialize(int x, int y);
	void setOrigin(int row, int col){ initialize(col, row);}
	void setBoard();
public:
	Board(int x, int y);
	virtual int displayRow();
	virtual void display();
};

class Layout
{
protected:
	Display* items[MaxDisplayCount];
	int itemCount;
	bool validate(Display* newItem);
	void defaultDraw();
	bool startDrawing(int row,int col, int&amp; index);
public:
	Layout();
	void addItem(Display* item);
	void display();
};





</pre>
<pre>
</pre>
<pre></pre>
<pre></pre>
<pre><font size="3" color="#FF0000"><span lang="en-ca"><b>file name: layout</b></span><b>.cpp </b></font></pre>
<pre>#include &lt;iostream&gt;
#include &quot;Layout.h&quot;

using namespace std;

void Display::setBottom(int row, int col)
{
	bottomRight.x=col;
	bottomRight.y=row;
}

void Display::setTop(int row, int col)
{
	topLeft.x=col;
	topLeft.y=row;
}

Board::Board(int x, int y)
{
	initialize(x, y);
}

void Board::initialize(int x, int y)
{
	Display::topLeft.x =x;
	Display::topLeft.y =y;
	Display::bottomRight.x=x+BoardSize;
	Display::bottomRight.y=y+BoardSize;
	rowLeft=BoardSize;
	setBoard();
}

void Board::setBoard()
{
	for (int r=0; r&lt;BoardSize; r++)
	{
		for (int c=0; c&lt;BoardSize; c++)
		{
			board[c][r]='b'+c+r;
		}
	}
}

int Board::displayRow()
{
	for (int c=0; c&lt;BoardSize; c++)
	{
		cout&lt;&lt;board[c][BoardSize-rowLeft];
	}
	return --rowLeft;
}

void Board::display()
{
	for (int r=0; r&lt;BoardSize; r++)
	{
		for (int c=0; c&lt;BoardSize; c++)
		{
			cout&lt;&lt;board[c][r];
		}
	}
}

Layout::Layout()
{
	itemCount=0;
}

bool Layout::validate(Display* newItem)
{
	return true;
}
	
void Layout::addItem(Display* item)
{
	if (validate(item))
	{
		if (itemCount&lt;MaxDisplayCount)
		{
			items[itemCount]=item;
			itemCount++;
		}
	}
}

void Layout::display()
{
	bool drawing;
	int count, index, rowLeft=-1;
	for (int r=0; r&lt;ScreenHeight; r++)
	{
		drawing=false;
		for (int c=0; c&lt;ScreenWidth; c++)
		{
			if (!drawing)
			{
				if (startDrawing(r,c, index))
				{
					drawing=true;
					rowLeft=items[index]-&gt;displayRow();
					count=items[index]-&gt;getBottom().x-items[index]-&gt;getTop().x;
					count--;
				}
				else
				{
					defaultDraw();
				}
			}
			else
			{
				if (rowLeft&gt;=0)
				{
					count--;
					if (count==0)
					{
						if (rowLeft==0)
						{
							rowLeft=-1;
						}
						drawing=false;
					}					
				}
				else
				{
					drawing=false;
				}
			}
		}
		cout&lt;&lt;endl;
	}
}

bool Layout::startDrawing(int row, int col, int&amp; index)
{
	for (index=0; index&lt;itemCount; index++)
	{
		if (row&gt;=items[index]-&gt;getTop().y&amp;&amp;row&lt;items[index]-&gt;getBottom().y
			&amp;&amp;col&gt;=items[index]-&gt;getTop().x&amp;&amp;col&lt;items[index]-&gt;getBottom().x)
		{
			return true;
		}
	}
	return false;
}


void Layout::defaultDraw()
{
	cout&lt;&lt;DefaultChar;
}


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

using namespace std;

int main()
{

	Display* D=new Board(10, 10);
	Display* E=new Board(40, 11);
	Display* F=new Board(55, 15);
	Layout L;
	L.addItem(D);
	L.addItem(E);
	L.addItem(F);
	L.display();
	return 0;
}</pre>
<pre>　</pre>
<pre>　</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre><font color="#0000FF"><b>Here is the result:<span lang="en-ca"> </span>D<span lang="en-ca">o you know what this means? It means that we have three board which is 10x10</span>.<span lang="en-ca"> And we</span></b></font></pre>

<pre><span lang="en-ca"><font color="#0000FF"><b>want it to be placed on some particular co-ordinate. As long as the Board or what ever &quot;rectangle&quot; object</b></font></span></pre>

<pre><span lang="en-ca"><font color="#0000FF"><b>is inherited from abstract class &quot;Display&quot;, we can easily display them by setting up its top-left and bottom-</b></font></span></pre>

<pre><span lang="en-ca"><font color="#0000FF"><b>right coordinate.  </b></font></span></pre>

<pre>　</pre>

<pre>**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********bcdefghijk**************************************************
**********cdefghijkl********************bcdefghijk********************
**********defghijklm********************cdefghijkl********************
**********efghijklmn********************defghijklm********************
**********fghijklmno********************efghijklmn********************
**********ghijklmnop********************fghijklmno*****bcdefghijk*****
**********hijklmnopq********************ghijklmnop*****cdefghijkl*****
**********ijklmnopqr********************hijklmnopq*****defghijklm*****
**********jklmnopqrs********************ijklmnopqr*****efghijklmn*****
**********klmnopqrst********************jklmnopqrs*****fghijklmno*****
****************************************klmnopqrst*****ghijklmnop*****
*******************************************************hijklmnopq*****
*******************************************************ijklmnopqr*****
*******************************************************jklmnopqrs*****
*******************************************************klmnopqrst*****
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
Press any key to continue



</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="WhoAmI.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>