在VB中怎么实现含有次方的 多项式计算如 3X^3+3X加上2X^2+X

1个回答

  • 多项式,如3X^3+3X、2X^2+X,完全可以用数组的每个元素记录其系数来表示,

    如第一个(假设用a()数组),a(3)=3、a(2)=0、a(1)=3、a(0)=0

    如第二个(假设用b()数组),b(2)=2、b(1)=1、b(0)=0

    对于两个多项式运算,加减乘运算参考如下:

    Option Base 0

    '模块级变量a()、b()、c()

    Dim a() As Single,b() As Single,c() As Single

    '多项式加法

    Private Sub Duoxiangshi_Jia()

    Dim Ua As Integer,Ub As Integer,U As Integer,i As Integer

    Ua=Ubound(a()) :Ub=Ubound(b()) :U=IIf(Ua>ub,Ua,ub)

    ReDim c(U)

    For i=0 to Ua

    c(i)=a(i)

    Next i

    For i=0 to Ub

    c(i)=c(i)+b(i)

    Next i

    End Sub

    '多项式减法

    Private Sub Duoxiangshi_Jian()

    '代码同加法,把“+”改为“-”即可

    End Sub

    '多项式乘法

    Private Sub Duoxiangshi_Cheng()

    dim Ua As Integer,Ub As Integer,U As Integer,i As Integer,j As Integer

    Ua=Ubound(a()) :Ub=Ubound(b())

    U=Ua+Ub

    ReDim c(U)

    For i=0 to Ua

    For j=0 to Ub

    c(i+j)=a(i)*b(j)

    Nextj

    Next i

    End Su