| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2917 人关注过本帖
标题:这个怎么编呢??编写程序实现两个矩阵相乘???
只看楼主 加入收藏
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
给了源代码都看不懂,我看你这样学习,做一万道题都是白做。首先你知道矩阵乘法手工怎么做没有?知道的话,不可能看不懂的。其实,从你问过正弦又继续问余弦来看,证明你根本没有从正弦那个题目中学到任何东西,说白学没有冤枉你的。

[ 本帖最后由 TonyDeng 于 2012-1-7 22:02 编辑 ]

授人以渔,不授人以鱼。
2012-01-07 21:40
Tiger5392
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:88
帖 子:2775
专家分:2237
注 册:2006-5-17
收藏
得分:10 
*两个矩阵的和
m=3  &&假定两个矩阵A和B都是3行4列,则得到的矩阵C=A+B也应该是3行4列
n=4
dimension a(m,n),b(m,n),c(m,n)
store 0 to a,b,c
for i=1 to m
  for j=1 to n
    input "请输入矩阵A("+alltrim(str(i))+","+alltrim(str(j))+")" to a(i,j)
  endfor
endfor
for i=1 to m
  for j=1 to n
    input "请输入矩阵B("+alltrim(str(i))+","+alltrim(str(j))+")" to b(i,j)
    c(i,j)=a(i,j)+b(i,j)
  endfor
endfor
for i=1 to m
  ?
  for j=1 to n
    ??a(i,j)
  endfor
endfor
for i=1 to m
  ?
  for j=1 to n
    ??b(i,j)
  endfor
endfor
for i=1 to m
  ?
  for j=1 to n
    ??c(i,j)
  endfor
endfor
return

*两个矩阵的积
m=3 &&假定矩阵A为3行4列,B为4行5列,则矩阵C=AB必定为3行5列
n=4
k=5
dimension a(m,n),b(n,k),c(m,k)
for i=1 to m
  for j=1 to n
    input "请输入矩阵A("+alltrim(str(i))+","+alltrim(str(j))+")" to a(i,j)
  endfor
endfor
for i=1 to n
  for j=1 to k
    input "请输入矩阵B("+alltrim(str(i))+","+alltrim(str(j))+")" to b(i,j)
  endfor
endfor
for i=1 to m
  for j=1 to k
    c(i,j)=0
    for p=1 to n
      c(i,j)=c(i,j)+a(i,p)*b(p,k)
    endfor
  endfor
endfor
for i=1 to m
  ?
  for j=1 to n
    ??a(i,j)
  endfor
endfor
for i=1 to n
  ?
  for j=1 to k
    ??b(i,j)
  endfor
endfor
for i=1 to m
  ?
  for j=1 to k
    ??c(i,j)
  endfor
endfor
return

感言:学以致用。 博客:http://www./blog/user14/65009/index.shtml email:Tiger5392@
2012-01-08 16:13
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
以下是引用软件服务在2012-1-7 15:57:08的发言:

ExportToArray?
tcMatrixA 和 tcMatrixB 反转, 好象会变成不同的效果
对呀,矩阵乘法交换律不成立的,AB≠BA。

授人以渔,不授人以鱼。
2012-01-08 21:01
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 


程序代码:
SET DEFAULT TO SYS(2023)
SET CONFIRM ON
SET DECIMALS TO 4
CLEAR ALL
CLOSE DATABASES ALL
goForm = CREATEOBJECT("C_Form")
goForm.Show
READ EVENTS
CLOSE DATABASES ALL
CLEAR ALL
SET DEFAULT TO
RETURN 

PROCEDURE CreateMatrixTable(tcTableName AS Character, tnRows AS Integer, tnCols AS Integer)
    LOCAL lcString AS Character
    LOCAL lnRow AS Interger, lnColumn AS Integer
   
    lcString = "(R C(3), "
    FOR lnColumn = 1 TO tnCols
        lcString = lcString + "C" + PADL(lnColumn, 2, '0') + " N(10.4)" + IIF(lnColumn < tnCols, ", ", ")")
    NEXT
    CREATE TABLE (tcTableName) FREE &lcString
    SELECT (tcTableName)
    FOR lnRow = 1 TO tnRows
        APPEND BLANK
        REPLACE R WITH "R" + PADL(lnRow, 2, '0')
    NEXT
    USE IN (tcTableName)
   
