矩阵类的设计

2024-12-12 22:13:50
推荐回答(3个)
回答1:

{矩阵乘法}
program matrix;
const
maxn=100;
var
a,b,c:array[1..maxn,1..maxn]of integer;
i,j,k,n1,m1,n2,m2:integer;
begin

readln(n1,m1); {A矩阵的行数n1和列数m1}
for i:=1 to n1 do {读入矩阵A}
begin
for j:=1 to m1 do read(a[i,j]);
readln;
end;
readln(n2,m2); {B矩阵的行数n2和列数m2(由于要进行矩阵乘法,故必须m1=n2)}
for i:=1 to n2 do {读入矩阵B}
begin
for j:=1 to m2 do read(b[i,j]);
readln;
end;

for i:=1 to n1 do
for j:=1 to m2 do
for k:=1 to n2 do
c[i,j]:=c[i,j]+a[i,k]*b[k,j];
{矩阵乘法的计算公式:a[i,j]:=∑(k=0→m1/n2) a[i,k]*a[k,j]}

for i:=1 to n1 do {输出矩阵C}
begin
for j:=1 to m2 do write(c[i,j],' ');
writeln;
end;

end.

{例:
输入:
2 3 ========>n1,m1
1 2 5
3 4 6
3 4 ========>n2,m2
10 1 2 0
8 3 4 7
7 5 6 9
输出:
61 32 40 59
104 45 58 82
注:n*m的矩阵与m*t的矩阵相乘,得到一个n*t的矩阵}

回答2:

输入矩阵:就是输入多维数组。
例如:
Option Base 1
Private Sub Form_Click()
Dim n1(3, 5) As Integer, n2(3, 5) As Integer, n3(3, 5) As Integer
For i = 1 To 3
For j = 1 To 5
n1(i, j) = Val(InputBox(""))
Next j
Next i
print n1; ""
For i = 1 To 3
For j = 1 To 5
n2(i, j) = Val(InputBox(""))
Next j
Next i
print n2; ""
For i = 1 To 3
For j = 1 To 5
n3(i, j) = n1(i, j) + n2(i, j) '表示矩阵加法
Next j
Next i
Print "打印输出数组n3"
For i = 1 To 3
For j = 1 To 5
Print n3(i, j); "" '表示输出打印数组n3
Next j
Next i
Print '表示换行
End Sub

同样的道理数组的乘法公式:
如:C11=A11*B11+A12*B21
C12=A11*B12+A12*B22
举个例子:
a矩阵的各元素 b矩阵的各元素
1 2 1 2 3
3 4 4 5 6
5 6
7 8
求以上两个矩阵相乘之后的c矩阵
Option Base 1
Private Sub form1_click()
Dim a(4, 2) As Integer, b(2, 3) As Integer
Dim c(4, 3) As Integer

For i = 1 To 4
For j = 1 To 2
a(i, j) = Val(inputbxo("输入a数组"))
Next j
Next i
For i = 1 To 2
For j = 1 To 3
b(i, j) = Val(inputbxo("输入b数组"))
Next j
Next i
For i = 1 To 4
For j = 1 To 3
c(i, j) = 0
For k = 1 To 2
c(i, j) = c(i, j) + a(i, k) * b(k, j)
Next k
Next j
Next i
Print "输出c数组"
For i = 1 To 4
For j = 1 To 3
Print c(i, j); "";
Next j
Print
Next i
End Sub

回答3:

去csdn找下,csdn的博客也可以找,下载那可以找相关资料找找
网~~站:
www.csdn.net
下载:download.csdn.net
注册个账号就能下载
博客:blog.csdn.net