<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 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Puzzle</title>
</head>

<body>

<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000" size="6"><b>&nbsp;</b></font><font color="#FF0000" size="6"><b>我的Matrix</b></font>  
</p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">A.第一版</font></b></pre>
</div>
<div align="left">
  <pre><b>这个小程序是最初的版本。</b></pre>
</div>
<div align="left">
  <pre><b>1。 程序基本说明：这是一个实现矩阵各种运算的类，目前仅仅实现了从文件读取矩阵，将矩阵化简为echelon form和reduced echelon </b></pre>
</div>
<div align="left">
  <pre><b>	form.</b></pre>
</div>
<div align="left">
  <pre><b>2。 程序思路：我的类功能还很简单，通过一个内置的二维数组记录矩阵的entry。</b><b>	</b></pre>
</div>
<div align="left">
  <pre><b>3。 主要函数介绍：</b></pre>
</div>
<div style="WIDTH: 892px; HEIGHT: 102px" align="left">
  <pre><b>     A.  void readFromFile(const char* fileName);从文件读取矩阵，以换行符作为矩阵行数增加的标志，我利用了ifstream的</b></pre>
  <pre><b>	成员函数peek()每次去偷窥一下是否为换行符，是的话就将矩阵行数加一，同时将列数更新，当然最后一行会因为文件结束而只能强</b></pre>
  <pre><b>	行加一。</b></pre>
  <pre><b>     B. void echelon(int r, bool reduced=true);化简为echelon form的函数，首先从0行调用，找到不为0的entry然后将其化简</b></pre>
  <pre><b>	为1，然后将行数参数r加1，以递归调用。参数reduced默认化简为最简echelon form.	</b></pre>
  <pre><b>4。 不足之处：</b></pre>
  <pre><b>	A. </b><b>时间比较少，方法写的比较粗糙。</b></pre>
</div>
<div align="left">
  <pre><b>	B. </b><b>还没有实现矩阵的加碱法，乘法。</b></pre>
</div>
<div align="left">
  <pre><b>	</b></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;</pre>
<pre>using namespace std;</pre>
<pre>const MaxRow = 10;
const MaxCol = 10;
</pre>
<pre>class Matrix
{
private:
	int rowNum;
	int colNum;
	double lst[MaxRow][MaxCol];
	void mul(int source, int dest, double scalor);</pre>
<pre>public:
	Matrix();
	int row() const {return rowNum;}
	int col() const {return colNum;}
	void setRow(const int newRow) { rowNum = newRow;}
	void setCol(const int newCol) { colNum = newCol;}
	void display();
	double&amp; items(int r, int c);
	void initialize();
	void readFromFile(const char* fileName);
	void echelon(int r, bool reduced=true);
};
</pre>
<pre>int main()
{
	Matrix M;
	M.readFromFile(&quot;c:\\nick.txt&quot;);
	M.echelon(0);
	M.display();</pre>
<pre>	cout&lt;&lt;&quot;\n\n&quot;&lt;&lt;&quot;row is:&quot;&lt;&lt;M.row();
	cout&lt;&lt;&quot;\ncol is:&quot;&lt;&lt;M.col()&lt;&lt;endl;
	return 0;
}</pre>
<pre>void Matrix::echelon(int r, bool reduced)
{
	int c=r;
	if (r&lt;rowNum)
	{
		while (c&lt; colNum)
		{
			if (lst[r][c] ==0)
			{
				c++;
			}
			else
			{
				mul(r, r, (1-lst[r][c])/lst[r][c]);
				for (int i=(!reduced?r+1:0); i&lt; rowNum; i++)
				{
					if (i!=r)
					{
						mul(r, i, -lst[i][c]);
					}
				}
				echelon(r+1, reduced);
				break;
			}
		}
	}
	</pre>
<pre>}</pre>
<pre>void Matrix::mul(int source, int dest, double scalor)
{
	for (int c=0; c&lt; colNum; c++)
	{
		lst[dest][c] += lst[source][c]*scalor;
	}
}
</pre>
<pre>double&amp; Matrix::items(int r, int c)
{
	return lst[r][c];
}</pre>
<pre>void Matrix::readFromFile(const char* fileName)
{
	int r=0, c=0;
	char ch;
	ifstream f;
	f.open(fileName);
	while (!f.eof())
	{
		ch = f.peek();
		</pre>
<pre>		if (ch!=10)
		{
			</pre>
<pre>			f&gt;&gt;lst[r][c];
			c++;
			if (c&gt;colNum)
				colNum = c;
		}
		else
		{
			f.ignore();
			r++;
			setCol(c);
			c =0;
		}
	}
	if (r!=0)
	{
		setRow(r+1);
	}
}
</pre>
<pre>void Matrix::initialize()
{
	for (int r=0; r &lt; rowNum; r++)
	{
		for (int c=0; c&lt; colNum; c++)
		{
			lst[r][c] = r*2+c;
		}
	}	
}
</pre>
<pre>void Matrix::display()
{
	int temp;
	long preFlag;
	preFlag = cout.flags();
	temp = cout.precision(4);
//	cout.setf(ios::fixed|ios::showpoint);
	</pre>
<pre>	cout&lt;&lt;&quot;row\\col&quot;;
	for (int c=0; c&lt; colNum; c++)
	{
		cout&lt;&lt;&quot;\t&quot;&lt;&lt;c;
	}
	cout&lt;&lt;&quot;\n\n&quot;;
	for (int r = 0; r&lt; rowNum; r++)
	{
		cout&lt;&lt;r;
		for (c = 0; c&lt; colNum; c++)
		{
			cout&lt;&lt;&quot;\t&quot;&lt;&lt;lst[r][c];
		}
		cout&lt;&lt;endl;
	}
	cout.precision(temp);
	cout.flags(preFlag);
}</pre>
<pre>Matrix::Matrix()
{
	rowNum = 5;
	colNum = 5;
	initialize();
}</pre>
<pre><img border="0" src="picture/matrix.JPG" width="668" height="510"></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;&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="proof.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; 
<a href="Matrix1.htm"><img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</a>          


</p>

</body>

</html>
