文号
t20497
阅读
19564
回复
11
收藏
5
[求助] 用VB数组解决矩阵乘法
2009/10/30
IP:江苏
我尝试用VB的数组编写一个3*3矩阵计算器,可是在遇到表达式的时候,要么卡壳,要么得到一些莫名其妙的结果。麻烦会得分帮忙看一下那里出问题了,应该怎么改。谢谢!

本程序的功能除了实现矩阵乘法,还要求出每个矩阵的行列式。(通过矩阵下方的“”det()=按实现)

我已经用C语言实现了这种算法,代码如下:
#include <stdio.h>
#include <math.h>
void main()
{
  #define M 3 /*left row*/
  #define S 3 /*left column & right row*/
  #define N 3 /*right column*/

  int a[M][S],b[S][N],c[M][N]={0,0,0}/*Adjust the number of "0" when n is changed!*/;
  int i,j,s;
  char ch='Y';

  do{

  printf("Input the elements of the left matrix by row.\n");
  for(i=0;i<M;i++)
  {
    for(j=0;j<S;j++)
    {
      scanf("%d",&a[i][j]);
    }
    printf("\n");
  }

  printf("Input the elements of the right matrix by row.\n");
  for(i=0;i<S;i++)
  {
    for(j=0;j<N;j++)
    {
      scanf("%d",&b[i][j]);
    }
    printf("\n");
  }

  printf("The product is:\n");
  for(i=0;i<M;i++)
  {
    for(j=0;j<N;j++)
    {
      for(s=0;s<S;s++)
      {
         c[i][j]+=a[i][s]*b[s][j];
      }
      printf("%d\t",c[i][j]);
    }
    printf("\n");
  }

  printf("Continue?(Y/N)\n");
  ch=getchar();
  
  }while(ch=='Y'||ch=='y');
} product.jpg

attachment icon matrix1.rar 2.10KB RAR 33次下载
product2.jpg
来自:
软件综合
11
carneades作者
17年2个月前IP:未同步
161573
3楼
谢谢楼上诸位的关注!

学了C语言后就觉得VB的语句忒啰嗦。
我最近学习线性代数,同学们苦于矩阵运算的麻烦,希望我给他们做一个程序。
我现在用最土得办法——把行列式展开成代数式求解,这居然还卡壳!

Private Sub cmda_Click()
    Dim a11 As Integer, a12 As Integer, a13 As Integer
    Dim a21 As Integer, a22 As Integer, a23 As Integer
    Dim a31 As Integer, a32 As Integer, a33 As Integer
    Dim dta As Integer
    
    a11 = Val(a11.Text): a12 = Val(a12.Text): a13 = Val(a13.Text)
    a21 = Val(a21.Text): a22 = Val(a22.Text): a23 = Val(a23.Text)
    a31 = Val(a31.Text): a32 = Val(a32.Text): a33 = Val(a33.Text)
    
    dta = (a11 * a22 * a33) + (a12 * a23 * a31) + (a21 * a32 * a13) - (a31 * a22 * a13) - (a21 * a12 * a33) - (a32 * a23 * a11)
    
    dta.Text = Str(dta)
End Sub

在运行这段语句的时候,卡在a11 = Val(a11.Text): 这里了。说什么“编译错误,无效限定符”。
控件的名字弄得跟变量一样要紧吗?
引用
评论
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的