一个关于数组的问题????

metalwaves 2002-12-30 04:12:17
大家好,我是一个vb的初学者,我在用vb做一个小程序的时候碰到了一个小问题:要实现两个数组元素之间的交、并、差运算,用for循环通过数组比较可以解决交运算,但是在并和差运算的时候一直得不到期望的的结果,例如;

Dim i As Integer, j As Integer, k As Integer, c(1 To 10) As String
For i = 1 To 10
For j = 1 To 10
If a(i) <> b(j) Then //a、b数组为一个通过inputbox接收输入的数组,大小为10
c(i)=a(i)
End If
Next j
Next i

请问各位有没有其他的方法实现类似的并和差运算呢?请指教,谢谢
...全文
13 点赞 收藏 16
写回复
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
metalwaves 2003-01-04
ok
that's all right
回复
xks 2003-01-02
差不多了

应该可以实现了

Private Sub Form_Load()
Dim a(1 To 10) As Integer, b(1 To 10) As Integer, c(1 To 20) As Integer
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 10
a(i) = i
b(i) = 5 + i
Next
Dim Count As Integer, Add As Integer
For i = 1 To 10
c(i) = a(i)
Next
Count = 10
For i = 1 To 10
Add = 0
For j = 1 To 10
If b(i) = a(j) Then
Add = 1
Exit For
End If
Next j
If Add = 0 Then
Count = Count + 1
c(Count) = b(i)
End If
Next i
For i = 1 To 20
Text1.Text = Text1.Text + " " + Trim(Str(c(i)))
Next
End Sub
回复
chenyu5188 2003-01-02
UP
这些算法的名称是什么哈?
回复
redwrite 2003-01-02
呵呵,少加了一个end if

Private Sub Form_Load()
Dim a(1 To 10) As Integer, b(1 To 10) As Integer, c(1 To 20) As Integer
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 10
a(i) = i
b(i) = 5 + i
Next
Dim Count As Integer, Add As Integer
For i = 1 To 10
c(i) = a(i)
Next
Count = 10
For i = 1 To 10
Add = 0
For j = 1 To 10
If b(i) = a(j) Then
Add = 1
Exit For
End If
Next j
If Add = 0 Then
Count = Count + 1
c(Count) = b(i)
End If
Next i
For i = 1 To 20
Text1.Text = Text1.Text + " " + Trim(Str(c(i)))
Next
End Sub

这次可以了,这个方法前提是两个数组自身的数据是不相同的。
回复
redwrite 2003-01-02
不会吧,我调试一下看看
回复
metalwaves 2003-01-02
to redwrite(红妆素裹):
奇怪的是按照这个算法得到的输出应该是a与b的和集。但是调试中输出的结果却是简单的把a与b数组相加,两数组中重复的元素也照样重复输出了?
到底是怎么回事呢?
回复
redwrite 2003-01-01
这次应该没有什么问题了吧
Dim i As Integer, j As Integer, k As Integer, c(1 To 20) As String
Dim Count As Integer, Add As Integer,my
For i = 1 To 10
c(i) = a(i)
Next
Count = 10
For i = 1 To 10
Add = 0
For j = 1 To 10
If b(i)=a(j) Then Add = 1
Exit For
Next j
If Add = 0 Then
Count = Count + 1
c(Count) = b(i)
End If
next i

这个方法前提是两个数组自身的数据是不相同的。
回复
metalwaves 2003-01-01
和运算还是搞不定,又没有什么更好的算法了呢?
回复
metalwaves 2002-12-30
我把和运算修改了一下,不过好像还是不对,结果只有原来a数组的值?
Dim i As Integer, j As Integer, k As Integer, c(1 To 20) As String
Dim Count As Integer, Add As Integer
For i = 1 To 10
c(i) = a(i)
Next
Count = 10
For i = 1 To 10
Add = 0
For j = 1 To 10
If a(i) = b(j) Then Add = 1
Exit For
Next j
If Add = 0 Then
Count = Count + 1
c(Count) = b(j)
End If
回复
metalwaves 2002-12-30
谢谢楼上的兄弟
交和差运算已经实现了,不过redwrite(红妆素裹) 提供的和运算好像有点问题?
不单循环变量位置不对,c(count)=a(i)这句应该改为c(count)=b(j)吧?
回复
redwrite 2002-12-30
呵呵,可能写得仓促了一些。
其实算法是最重要的,只要思路清淅正确,再在机器上调试一下,就会得到正确的结果。
回复
AechoJohn 2002-12-30
上面的老哥,循环变量位置错了吧!
补一下差的运算
Dim i As Integer, j As Integer, k As Integer, c(1 To 10) As String
dim Count as integer,Add as integer

count = 0
For i = 1 To 10
Add=0
for j=1 to 10
If a(i) = b(j) Then
Add=1
exit for
end if
Next j
if Add=0 then
count=count+1
c(count)=a(i)
end if
next i

上面C中的数据为A-B的,这要事先保证A、B同一个空间,并且A要大于B
回复
redwrite 2002-12-30

Dim i As Integer, j As Integer, k As Integer, c(1 To 20) As String
dim Count as integer
count=0
For i = 1 To 10
for j=1 to 10
If a(i) = b(j) Then
c(count)=a(i)
count=count+1
exit for
End If
next j
Next i


Dim i As Integer, j As Integer, k As Integer, c(1 To 20) As String
dim Count as integer,Add as integer
for i=1 to 10
c(i)=a(i)
next
count=10
For i = 1 To 10
Add=0
for j=1 to 10
If a(i) = b(j) Then Add=1
Next i
if Add=0 then
count=count+1
c(count)=a(i)
end if
next j
结果在数据C里


差的话要复杂一些了
回复
metalwaves 2002-12-30
楼上的好像不对吧,这样比较的结果是把a,b中每一个元素逐个比较,但是要求的是对两个数组的元素进行类似集合的运算比较啊
回复

Dim i As Integer, j As Integer, k As Integer, c(1 To 10) As String
For i = 1 To 10
If a(i) <> b(i) Then
c(i)=a(i)
End If
Next i

Dim i As Integer, j As Integer, k As Integer, c(1 To 10) As String
For i = 1 To 10
If a(i) <> b(i) Then
c(i)=a(i)+b(i)
End If
Next i

Dim i As Integer, j As Integer, k As Integer, c(1 To 10) As String
For i = 1 To 10
If a(i) <> b(i) Then
c(i)=a(i)-b(i)
End If
Next i
回复

Dim i As Integer, j As Integer, k As Integer, c(1 To 10) As String
For i = 1 To 10
If a(i) <> b(i) Then
c(i)=a(i)
End If
Next i

Dim i As Integer, j As Integer, k As Integer, c(1 To 10) As String
For i = 1 To 10
If a(i) <> b(i) Then
c(i)=a(i)+b(i)
End If
Next i

Dim i As Integer, j As Integer, k As Integer, c(1 To 10) As String
For i = 1 To 10
If a(i) <> b(i) Then
c(i)=a(i)-b(i)
End If
Next i
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7450

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告