在C#中实现矩阵运算
环境:
主机:XP
开发环境:VS2008
功能:
在C#中实现矩阵运算
源代码:
[csharp]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//矩阵数据结构
//二维矩阵
class _Matrix
{
public int m;
public int n;
public float[] arr;
//初始化
public _Matrix()
{
m = 0;
n = 0;
}
public _Matrix(int mm,int nn)
{
m = mm;
n = nn;
}
//设置m
public void set_mn(int mm,int nn)
{
m = mm;
n = nn;
}
//设置m
public void set_m(int mm)
{
m = mm;
}
//设置n
public void set_n(int nn)
{
n = nn;
}
//初始化
public void init_matrix()
{
arr = new float[m * n];
}
//释放
public void free_matrix()
{
//delete [] arr;
}
//读取i,j坐标的数据
//失败返回-31415,成功返回值
public float read(int i,int j)
{
if (i >= m || j >= n)
{
return -31415;
}
//return *(arr + i * n + j);
return arr[i * n + j];
}
//写入i,j坐标的数据
//失败返回-1,成功返回1
public int write(int i,int j,float val)
{
if (i >= m || j >= n)
{
return -1;
}
arr[i * n + j] = val;
return 1;
}
};
//二维运算类
class _Matrix_Calc
{
//初始化
public _Matrix_Calc()
{
}
//C = A + B
//成功返回1,失败返回-1
public int add(ref _Matrix A,ref _Matrix B,ref _Matrix C)
{
int i = 0;
int j = 0;
//判断是否可以运算
if (A.m != B.m || A.n != B.n ||
A.m != C.m || A.n != C.n)
{
return -1;
}
//运算
for (i = 0;i < C.m;i++)
{
for (j = 0;j < C.n;j++)
{
C.write(i,j,A.read(i,j) + B.read(i,j));
}
}
return 1;
}
//C = A - B
//成功返回1,失败返回-1
public int subtract(ref _Matrix A,ref _Matrix B, ref _Matrix C)
{
int i = 0;
int j = 0;
//判断是否可以运算
if (A.m != B.m || A.n != B.n ||
A.m != C.m || A.n != C.n)
{
return -1;
}
//运算
for (i = 0;i < C.m;i++)
{ 
补充:软件开发 , C# ,