重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Dim str As String = ""
创新互联是专业的岑溪网站建设公司,岑溪接单;提供成都网站设计、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行岑溪网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
Dim n, m As Integer
Dim a() As Integer
n = InputBox("请输入要产生随机数的个数n")
m = InputBox("请输入要移动的位数m")
ReDim a(n)
str = "生成数组为" vbCrLf
For i = 1 To n
a(i) = Int(Rnd() * 90 + 10)
str = str a(i) " "
Next
Dim b(m), c(n) As Integer
For i = 1 To m
b(i) = a(n - m + i)
Next
For i = 1 To m
c(i) = b(i)
Next
For i = m + 1 To n
c(i) = a(i - m)
Next
str = str vbCrLf "移动后的数组为" vbCrLf
For i = 1 To n
str = str c(i) " "
Next
TextBox6.Text = str
dim vArray(0 to 100) '假如这是你要移动的数组
'调用这个函数(过程)可以循环位移n位。
private sub ArrayMLeft(byval n as long)
dim i as long, j as long
dim U as long, L as long
dim vTemp
U=ubound(vArray) 'U为数组上限
L=lbound(vArray) 'L为数组下限
for j=1 to n
vTmp=vArray(L) '记录第一个元素
for i=L to U-1
vArray(i)=vArray(i+1) '每一个单元都COPY第i+1个单元
next i
vArray(U)=vTmp '将第一个元素赋值到最后一个数组单元
next j
end sub
这个函数(过程)的优点是可读性比较好,但缺点是效率较低,希望能理解这里的意思,根据实际具体情况改进一下,让它只循环一次即可获得最终结果。
代码: Dim
a(1
To
10)
As
Integer
Private
Sub
Command1_Click()
Randomize
Dim
i
As
Integer
"随机生成10个两位正整数存入数组中:"
For
i
=
1
To
10
a(i)
=
Int(Rnd
*
90)
+
10
a(i);
Next
i
End
SubPrivate
Sub
Command2_Click()
Dim
temp
As
Integer,
i
As
Integer
temp
=
a(1)
For
i
=
1
To
9
a(i)
=
a(i
+
1)
Next
i
a(10)
=
temp
"循环左移一位后:"
For
i
=
1
To
10
a(i);
Next
i
End
Sub
图:
分类: 电脑/网络 程序设计 其他编程语言
问题描述:
请问VB中怎么能向右循环一位,谢谢了
解析:
ithack/doc16415
VB程序中实现字节移位操作
在利用VB系统开发数据采集或工业控制软件,或对文件进行低级操作时,常需要对字节进行移位操作,但VB系统中没有提供字节移位操作的指令和函数,只提供了And(与)、Or(或)、Xor(异或)、Equ(同或)、Not(非)等几个逻辑运算指令。笔者在用VB系统开发工业控制软件的过程中,就遇到了这个问题,于是利用VB中已有的逻辑运算指令,模拟汇编语言的字节移位指令,编制了七个字节移位函数:逻辑左移、逻辑右移、算术右移、循环左移、循环右移、进位循环左移和进位循环右移。
在汇编语言指令中,逻辑左移的功能相当于乘2, 逻辑右移的功能相当于除2,利用这个特性,在VB程序中用乘2和除2方法实现位的左移和右移,然后再用And(与)和Or(或)逻辑运算指令,判断移位过程中是否有进位发生,将进行标志置位。
程序清单
CF是进位标志,采用Boolean类型逻辑变量,如果CF为True表示有进位,为False则表示无进位。
Public CF As Boolean '进位标志
'1.逻辑左移
Public Function SHL(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
BD = OPR
For i = 1 To n - 1
BD = (BD And H7F) * 2 '将D7位屏蔽左移,防止字节溢出
Next i
CF = BD And H80 '判断D7位是否进位
SHL = (BD And H7F) * 2
End Function
'2.逻辑右移
Public Function SHR(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
BD = OPR
For i = 1 To n - 1
BD = BD \ 2 '右移
Next i
CF = BD And 1 '判断D0位是否进位
SHR = BD \ 2
End Function
'3.算术右移
Public Function SAR(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
BD = OPR
Fg1 = BD And H80
For i = 1 To n - 1
BD = BD \ 2 '右移
Next i
CF = BD And 1 '判断D0位是否进位
BD = BD \ 2 '右移
SAR = BD Or Fg1
End Function
'4.循环左移
Public Function ROL(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
BD = OPR
For i = 1 To n
Fg1 = (BD And H80) \ 128 '判断D7位是否进位
BD = ((BD And H7F) * 2) Or Fg1 '带进位左移
Next i
CF = Fg1
ROL = BD
End Function
'5.循环右移
Public Function ROR(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
Dim Fg2 As Byte
BD = OPR
For i = 1 To n
Fg1 = (BD And 1) * 128 '判断D0位是否进位
BD = (BD \ 2) Or Fg1 '带进位右移
Next i
CF = Fg1
ROR = BD
End Function
'6.进位循环左移
Public Function RCL(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
Dim Fg2 As Byte
BD = OPR
Fg2 = CF And 1
For i = 1 To n
Fg1 = (BD And H80) \ 128 '判断D7位是否进位
BD = ((BD And H7F) * 2) Or Fg2 '带进位左移
Fg2 = Fg1
Next i
CF = Fg1
RCL = BD
End Function
'7.进位循环右移
Public Function RCR(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
Dim Fg2 As Byte
BD = OPR
Fg2 = CF And 128
For i = 1 To n
Fg1 = (BD And 1) * 128 '判断D0位是否进位
BD = (BD \ 2) Or Fg2 '带进位右移
Fg2 = Fg1
Next i
CF = Fg1
RCR = BD
End Function
结束语
以上七个字节移位操作函数的功能和用法同宏汇编语言的移位操作指令基本相同,不过只对单字节进行操作,但上述程序稍加改动后,就可对双字节Integer类型和四字节Long类型进行移位操作。