Phinecos(洞庭散人)
光荣在于平淡,艰巨因为漫长
C++ Exercises(十四)--解线性方程组
帮一个朋友做的作业题,现在真是什么专业都要学编程了呀。。
//
向量类
/**/
/*
**author:phinecos
**date:7/17/2008
*/
class
CVector
{
public
:
CVector(unsigned
int
d
=
0
);
//
由向量的维数创建向量,向量元素值初始化为
CVector(unsigned
int
d,
double
*
pe);
//
由向量的维数和向量元素数组创建数组
CVector(
double
x,
double
y);
//
由两个元素生成二维向量
CVector(
double
x,
double
y,
double
z);
//
由三个元素生成三维向量
CVector(CVector
&
v);
//
复制构造函数
~
CVector();
//
析构函数
CVector
&
operator
=
(
const
CVector
&
v);
//
重载赋值运算符
CVector
&
operator
+
(
void
);
//
重载一元运算符+
CVector
operator
+
(
const
CVector
&
v)
const
;
//
重载二元运算符+
CVector
&
operator
-
(
void
);
//
重载一元运算符-
CVector
operator
-
(
const
CVector
&
v)
const
;
//
重载二元运算符-
CVector
operator
*
(
const
CVector
&
v)
const
;
//
重载二元运算符*,表示向量的叉乘(向量积)
double
operator
%
(
const
CVector
&
v)
const
;
//
重载二元运算符%,表示向量的点乘(数量积)
CVector
operator
*
(
const
double
&
d)
const
;
//
重载二元运算符*,表示向量的数乘
double
&
operator
[](
const
unsigned
int
i);
//
重载操作符[],对指定向量元素进行操作
unsigned
int
GetDegree()
const
;
//
获取向量维数
void
printElements();
friend
class
CLinearEquation;
private
:
double
*
pElement;
//
向量元素存储地址
unsigned
int
nDegree;
//
向量的维数
void
initByZero();
//
初始化为
void
initByArray(
double
*
pe);
//
用数组初始化
void
alloc(unsigned
int
n
=
0
);
//
分配空间
void
reverse();
//
变号
void
destory();
}
;
向量类实现
#include
"
stdafx.h
"
#include
"
Vector.h
"
#include
<
cassert
>
#include
<
memory.h
>
#include
<
iostream
>
using
namespace
std;
/**/
/*
**author:phinecos
**date:7/17/2008
*/
CVector::CVector(unsigned
int
d):nDegree(d)
{
//
由向量的维数创建向量,向量元素值初始化为
if
(d
>
0
)
{
this
->
alloc(
this
->
nDegree);
this
->
initByZero();
//
向量元素值初始化为
}
}
CVector::CVector(
double
x,
double
y):nDegree(
2
)
{
this
->
alloc(nDegree);
this
->
pElement[
0
]
=
x;
this
->
pElement[
1
]
=
y;
}
CVector::CVector(
double
x,
double
y,
double
z):nDegree(
3
)
{
//
由三个元素生成三维向量
this
->
alloc(nDegree);
this
->
pElement[
0
]
=
x;
this
->
pElement[
1
]
=
y;
this
->
pElement[
2
]
=
z;
}
CVector::CVector(unsigned
int
d,
double
*
pe):nDegree(d)
{
//
由向量的维数和向量元素数组创建数组
if
(d
>
0
)
{
this
->
alloc(
this
->
nDegree);
this
->
initByArray(pe);
}
}
CVector::CVector(CVector
&
v)
{
//
复制构造函数
this
->
nDegree
=
v.GetDegree();
if
(
this
->
nDegree
>
0
)
{
this
->
alloc(
this
->
nDegree);
this
->
initByArray(v.pElement);
}
}
CVector
&
CVector::
operator
=
(
const
CVector
&
v)
{
//
重载赋值运算符
if
(v.GetDegree()
>
0
)
{
this
->
destory();
//
销毁原数据
this
->
nDegree
=
v.GetDegree();
//
新向量大小
this
->
alloc(
this
->
nDegree);
//
分配空间
this
->
initByArray(v.pElement);
//
复制数据
}
return
*
this
;
}
CVector
&
CVector::
operator
+
(
void
)
{
//
重载一元运算符+
return
*
this
;
}
CVector CVector::
operator
+
(
const
CVector
&
v)
const
{
//
重载二元运算符+
assert(
this
->
nDegree
==
v.GetDegree());
//
两个向量的维数应该相等
CVector result(
this
->
nDegree,
this
->
pElement);
for
(unsigned
int
i
=
0
;i
<
this
->
nDegree;
++
i)
{
result.pElement[i]
+=
v.pElement[i];
}
return
result;
}
CVector
&
CVector::
operator
-
(
void
)
{
//
重载一元运算符-
this
->
reverse();
return
*
this
;
}
CVector CVector::
operator
-
(
const
CVector
&
v)
const
{
//
重载二元运算符-
assert(
this
->
nDegree
==
v.GetDegree());
//
两个向量的维数应该相等
CVector result(
this
->
nDegree,
this
->
pElement);
for
(unsigned
int
i
=
0
;i
<
this
->
nDegree;
++
i)
{
result.pElement[i]
-=
v.pElement[i];
}
return
result;
}
CVector CVector::
operator
*
(
const
CVector
&
v)
const
{
//
重载二元运算符*,表示向量的叉乘(向量积)
assert(
this
->
nDegree
==
v.GetDegree());
assert(
this
->
nDegree
==
3
);
double
a1
=
this
->
pElement[
0
];
double
b1
=
this
->
pElement[
1
];
double
c1
=
this
->
pElement[
2
];
double
a2
=
v.pElement[
0
];
double
b2
=
v.pElement[
1
];
double
c2
=
v.pElement[
2
];
return
CVector(b1
*
c2
-
b2
*
c1,c1
*
a2
-
a1
*
c2,a1
*
b2
-
a2
*
b1);
}
double
CVector::
operator
%
(
const
CVector
&
v)
const
{
//
重载二元运算符%,表示向量的点乘(数量积)
assert(
this
->
nDegree
==
v.GetDegree());
double
result
=
0.0f
;
for
(unsigned
int
i
=
0
;i
<
this
->
nDegree;
++
i)
{
result
+=
this
->
pElement[i]
*
v.pElement[i];
}
return
result;
}
double
&
CVector::
operator
[](
const
unsigned
int
i)
{
//
重载操作符[],对指定向量元素进行操作
assert(i
>=
0
&&
i
<
this
->
nDegree);
return
this
->
pElement[i];
}
void
CVector::reverse()
{
for
(unsigned
int
i
=
0
;i
<
this
->
nDegree;
++
i)
{
this
->
pElement[i]
=
-
this
->
pElement[i];
}
}
void
CVector::alloc(unsigned
int
n)
{
//
分配大小为n的存储区
if
(n
>
0
)
{
this
->
pElement
=
new
double
[n];
}
}
void
CVector::initByZero()
{
for
(unsigned
int
i
=
0
;i
<
nDegree;
++
i)
{
this
->
pElement[i]
=
0
;
}
}
void
CVector::initByArray(
double
*
pe)
{
for
(unsigned
int
i
=
0
;i
<
nDegree;
++
i)
{
this
->
pElement[i]
=
pe[i];
}
}
void
CVector::destory()
{
if
(
this
->
pElement
!=
NULL)
{
delete [] pElement;
this
->
pElement
=
NULL;
this
->
nDegree
=
0
;
}
}
CVector::
~
CVector(
void
)
{
this
->
destory();
}
unsigned
int
CVector::GetDegree()
const
{
//
获取向量维数
return
this
->
nDegree;
}
void
CVector::printElements()
{
for
(unsigned
int
i
=
0
;i
<
this
->
nDegree;
++
i)
{
cout
<<
"
x[
"
<<
i
<<
"
]=
"
<<
this
->
pElement[i]
<<
endl;
}
}
class
CVector;
//
前向声明
//
矩阵类
/**/
/*
**author:phinecos
**date:7/17/2008
*/
class
CMatrix
{
public
:
CMatrix(unsigned
int
r
=
0
,unsigned
int
c
=
0
);
//
由矩阵的行数和列数创建矩阵类对象,并为矩阵元素分配存储空间,将矩阵初始化为单位矩阵;
CMatrix(
const
char
*
pFileName);
//
由矩阵存储文件名创建矩阵类对象,文件格式可参考附录,但不限于采用此格式;
CMatrix(
const
CMatrix
&
m);
//
复制构造函数,由已有矩阵类对象创建新的矩阵类对象;
unsigned
int
GetRowsNum()
const
;
//
获取矩阵的行数
unsigned
int
GetColumnsNum()
const
;
//
获取矩阵的列数
double
&
operator
()(unsigned
int
r, unsigned
int
c);
//
重载运算符(),用于提取指定行(r)列(c)的元素值
CMatrix
&
operator
=
(
const
CMatrix
&
m);
//
重载运算符=,用于矩阵之间相互赋值
CMatrix
&
operator
+
()
const
;
//
重载一元运算符+,即取矩阵本身
CMatrix
operator
-
()
const
;
//
重载一元运算符-,即矩阵元素取相反数
CMatrix
operator
+
(
const
CMatrix
&
m)
const
;
//
重载二元运算符+,即两个矩阵求和
CMatrix
operator
-
(
const
CMatrix
&
m)
const
;
//
重载二元运算符-,即两个矩阵求差
CMatrix
operator
*
(
const
CMatrix
&
m)
const
;
//
重载二元运算符*,即两个矩阵求积
CMatrix
operator
*
(
const
double
&
x)
const
;
//
重载二元运算符*,即矩阵与数相乘
CMatrix
operator
*
(CVector
&
v)
const
;
//
重载二元运算符*,即矩阵与向量相乘
CMatrix
operator
/
(
const
double
&
x )
const
;
//
重载二元运算符/,即矩阵与数相除
CMatrix
operator
^
(
const
int
&
t)
const
;
//
重载二元运算符^,即矩阵求t次幂
void