用不带参数的构造函数,怎么进行矩阵运算

老师给我留的作业。急!!!
题目如下:
设计一个矩阵类,重载其加、减、乘运算符,实现矩阵的相加、相减、相乘,并用Windows Form程序进行测试。
要求:
1)定义一个一维数组,用于存储矩阵的值。
2)定义两个私有成员变量存储矩阵行列值。
3)定义带有set\get访问器的两个属性,对行列值变量进行设置。
4)定义一个索引器返回指定某行某列的矩阵的元素,索引器只有get访问器。
5)定义一个不带参构造函数,根据输入行列属性值随机创建矩阵,随机值为(1-100之间的数)。
6)定义+,-,*的重载方法,实现两个矩阵的相加、相减、相乘。
7)定义一个方法,实现矩阵的转置。

问题:
1.一个不带参数的构造函数,怎么能让数组的行列数自定义呢?怎么可以对矩阵进行各种运算呢?
2.求大神帮帮忙,写个完整的代码我看看。头凸

定义一个不带参构造函数,根据输入行列属性值随机创建矩阵
说的很清楚,通过键盘输入
比如
class Rectangle
{
private int rowcount;
private int colcount;
private int[,] data;
public Rectangle()
{
rowcount = int.Parse(Console.ReadLine());
colcount = int.Parse(Console.ReadLine());
...
}
}

重载+运算
public static Rectangle operator +(Rectangle a, Rectangle b)
{
Rectangle result = new Rectangle(a.rowcount, a.colcount);
for (int i = 0; i < result.rowcount; i++)
for (int j = 0; j < result.colcount; j++)
result.data[i,j] = a.data[i, j] + b.data[i, j];
return result;
}

别的类似

采纳了可以完整写给你。

这个答案都不采纳,我就真的生气了。

Matrix

using System;

namespace TestForm
{
    class MATRIX
    {
        private int[] data;
        private int rows;
        private int columns;

        public MATRIX()
        {
            Initialize();
        }

        public int Rows
        {
            get => rows;
            set
            {
                if (value > 0)
                {
                    rows = value;
                    Initialize();
                }
            }
        }
        public int Columns
        {
            get => columns;
            set
            {
                if (value > 0)
                {
                    columns = value;
                    Initialize();
                }
            }
        }

        public int this[int index]
        {
            get
            {
                return data[index];
            }
        }

        private void Initialize()
        {
            data = new int[rows * columns];
            Random rnd = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = rnd.Next(1, 101);
            }
            GC.Collect();
        }
        public static MATRIX operator +(MATRIX a, MATRIX b)
        {
            MATRIX ret = null;
            if (a != null && b != null && a.rows == b.rows && a.columns == b.columns)
            {
                ret = new MATRIX() { Rows = a.rows, Columns = a.columns };
                for (int i = 0; i < ret.Rows * ret.Columns; i++)
                {
                    ret.data[i] = a.data[i] + b.data[i];
                }
            }
            return ret;
        }
        public static MATRIX operator -(MATRIX a, MATRIX b)
        {
            MATRIX ret = null;
            if (a != null && b != null && a.rows == b.rows && a.columns == b.columns)
            {
                ret = new MATRIX() { Rows = a.rows, Columns = a.columns };
                for (int i = 0; i < ret.Rows * ret.Columns; i++)
                {
                    ret.data[i] = a.data[i] - b.data[i];
                }
            }
            return ret;
        }
        public static MATRIX operator *(MATRIX a, MATRIX b)
        {
            MATRIX ret = null;
            if (a != null && b != null && a.rows == b.columns)
            {
                ret = new MATRIX() { Rows = a.rows, Columns = b.columns };
                for (int i = 0; i < ret.rows; i++)
                    for (int j = 0; j < ret.columns; j++)
                    {
                        ret.data[i * ret.columns + j] = 0;
                        for (int k = 0; k < a.columns; k++)

                            ret.data[i * ret.columns + j] += a.data[i * a.columns + k] * b.data[k * b.columns + j];
                    }
            }
            return ret;
        }

        public void T()
        {
            int[] newdata = new int[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                newdata[i] = data[i % columns * columns + i / rows];
            }
            data = newdata;
        }
    }
}

MainForm.cs

using System;
using System.Windows.Forms;

namespace TestForm
{
    public partial class MainForm : Form
    {
        private MATRIX matrixA;
        private MATRIX matrixB;
        public MainForm()
        {
            InitializeComponent();
        }

