<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">B.第二版</font></b></pre>
</div>
<div align="left">
  <pre><b>这个小程序是增强版本。</b></pre>
</div>
<div align="left">
  <pre><b>1。 程序基本说明：在前一个版本基础上进一步实现矩阵与数的乘法，矩阵的乘法，加法（减法忘了实现了，不过可以用*-1再加来做，所</b></pre>
</div>
<div align="left">
  <pre><b>	以。。。），矩阵的复制(操作符=），矩阵的旋转（transposition即绕主轴翻转180度)。</b></pre>
</div>
<div align="left">
  <pre><b>2。 程序思路：在矩阵的操作符重载后我返回矩阵的引用以便进行操作符的cascade,如M*2*4。</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. </b><b>void mul(int source, int dest, double scalor);  </b></pre>
  <pre><b>	</b><b>void mul(int dest, double scalor);</b></pre>
  <pre><b>	内部方法第一个是把source行乘以scalor对应相加到dest行的对应项。第二个把dest行的每个元素乘以scalor.</b></pre>
  <pre><b>     B. </b><b>Matrix&amp; operator *(Matrix otherMatrix);  矩阵的乘法，我为了省事，就在局部声明了一个临时矩阵以便记载结果，然后</b></pre>
  <pre><b>	再用复制的方法返回矩阵。</b></pre>
  <pre><b>4。 不足之处：</b></pre>
  <pre><b>	A. </b><b>时间比较少，方法写的比较粗糙。</b></pre>
</div>
<div align="left">
  <pre><b>		</b></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;fstream&gt;</pre>
<pre>using namespace std;</pre>
<pre>const int MaxRow = 10;
const int MaxCol = 10;
const double LIMIT = 0.01;
</pre>
<pre>class Matrix
{
private:
	int rowNum;
	int colNum;
	double lst[MaxRow][MaxCol];
	void mul(int source, int dest, double scalor);
	void mul(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);
	Matrix&amp; operator*(double i);
	Matrix&amp; operator *(Matrix otherMatrix);  
	Matrix&amp; operator = (Matrix other);
	Matrix&amp; operator+(Matrix other);
	Matrix&amp; transposition();
	</pre>
<pre>};
</pre>
<pre>int main()
{
	Matrix M, N;
	M.readFromFile(&quot;c:\\nick.txt&quot;);
	N = M;
	cout&lt;&lt;&quot;\nthis is M\n&quot;;
	M.display();
	M*2;
//	M.echelon(0, false);
	cout&lt;&lt;&quot;\nthis is M*2\n&quot;;
	M.display();
	cout&lt;&lt;&quot;\nthis is N\n&quot;;
	N.display();</pre>
<pre>	M*N;
	cout&lt;&lt;&quot;\nthis is M*N\n&quot;;
	M.display();
	cout&lt;&lt;&quot;\nthis is rotating of M\n&quot;;
	M.transposition();
	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>Matrix&amp; Matrix::transposition()
{
	double hold;
	int temp;
	for (int r =0; r&lt; rowNum; r++)
	{
		for (int c=0; c&lt; r; c++)
		{
			hold = lst[r][c];
			lst[r][c] = lst[c][r];
			lst[c][r] = hold;
		}
	}
	temp = rowNum;
	rowNum = colNum;
	colNum = temp;
	return (*this);
}
</pre>
<pre>Matrix&amp; Matrix::operator +(Matrix other)
{
	if (rowNum!= other.row() || colNum!= other.col())
	{
		cout&lt;&lt;&quot;\nTwo matrix has different row or col number!\n&quot;;
		return (*this);
	}
	else
	{
		for (int r=0; r&lt; rowNum; r++)
		{
			for (int c=0; c&lt; colNum; c++)
			{
				lst[r][c] +=other.items(r, c);
			}
		}
		return (*this);
	}
}</pre>
<pre>Matrix&amp; Matrix::operator *(Matrix other)
{
	double total =0;
	Matrix temp;
	temp.setRow(rowNum);
	temp.setCol(other.col());</pre>
<pre>	if (colNum!=other.row())
	{
		cout&lt;&lt;&quot;\nrow &amp; col are not same!\n&quot;;
	</pre>
<pre>	}
	else
	{
		for (int r =0; r&lt; rowNum; r++)
		{
			for (int c=0; c&lt;other.col(); c++)
			{
				total =0;
				for (int i=0; i&lt;colNum; i++)
				{
					total += lst[r][i] * other.items(i, c);
				}
				temp.items(r, c) = total;
			}
		}
		*this = temp;
	</pre>
<pre>	}
	return (*this);
}
				</pre>
<pre>Matrix&amp; Matrix::operator =(Matrix other)
{
	setRow(other.row());
	setCol(other.col());
	for (int r=0; r&lt; other.row(); r++)
	{
		for (int c=0; c&lt; other.col(); c++)
		{
			lst[r][c] = other.items(r, c);
		}
	}
	</pre>
<pre>	return (*this);
}</pre>
<pre>void Matrix::mul(int dest, double scalor)
{
	for (int c=0; c&lt; colNum; c++)
	{
		lst[dest][c] *= scalor;
	}
}</pre>
<pre>Matrix&amp; Matrix::operator *(double i)
{
	for (int r=0; r&lt;rowNum; r++)
	{
		for (int c=0; c&lt;colNum; c++)
		{
			lst[r][c] *= i;
		}
	}
	return (*this);
}</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, 1/lst[r][c]);   //make it 1 for this row
				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);
	</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;(fabs(lst[r][c])&lt;LIMIT?0: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/matrix1.JPG" width="662" height="686"></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="Matrix.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="stack.htm"><img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</a>          


</p>

</body>

</html>