ENDPROC 

PROCEDURE ExportToArray(tcTable AS Character, taArray[])
    LOCAL i AS Integer, j AS Integer
   
    GOTO TOP IN (tcTable)
    i = 1
    DO WHILE !EOF(tcTable)
        FOR j = 2 TO FCOUNT(tcTable)
            taArray[i,j-1] = EVALUATE(tcTable + "." + FIELD(j, tcTable))
        NEXT
        SKIP IN (tcTable)
        i = i + 1
        IF !EOF(tcTable)
            DIMENSION taArray[i,FCOUNT(tcTable)-1]
        ENDIF
    ENDDO
   
ENDPROC 

PROCEDURE Do_Matrix_Multiplication(tcMatrixA AS Character, tcMatrixB AS Character, tcResult AS Character)

    LOCAL arrayA[1,FCOUNT(tcMatrixA)-1], arrayB[1,FCOUNT(tcMatrixB)-1]
    ExportToArray(tcMatrixA, @arrayA)
    ExportToArray(tcMatrixB, @arrayB)
   
    LOCAL i, j, k
    SELECT (tcResult)
    LOCAL lcField AS Character
    LOCAL arrayC[ALEN(arrayA,1),ALEN(arrayB,2)]
    FOR i = 1 TO ALEN(arrayC,1)
        FOR j = 1 TO ALEN(arrayC,2)
            arrayC[i,j] = 0.0
            FOR k = 1 TO ALEN(arrayA,2)
                arrayC[i,j] = arrayC[i,j] + arrayA[i,k] * arrayB[k,j]
            NEXT
            GOTO i IN (tcResult)
            lcField = tcResult + "." + FIELD(j + 1, tcResult)
            REPLACE (lcField) WITH arrayC[i,j] IN (tcResult)
        NEXT
    NEXT
   
ENDPROC 

