public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ReadMatrix(ref float[,] maxtrix, TextBox tb)
{
string tpstr;
string[] tpstrs;
//读取A矩阵
tpstr = textBox1.Lines[0];
tpstrs = tpstr.Split(','); //以‘,’为分割
maxtrix = new float[tb.Lines.Length, tpstrs.Length];
for (int i = 0; i < textBox1.Lines.Length; i++)
{
tpstr = tb.Lines[i];
tpstrs = tpstr.Split(',');
for (int j = 0; j < tpstrs.Length; j++)
{
maxtrix[i, j] = Convert.ToSingle(tpstrs[j]);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
float[,] A, B, C;
A = new float[1, 1];
B = new float[1, 1];
ReadMatrix(ref A, textBox1);
ReadMatrix(ref B, textBox2);
C = new float[A.GetLength(0), B.GetLength(1)];
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < B.GetLength(1); j++)
{
for (int k = 0; k < A.GetLength(1); k++)
{
C[i, j] = C[i, j] + (A[i, k] * B[k, j]);
}
textBox3.Text = textBox3.Text + C[i, j].ToString() + ",";
}
textBox3.Text = textBox3.Text + "\n";
}
}
输出结果: