sub MatrixStructureChange() htmMatrixB.all("row").value=htmMatrixA.all("column").value htmMatrixC.all("row").value=htmMatrixA.all("row").value htmMatrixC.all("Column").value=htmMatrixB.all("column").value GenerateMatrixStructure end sub
sub GenerateMatrixStructure() dim iMatrixARow,iMatrixBRow,iMatrixCRow,iMatrixAColumn,iMatrixBColumn,iMatrixCColumn iMatrixARow=htmMatrixA.all("Row").value iMatrixBRow=htmMatrixB.all("Row").value iMatrixCRow=htmMatrixC.all("Row").value iMatrixAColumn=htmMatrixA.all("Column").value iMatrixBColumn=htmMatrixB.all("Column").value iMatrixCColumn=htmMatrixC.all("Column").value
dim iRow,iColumn,strTmp strTmp="" for iRow=1 to iMatrixARow for iColumn=1 to iMatrixAColumn strTmp=strTmp & "<input MaxLength=3 class='cssMatrixItem' value=0 onchange='CheckItem()' id='Item" & iRow & iColumn & "'></input>" next strTmp=strTmp & "<br>" next htmMatrixAInput.innerhtml=strTmp
strTmp="" for iRow=1 to iMatrixBRow for iColumn=1 to iMatrixBColumn strTmp=strTmp & "<input MaxLength=3 class='cssMatrixItem' value=0 onchange='CheckItem()' id='Item" & iRow & iColumn & "'></input>" next strTmp=strTmp & "<br>" next htmMatrixBInput.innerhtml=strTmp strTmp="" for iRow=1 to iMatrixCRow for iColumn=1 to iMatrixCColumn strTmp=strTmp & "<input readonly class='cssMatrixItem' style='width:40;' onchange='CheckItem()' value=0 id='Item" & iRow & iColumn & "'></input>" next strTmp=strTmp & "<br>" next htmMatrixCInput.innerhtml=strTmp end sub
sub CheckItem() if Not(IsNumeric(window.event.srcelement.value)) then window.event.srcelement.value=0 else window.event.srcelement.value=cint(window.event.srcelement.value) end if
end sub
sub CaculateMatrix() dim iMatrixARow,iMatrixBRow,iMatrixCRow,iMatrixAColumn,iMatrixBColumn,iMatrixCColumn iMatrixARow=htmMatrixA.all("Row").value iMatrixBRow=htmMatrixB.all("Row").value iMatrixCRow=htmMatrixC.all("Row").value iMatrixAColumn=htmMatrixA.all("Column").value iMatrixBColumn=htmMatrixB.all("Column").value iMatrixCColumn=htmMatrixC.all("Column").value
letMatrixInputValue "htmMatrixC",aryiMatrixC end sub
function MatrixMultiply(byref iaryA,byref iaryB,byref iaryC) '计算矩阵尺寸 dim iAMaxRow,iAMaxColumn,iBMaxRow,iBMaxColumn,iCMaxRow,iCMaxColumn iAMaxRow=ubound(iaryA,1)+1 iAMaxColumn=ubound(iaryA,2)+1
'判定矩阵是否符合乘法要求 if iAMaxRow<1 or iAMaxColumn<1 or iBMaxRow<1 or iBMaxColumn<1 or iCMaxRow<1 or iCMaxColumn<1 then MatrixMutiply=False exit function end if if iAMaxColumn<>iBMaxRow or iCMaxRow<>iAMaxRow or iCMaxColumn<>iBMaxColumn then MatrixMutiply=False exit function end if '计算 dim iCColumn,iCRow,iAColumn dim iTmp for iCRow=0 to iCMaxRow-1 for iCColumn=0 to iCMaxColumn-1 iTmp=0 for iAColumn=0 to iAMaxColumn-1 iTmp=iTmp + iaryA(iCRow,iAColumn) * iaryB(iAColumn,iCColumn) next iaryC(iCRow,iCColumn)=iTmp next next
MatrixMultiply=True end function
sub GetMatrixInputValue(strhtmMatrixID,byref iaryMatrix) dim iMatrixRow,iMatrixColumn iMatrixRow=document.all(strHtmMatrixID).all("Row").value iMatrixColumn=document.all(strHtmMatrixID).all("Column").value
dim iRow,iColumn for iRow=1 to iMatrixRow for iColumn=1 to iMatrixColumn iaryMatrix(iRow-1,iColumn-1)=cint(document.all(strHtmMatrixID&"Input").all("Item"&cstr(iRow)&cstr(iColumn)).value) next next end sub sub LetMatrixInputValue(strHtmMatrixID,byref iaryMatrix) dim iMatrixRow,iMatrixColumn iMatrixRow=document.all(strHtmMatrixID).all("Row").value iMatrixColumn=document.all(strHtmMatrixID).all("Column").value
dim iRow,iColumn for iRow=1 to iMatrixRow for iColumn=1 to iMatrixColumn document.all(strHtmMatrixID&"Input").all("Item"&cstr(iRow)&cstr(iColumn)).value=iaryMatrix(iRow-1,iColumn-1) next next end sub
sub ResetMatrixInputValue(strHtmMatrixID,byref iaryMatrix) dim iMatrixRow,iMatrixColumn iMatrixRow=document.all(strHtmMatrixID).all("Row").value iMatrixColumn=document.all(strHtmMatrixID).all("Column").value
dim iRow,iColumn for iRow=1 to iMatrixRow for iColumn=1 to iMatrixColumn iaryMatrix(iRow-1,iColumn-1)=0 next next end sub </script>
'用定义做的,所以如果矩阵太大会死敲敲。
'保存为*.hta,点击运行,vbs和VB完全一样语法。<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<style>
span{font-size:10pt; color:blue;}
.cssMatrixItem{width:30; height:20; border:1 solid white; background:menu;}
</style>
</head><body>
<span id="htmMatrixA" style="border:2 solid menu; background:menu;">
<span>定义矩阵A</span>
<select id="Row" onChange="MatrixStructureChange()">
<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option>
</select>
<select id="Column" onChange="MatrixStructureChange()" >
<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option>
</select>
</span >
<span id="htmMatrixB" style="border:2 solid menu; background:menu;">
<span>定义矩阵B</span>
<select id="Row" disabled>
<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option>
</select>
<select id="Column" onChange="MatrixStructureChange()">
<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option>
</select>
</span >
=
<span id="htmMatrixC" style="border:2 solid menu; background:menu;">
<span>结果矩阵C</span>
<select id="Row" disabled>
<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option>
</select>
<select id="Column" disabled>
<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option>
</select>
</span >
<input type="Button" value=" 生成矩阵 " onclick="GenerateMatrixStructure()">
<hr>
<table >
<tr>
<td style="font-size:30pt;">(</td>
<td><span id="htmMatrixAInput">A</span></td>
<td style="font-size:30pt;">)X(</td>
<td><span id="htmMatrixBInput">B</span></td>
<td style="font-size:30pt;">)=(</td>
<td><span id="htmMatrixCInput">C</span></td>
<td style="font-size:30pt;">)</td>
</tr>
</table><hr>
<input value=" 计 算 " type=button onclick="CaculateMatrix()">
</body>
<script language=vbs>
option explicit
sub document_onReadyStateChange()
GenerateMatrixStructure
end sub
sub MatrixStructureChange()
htmMatrixB.all("row").value=htmMatrixA.all("column").value
htmMatrixC.all("row").value=htmMatrixA.all("row").value
htmMatrixC.all("Column").value=htmMatrixB.all("column").value
GenerateMatrixStructure
end sub
sub GenerateMatrixStructure()
dim iMatrixARow,iMatrixBRow,iMatrixCRow,iMatrixAColumn,iMatrixBColumn,iMatrixCColumn
iMatrixARow=htmMatrixA.all("Row").value
iMatrixBRow=htmMatrixB.all("Row").value
iMatrixCRow=htmMatrixC.all("Row").value
iMatrixAColumn=htmMatrixA.all("Column").value
iMatrixBColumn=htmMatrixB.all("Column").value
iMatrixCColumn=htmMatrixC.all("Column").value
dim iRow,iColumn,strTmp strTmp=""
for iRow=1 to iMatrixARow
for iColumn=1 to iMatrixAColumn
strTmp=strTmp & "<input MaxLength=3 class='cssMatrixItem' value=0 onchange='CheckItem()' id='Item" & iRow & iColumn & "'></input>"
next
strTmp=strTmp & "<br>"
next
htmMatrixAInput.innerhtml=strTmp
strTmp=""
for iRow=1 to iMatrixBRow
for iColumn=1 to iMatrixBColumn
strTmp=strTmp & "<input MaxLength=3 class='cssMatrixItem' value=0 onchange='CheckItem()' id='Item" & iRow & iColumn & "'></input>"
next
strTmp=strTmp & "<br>"
next
htmMatrixBInput.innerhtml=strTmp strTmp=""
for iRow=1 to iMatrixCRow
for iColumn=1 to iMatrixCColumn
strTmp=strTmp & "<input readonly class='cssMatrixItem' style='width:40;' onchange='CheckItem()' value=0 id='Item" & iRow & iColumn & "'></input>"
next
strTmp=strTmp & "<br>"
next
htmMatrixCInput.innerhtml=strTmp
end sub
sub CheckItem()
if Not(IsNumeric(window.event.srcelement.value)) then
window.event.srcelement.value=0
else
window.event.srcelement.value=cint(window.event.srcelement.value)
end if
end sub
sub CaculateMatrix()
dim iMatrixARow,iMatrixBRow,iMatrixCRow,iMatrixAColumn,iMatrixBColumn,iMatrixCColumn
iMatrixARow=htmMatrixA.all("Row").value
iMatrixBRow=htmMatrixB.all("Row").value
iMatrixCRow=htmMatrixC.all("Row").value
iMatrixAColumn=htmMatrixA.all("Column").value
iMatrixBColumn=htmMatrixB.all("Column").value
iMatrixCColumn=htmMatrixC.all("Column").value
redim aryiMatrixA(iMatrixARow-1,iMatrixAColumn-1)
redim aryiMatrixB(iMatrixBRow-1,iMatrixBColumn-1)
redim aryiMatrixC(iMatrixCRow-1,iMatrixCColumn-1) GetMatrixInputValue "htmMatrixA",aryiMatrixA
GetMatrixInputValue "htmMatrixB",aryiMatrixB
ResetMatrixInputValue "htmMatrixC",aryiMatrixC
MatrixMultiply aryiMatrixA,aryiMatrixB,aryiMatrixC
letMatrixInputValue "htmMatrixC",aryiMatrixC end sub
function MatrixMultiply(byref iaryA,byref iaryB,byref iaryC)
'计算矩阵尺寸
dim iAMaxRow,iAMaxColumn,iBMaxRow,iBMaxColumn,iCMaxRow,iCMaxColumn
iAMaxRow=ubound(iaryA,1)+1
iAMaxColumn=ubound(iaryA,2)+1
iBMaxRow=ubound(iaryB,1)+1
iBMaxColumn=ubound(iaryB,2)+1
iCMaxRow=ubound(iaryC,1)+1
iCMaxColumn=ubound(iaryC,2)+1
'判定矩阵是否符合乘法要求
if iAMaxRow<1 or iAMaxColumn<1 or iBMaxRow<1 or iBMaxColumn<1 or iCMaxRow<1 or iCMaxColumn<1 then
MatrixMutiply=False
exit function
end if
if iAMaxColumn<>iBMaxRow or iCMaxRow<>iAMaxRow or iCMaxColumn<>iBMaxColumn then
MatrixMutiply=False
exit function
end if '计算
dim iCColumn,iCRow,iAColumn
dim iTmp for iCRow=0 to iCMaxRow-1
for iCColumn=0 to iCMaxColumn-1
iTmp=0
for iAColumn=0 to iAMaxColumn-1
iTmp=iTmp + iaryA(iCRow,iAColumn) * iaryB(iAColumn,iCColumn)
next
iaryC(iCRow,iCColumn)=iTmp
next
next
MatrixMultiply=True
end function
sub GetMatrixInputValue(strhtmMatrixID,byref iaryMatrix)
dim iMatrixRow,iMatrixColumn
iMatrixRow=document.all(strHtmMatrixID).all("Row").value
iMatrixColumn=document.all(strHtmMatrixID).all("Column").value
dim iRow,iColumn
for iRow=1 to iMatrixRow
for iColumn=1 to iMatrixColumn
iaryMatrix(iRow-1,iColumn-1)=cint(document.all(strHtmMatrixID&"Input").all("Item"&cstr(iRow)&cstr(iColumn)).value)
next
next
end sub sub LetMatrixInputValue(strHtmMatrixID,byref iaryMatrix)
dim iMatrixRow,iMatrixColumn
iMatrixRow=document.all(strHtmMatrixID).all("Row").value
iMatrixColumn=document.all(strHtmMatrixID).all("Column").value
dim iRow,iColumn
for iRow=1 to iMatrixRow
for iColumn=1 to iMatrixColumn
document.all(strHtmMatrixID&"Input").all("Item"&cstr(iRow)&cstr(iColumn)).value=iaryMatrix(iRow-1,iColumn-1)
next
next
end sub
sub ResetMatrixInputValue(strHtmMatrixID,byref iaryMatrix)
dim iMatrixRow,iMatrixColumn
iMatrixRow=document.all(strHtmMatrixID).all("Row").value
iMatrixColumn=document.all(strHtmMatrixID).all("Column").value
dim iRow,iColumn
for iRow=1 to iMatrixRow
for iColumn=1 to iMatrixColumn
iaryMatrix(iRow-1,iColumn-1)=0
next
next
end sub
</script>