        private void BTN_CreateMatrix_Click(object sender, EventArgs e)
        {
            MATRIX matrix = new MATRIX()
            {
                Rows = 3,
                Columns = 3
            };
            for (int row = 0; row < matrix.Rows; row++)
            {
                string line = "";
                for (int column = 0; column < matrix.Columns; column++)
                {
                    line += matrix[row * matrix.Columns + column].ToString() + "\t";
                }
                LOG(line);
            }
        }

        private void LOG(string msg)
        {
            LogBox.Text += msg + Environment.NewLine;
        }

        private void BTN_CreateMatrix_A_Click(object sender, EventArgs e)
        {
            LOG("创建矩阵 A");

            int r = 0;
            int c = 0;
            int.TryParse(RowBox_A.Text, out r);
            int.TryParse(ColumnBox_A.Text, out c);
            RowBox_A.Text = r.ToString();
            ColumnBox_A.Text = c.ToString();

            matrixA = new MATRIX()
            {
                Rows = r,
                Columns = c
            };
            OutPutMatrix(matrixA);
        }

        private void BTN_CreateMatrix_B_Click(object sender, EventArgs e)
        {
            LOG("创建矩阵 B");

            int r = 0;
            int c = 0;
            int.TryParse(RowBox_B.Text, out r);
            int.TryParse(ColumnBox_B.Text, out c);
            RowBox_B.Text = r.ToString();
            ColumnBox_B.Text = c.ToString();

            matrixB = new MATRIX()
            {
                Rows = r,
                Columns = c
            };
            OutPutMatrix(matrixB);
        }

        private void OutPutMatrix(MATRIX matrix)
        {
            if (matrix == null)
            {
                LOG("[空矩阵]");
                return;
            }
            for (int row = 0; row < matrix.Rows; row++)
            {
                string line = "";
                for (int column = 0; column < matrix.Columns; column++)
                {
                    line += matrix[row * matrix.Columns + column].ToString() + "\t";
                }
                LOG(line);
            }
        }

        private void BTN_Show_A_Click(object sender, EventArgs e)
        {
            LOG("显示矩阵 A 内容");
            OutPutMatrix(matrixA);
        }

        private void BTN_Show_B_Click(object sender, EventArgs e)
        {
            LOG("显示矩阵 B 内容");
            OutPutMatrix(matrixB);
        }

        private void BTN_Add_Click(object sender, EventArgs e)
        {
            LOG("A + B =");
            OutPutMatrix(matrixA + matrixB);
        }

        private void BTN_Minus_Click(object sender, EventArgs e)
        {
            LOG("A - B =");
            OutPutMatrix(matrixA - matrixB);
        }

        private void BTN_Mutiply_Click(object sender, EventArgs e)
        {
            LOG("A * B =");
            OutPutMatrix(matrixA * matrixB);
        }

        private void BTN_AT_Click(object sender, EventArgs e)
        {
            LOG("A' =");
            matrixA.T();
            OutPutMatrix(matrixA);
        }

        private void BTN_BT_Click(object sender, EventArgs e)
        {
            LOG("B' =");
            matrixB.T();
            OutPutMatrix(matrixB);
        }

        private void Box_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                {
                    e.Handled = true;
                }
            }
        }
    }
}

MainForm.Designer.cs