DEFINE CLASS C_Form AS Form
    Caption = "矩阵乘积"
    ControlBox = .F.
    BorderStyle = 1
    KeyPreview = .T.
    Width = 800
    Height = 600
    ShowTips = .T.
    AutoCenter = .T.
   
    ADD OBJECT cntMatrix1 AS C_GetMatrix WITH MatrixName = "矩阵 A", MatrixDBF = "M1"
    ADD OBJECT cntMatrix2 AS C_GetMatrix WITH MatrixName = "矩阵 B", MatrixDBF = "M2"

    ADD OBJECT cmdCalculation AS CommandButton WITH Caption = "运算", Height = 25, Width = 60
    ADD OBJECT cmdExit        AS CommandButton WITH Caption = "结束", Height = 25, Width = 60, ToolTipText = "按Esc键也可结束程序"
   
    PROCEDURE Activate
        WITH ThisForm.cmdCalculation
            .Top = ThisForm.Height - .Height - 5
            .Left = 5
        ENDWITH
        WITH ThisForm.cmdExit
            .Top = ThisForm.cmdCalculation.Top
            .Left = ThisForm.cmdCalculation.Left + ThisForm.cmdCalculation.Width
        ENDWITH
        WITH Top = 5
            .Left = 5
            .Height = (ThisForm.cmdCalculation.Top - .Top) / 2 - 5
            .Width = 300
        ENDWITH
        WITH Top = Top +  + 5
            .Left = 5
            .Height = ThisForm.cmdCalculation.Top - .Top - 5
            .Width = 300
        ENDWITH
    ENDPROC
   
    PROCEDURE Destroy
        CLEAR EVENTS
    ENDPROC
   
    #DEFINE K_ESC    27
    PROCEDURE KeyPress(tnKeyCode AS Integer, tnShiftAltCtrl AS Integer)
        IF tnKeyCode == K_ESC
            RELEASE ThisForm
        ENDIF
    ENDPROC
    #UNDEFINE K_ESC
   
    PROCEDURE cmdCalculation.Click
        LOCAL lcResultName AS Character
       
        lcResultName = "Result"
        WITH ThisForm
            IF (.cntMatrix1.Text1.Value > 0) .AND. (.cntMatrix1.Text2.Value > 0) .AND. (.cntMatrix2.Text1.Value > 0) .AND. (.cntMatrix2.Text2.Value > 0)
                IF .cntMatrix1.Text2.Value == .cntMatrix2.Text1.Value
                    IF VARTYPE(.lblResult) != "O"
                        .AddObject("lblResult", "Label")
                        WITH .lblResult
                            .Caption = "矩阵 AB"
                            .FontSize = 12
                            .FontBold = .T.
                            .AutoSize = .T.
                            .Top = 5
                            .Left = Left +  + 5
                            .Visible = .T.
                        ENDWITH
                    ENDIF
                    CreateMatrixTable(lcResultName, .cntMatrix1.Text1.Value, .cntMatrix2.Text2.Value)
                    IF USED(lcResultName)
                        USE IN (lcResultName)
                    ENDIF
                    USE (lcResultName) EXCLUSIVE IN 0
                    Do_Matrix_Multiplication(ALLTRIM(.cntMatrix1.MatrixDBF), ALLTRIM(.cntMatrix2.MatrixDBF), lcResultName)
                    SELECT (lcResultName)
                    GOTO TOP
                    IF VARTYPE(.grdResult) == "O"
                        .RemoveObject("grdResult")
                    ENDIF
                    .AddObject("grdResult", "MatrixGrid")
                    WITH .grdResult
                        .Set_Data(lcResultName)
                        .ReadOnly = .T.
                        .Top = ThisForm.lblResult.Top + ThisForm.lblResult.Height + 5
                        .Left = ThisForm.lblResult.Left
                        .Height = ThisForm.cmdCalculation.Top - .Top - 5
                        .Width = ThisForm.Width - .Left - 5
                        .Visible = .T.
                    ENDWITH
                ELSE
                    MESSAGEBOX("两矩阵的行列数不符合乘法运算条件!", 16, .Caption)
                ENDIF
            ELSE
                MESSAGEBOX("数据未输入完毕,无法运算!", 16, .Caption)
            ENDIF
        ENDWITH
       
    ENDPROC
   
    PROCEDURE cmdExit.Click
        RELEASE ThisForm
    ENDPROC
   
ENDDEFINE 

DEFINE CLASS MatrixGrid AS Grid
    DeleteMark = .T.
    RecordSourceType = 1

    PROCEDURE Set_Data(tcAlias AS Character )
        LOCAL lnColumn AS Integer
       
        WITH This
            .RecordSource = tcAlias
            .ColumnCount = FCOUNT(tcAlias)
            FOR lnColumn = 1 TO .ColumnCount
                WITH .Columns(lnColumn)
                    WITH .Header1
                        .Caption = IIF(lnColumn > 1, FIELD(lnColumn), "")
                        .FontBold = .T.
                        .Alignment = 2
                    ENDWITH
                    .ControlSource = tcAlias + "." + FIELD(lnColumn)
                    IF lnColumn > 1
                        .InputMask = "99999.9999"
                    ELSE
                        .Width = 40
                        .FontBold = .T.
                        .Alignment = 2
                        .Enabled = .F.
                    ENDIF
                ENDWITH
            NEXT
        ENDWITH
       
    ENDPROC 

ENDDEFINE 

