日期:2014-05-17  浏览次数:21024 次

c#实现矩阵的转置,相乘等

比较简单,适合 初学者

题目:

定义矩阵类,完成矩阵的产生,转置和相乘(控制台程序)

步骤:

1.可以新创建一个控制台程序,也可在原工程中添加新类

右键工程名

ADD->new items->class,名称为Matrix

具体的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassTest
{
    class Matrix
    {
        private double[,] matrix;//一个二维数组,保存矩阵中的值
        private int col;//矩阵的列数
        private int row;//矩阵的行数
        private string name;//矩阵的名字
        //构造函数
        public Matrix()
        {

        }
        public Matrix(int row,int col)
        {
            this.col = col;
            this.row = row;
            this.name = "";
            this.matrix = new double[row, col];
        }
        public Matrix(int row,int col,string name)
        {
            this.col = col;
            this.row = row;
            this.name=name;
            this.matrix=new double[row,col];
        }
        public Matrix(int row, int col, double[] data)
        {
            this.col = col;
            this.row = row;
            this.name = "";
            this.matrix = new double[row, col];
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    this.matrix[i,j] = data[i * col + j];
                }
            }
        }
        public Matrix(int row, int col,string name, double[] data)//用一个数组中的值为矩阵赋值
        {
        &nbs