namespace TestForm
{
    partial class MainForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.LogBox = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.ColumnBox_A = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.RowBox_A = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.ColumnBox_B = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.RowBox_B = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.BTN_CreateMatrix_A = new System.Windows.Forms.Button();
            this.BTN_CreateMatrix_B = new System.Windows.Forms.Button();
            this.BTN_Show_A = new System.Windows.Forms.Button();
            this.BTN_Show_B = new System.Windows.Forms.Button();
            this.BTN_Add = new System.Windows.Forms.Button();
            this.BTN_Minus = new System.Windows.Forms.Button();
            this.BTN_Mutiply = new System.Windows.Forms.Button();
            this.BTN_AT = new System.Windows.Forms.Button();
            this.BTN_BT = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // LogBox
            // 
            this.LogBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.LogBox.Location = new System.Drawing.Point(12, 183);
            this.LogBox.Multiline = true;
            this.LogBox.Name = "LogBox";
            this.LogBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.LogBox.Size = new System.Drawing.Size(664, 255);
            this.LogBox.TabIndex = 1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.BTN_AT);
            this.groupBox1.Controls.Add(this.BTN_Show_A);
            this.groupBox1.Controls.Add(this.BTN_CreateMatrix_A);
            this.groupBox1.Controls.Add(this.ColumnBox_A);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.RowBox_A);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new System.Drawing.Point(13, 13);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(627, 57);
            this.groupBox1.TabIndex = 10;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "矩阵 A";
            // 
            // ColumnBox_A
            // 
            this.ColumnBox_A.Location = new System.Drawing.Point(304, 21);
            this.ColumnBox_A.Name = "ColumnBox_A";
            this.ColumnBox_A.Size = new System.Drawing.Size(100, 20);
            this.ColumnBox_A.TabIndex = 9;
            this.ColumnBox_A.Text = "0";
            this.ColumnBox_A.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Box_KeyPress);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(266, 25);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(31, 13);
            this.label2.TabIndex = 8;
            this.label2.Text = "列:";
            // 
            // RowBox_A
            // 
            this.RowBox_A.Location = new System.Drawing.Point(140, 21);
            this.RowBox_A.Name = "RowBox_A";
            this.RowBox_A.Size = new System.Drawing.Size(100, 20);
            this.RowBox_A.TabIndex = 7;
            this.RowBox_A.Text = "0";
            this.RowBox_A.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Box_KeyPress);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(102, 25);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(31, 13);
            this.label1.TabIndex = 6;
            this.label1.Text = "行:";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.BTN_BT);
            this.groupBox2.Controls.Add(this.BTN_Show_B);
            this.groupBox2.Controls.Add(this.BTN_CreateMatrix_B);
            this.groupBox2.Controls.Add(this.ColumnBox_B);
            this.groupBox2.Controls.Add(this.label5);
            this.groupBox2.Controls.Add(this.RowBox_B);
            this.groupBox2.Controls.Add(this.label6);
            this.groupBox2.Location = new System.Drawing.Point(13, 76);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(627, 57);
            this.groupBox2.TabIndex = 11;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "矩阵 B";
            // 
            // ColumnBox_B
            // 
            this.ColumnBox_B.Location = new System.Drawing.Point(304, 24);
            this.ColumnBox_B.Name = "ColumnBox_B";
            this.ColumnBox_B.Size = new System.Drawing.Size(100, 20);
            this.ColumnBox_B.TabIndex = 9;
            this.ColumnBox_B.Text = "0";
            this.ColumnBox_B.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Box_KeyPress);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(266, 28);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(31, 13);
            this.label5.TabIndex = 8;
            this.label5.Text = "列:";
            // 
            // RowBox_B
            // 
            this.RowBox_B.Location = new System.Drawing.Point(140, 24);
            this.RowBox_B.Name = "RowBox_B";
            this.RowBox_B.Size = new System.Drawing.Size(100, 20);
            this.RowBox_B.TabIndex = 7;
            this.RowBox_B.Text = "0";
            this.RowBox_B.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Box_KeyPress);
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(102, 28);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(31, 13);
            this.label6.TabIndex = 6;
            this.label6.Text = "行:";
            // 
            // BTN_CreateMatrix_A
            // 
            this.BTN_CreateMatrix_A.Location = new System.Drawing.Point(17, 20);
            this.BTN_CreateMatrix_A.Name = "BTN_CreateMatrix_A";
            this.BTN_CreateMatrix_A.Size = new System.Drawing.Size(75, 23);
            this.BTN_CreateMatrix_A.TabIndex = 10;
            this.BTN_CreateMatrix_A.Text = "创建矩阵";
            this.BTN_CreateMatrix_A.UseVisualStyleBackColor = true;
            this.BTN_CreateMatrix_A.Click += new System.EventHandler(this.BTN_CreateMatrix_A_Click);
            // 
            // BTN_CreateMatrix_B
            // 
            this.BTN_CreateMatrix_B.Location = new System.Drawing.Point(17, 22);
            this.BTN_CreateMatrix_B.Name = "BTN_CreateMatrix_B";
            this.BTN_CreateMatrix_B.Size = new System.Drawing.Size(75, 23);
            this.BTN_CreateMatrix_B.TabIndex = 11;
            this.BTN_CreateMatrix_B.Text = "创建矩阵";
            this.BTN_CreateMatrix_B.UseVisualStyleBackColor = true;
            this.BTN_CreateMatrix_B.Click += new System.EventHandler(this.BTN_CreateMatrix_B_Click);
            // 
            // BTN_Show_A
            // 
            this.BTN_Show_A.Location = new System.Drawing.Point(424, 20);
            this.BTN_Show_A.Name = "BTN_Show_A";
            this.BTN_Show_A.Size = new System.Drawing.Size(75, 23);
            this.BTN_Show_A.TabIndex = 11;
            this.BTN_Show_A.Text = "显示矩阵";
            this.BTN_Show_A.UseVisualStyleBackColor = true;
            this.BTN_Show_A.Click += new System.EventHandler(this.BTN_Show_A_Click);
            // 
            // BTN_Show_B
            // 
            this.BTN_Show_B.Location = new System.Drawing.Point(424, 23);
            this.BTN_Show_B.Name = "BTN_Show_B";
            this.BTN_Show_B.Size = new System.Drawing.Size(75, 23);
            this.BTN_Show_B.TabIndex = 12;
            this.BTN_Show_B.Text = "显示矩阵";
            this.BTN_Show_B.UseVisualStyleBackColor = true;
            this.BTN_Show_B.Click += new System.EventHandler(this.BTN_Show_B_Click);
            // 
            // BTN_Add
            // 
            this.BTN_Add.Location = new System.Drawing.Point(13, 140);
            this.BTN_Add.Name = "BTN_Add";
            this.BTN_Add.Size = new System.Drawing.Size(75, 23);
            this.BTN_Add.TabIndex = 12;
            this.BTN_Add.Text = "A + B";
            this.BTN_Add.UseVisualStyleBackColor = true;
            this.BTN_Add.Click += new System.EventHandler(this.BTN_Add_Click);
            // 
            // BTN_Minus
            // 
            this.BTN_Minus.Location = new System.Drawing.Point(107, 140);
            this.BTN_Minus.Name = "BTN_Minus";
            this.BTN_Minus.Size = new System.Drawing.Size(75, 23);
            this.BTN_Minus.TabIndex = 13;
            this.BTN_Minus.Text = "A - B";
            this.BTN_Minus.UseVisualStyleBackColor = true;
            this.BTN_Minus.Click += new System.EventHandler(this.BTN_Minus_Click);
            // 
            // BTN_Mutiply
            // 
            this.BTN_Mutiply.Location = new System.Drawing.Point(201, 140);
            this.BTN_Mutiply.Name = "BTN_Mutiply";
            this.BTN_Mutiply.Size = new System.Drawing.Size(75, 23);
            this.BTN_Mutiply.TabIndex = 14;
            this.BTN_Mutiply.Text = "A * B";
            this.BTN_Mutiply.UseVisualStyleBackColor = true;
            this.BTN_Mutiply.Click += new System.EventHandler(this.BTN_Mutiply_Click);
            // 
            // BTN_AT
            // 
            this.BTN_AT.Location = new System.Drawing.Point(526, 19);
            this.BTN_AT.Name = "BTN_AT";
            this.BTN_AT.Size = new System.Drawing.Size(75, 23);
            this.BTN_AT.TabIndex = 15;
            this.BTN_AT.Text = "转置";
            this.BTN_AT.UseVisualStyleBackColor = true;
            this.BTN_AT.Click += new System.EventHandler(this.BTN_AT_Click);
            // 
            // BTN_BT
            // 
            this.BTN_BT.Location = new System.Drawing.Point(526, 23);
            this.BTN_BT.Name = "BTN_BT";
            this.BTN_BT.Size = new System.Drawing.Size(75, 23);
            this.BTN_BT.TabIndex = 16;
            this.BTN_BT.Text = "转置";
            this.BTN_BT.UseVisualStyleBackColor = true;
            this.BTN_BT.Click += new System.EventHandler(this.BTN_BT_Click);
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(688, 450);
            this.Controls.Add(this.BTN_Mutiply);
            this.Controls.Add(this.BTN_Minus);
            this.Controls.Add(this.BTN_Add);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.LogBox);
            this.Name = "MainForm";
            this.Text = "矩阵计算器";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        private System.Windows.Forms.TextBox LogBox;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Button BTN_CreateMatrix_A;
        private System.Windows.Forms.TextBox ColumnBox_A;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox RowBox_A;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Button BTN_CreateMatrix_B;
        private System.Windows.Forms.TextBox ColumnBox_B;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox RowBox_B;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Button BTN_Show_A;
        private System.Windows.Forms.Button BTN_Show_B;
        private System.Windows.Forms.Button BTN_Add;
        private System.Windows.Forms.Button BTN_Minus;
        private System.Windows.Forms.Button BTN_Mutiply;
        private System.Windows.Forms.Button BTN_AT;
        private System.Windows.Forms.Button BTN_BT;
    }
}