DEFINE CLASS C_GetMatrix AS Container
   
    MatrixName = ""
    MatrixDBF = ""
   
    ADD OBJECT lblMatrixName AS Label WITH Height = 25, AutoSize = .T., FontSize = 12, FontBold = .T.
   
    ADD OBJECT Label1 AS Label   WITH Caption = "行数: ", Height = 25, Width = 40, Alignment = 1
    ADD OBJECT Text1  AS TextBox WITH Value = 0, Height = 25, Width = 40, InputMask = "99", SelectOnEntry = .T.
    ADD OBJECT Label2 AS Label   WITH Caption = "列数: ", Height = 25, Width = 40, Alignment = 1
    ADD OBJECT Text2  AS TextBox WITH Value = 0, Height = 25, Width = 40, InputMask = "99", SelectOnEntry = .T.
   
    PROCEDURE MatrixName_Assign(tcName AS Character)
        This.MatrixName = ALLTRIM(tcName)
        This.lblMatrixName.Caption = This.MatrixName
    ENDPROC
   
    PROCEDURE Init
        WITH This.lblMatrixName
            .Caption = This.MatrixName
            .Top = 5
            .Left = 5
        ENDWITH
        WITH This.Label1
            .Top = 40
            .Left = 5
        ENDWITH
        WITH This.Text1
            .Top = This.Label1.Top
            .Left = This.Label1.Left + This.Label1.Width
        ENDWITH
        WITH This.Label2
            .Top = This.Label1.Top
            .Left = This.Text1.Left + This.Text1.Width + 5
        ENDWITH
        WITH This.Text2
            .Top = This.Label2.Top
            .Left = This.Label2.Left + This.Label2.Width
        ENDWITH
    ENDPROC
   
    PROCEDURE Text1.Valid
        IF This.Value > 0
            This.Parent.InputData
        ELSE
            IF This.Value < 0
                RETURN .F.
            ENDIF
        ENDIF
    ENDPROC
   
    PROCEDURE Text2.Valid
        IF This.Value > 0
            This.Parent.InputData
        ELSE
            IF This.Value < 0
                RETURN .F.
            ENDIF
        ENDIF
    ENDPROC
   
    PROCEDURE InputData
        LOCAL lcAlias AS Character
        LOCAL lnRow AS Integer, lnColumn AS Integer
        LOCAL lcString AS Character
       
        IF (This.Text1.Value <= 0) .OR. (This.Text2.Value <= 0)
            RETURN
        ENDIF
       
        lcAlias = ALLTRIM(This.MatrixDBF)
        IF USED(lcAlias)
            USE IN (lcAlias)
        ENDIF
        CreateMatrixTable(lcAlias, This.Text1.Value, This.Text2.Value)
        USE (lcAlias) EXCLUSIVE IN 0
        SELECT (lcAlias)
        GOTO TOP
        WITH This
            IF VARTYPE(This.Grid1) == "O"
                .RemoveObject("Grid1")
            ENDIF
            .AddObject("Grid1", "MatrixGrid")
            WITH .Grid1
                .Set_Data(lcAlias)
                .Top = This.Label2.Top + This.Label2.Height + 5
                .Left = 5
                .Height = This.Height - .Top - 5
                .Width = This.Width - .Left - 5
                .Visible = .T.
            ENDWITH
        ENDWITH
    ENDPROC
   
ENDDEFINE

授人以渔,不授人以鱼。
2012-01-08 23:46
软件服务
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:344
专家分:736
注 册:2011-12-23
收藏
得分:0 
回复 14楼 TonyDeng
眼前一亮
2012-01-09 00:06
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:0 
回复 14楼 TonyDeng
高,学习了
2012-01-09 09:53
c530837027
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2011-12-8
收藏
得分:0 
回复 11楼 TonyDeng
谢谢你的批评,我学了多少我其实有数的,比如说算数的相加其实我没问题了也。。。我问了sin然后问 cos只是为了确定一下编的对不对,毕竟这些题我没有标准答案,
我也没可以问的老师。。。
2012-01-09 19:53
c530837027
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2011-12-8
收藏
得分:0 
回复 11楼 TonyDeng
谢谢哦,懂了
2012-01-09 19:54
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
我的語氣可能重了一些。不過,我看你應該好好從基礎入手才成,不要一起手就做算法(而且你的這些算法題其實不是VFP的,因為對這門語言來說,你的題本來就不是題,特別是那道字符串相加的),首先搞清楚順序、循環、判斷語句的用法,其次瞭解變量的定義及其作用域、語言中慣用的函數等等,接下來才是算法。

授人以渔,不授人以鱼。
2012-01-09 20:07
快速回复:这个怎么编呢??编写程序实现两个矩阵相乘???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037206 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved