vbs实现c++的vector

2018/04/18 14:17
阅读数 17

代码(待更新):

class Vector
    Private length
    Private data()
    Sub Class_Initialize()
        length=0
    End Sub
    '插入元素'
    public Function Add (byval x)
        length=length+1
        redim preserve data(length-1)
        data(length-1)=x
    End Function
    '快速展示'
    public Function display()
        dim i
        dim s
        for i=0 to length-1
            s=s & cstr(data(i)) & " "
        next
        msgbox s
    End Function
    '返回大小'
    public Function size()
        size= length
    End Function
    '获取元素'
    public Function getAt (byval pos)
        if pos<0 or pos>length-1 then 
            getAt=0
            exit Function
        end if
        getAt=data(pos)
    End Function
    '删除元素'
    public Function delAt (byval pos)
        if pos<0 or pos>length-1 then exit Function '注意退出函数的表达式'
        dim i
        for i=pos to length-2
            data(i)=data(i+1)
        next
        length=length-1
    End Function
    '插入元素'
    public Function insert (byval pos,byval elem)
        if pos<0 or pos>length-1 then exit Function 
        dim i
        length=length+1
        redim preserve data(length-1)
        for i=length-1 to pos+1 step -1
            data(i)=data(i-1)
        next
        data(pos)=elem
    End Function
end class

调用:

set v = new Vector
v.Add 1
v.Add 2
v.Add 3
v.display
v.delAt 0
v.insert 1,555
v.display

 完整代码:

 

class Vector
	Private length
	public data()
	Sub Class_Initialize()
		length=0
	End Sub
	'插入元素'
	public Function Add (byval x)
		length=length+1
		redim preserve data(length-1)
		data(length-1)=x
	End Function
	'快速展示'
	public Function display()
		dim i
		dim s
		for i=0 to length-1
			s=s & cstr(data(i)) & " "
		next
		msgbox s
	End Function
	'返回大小'
	public Function size()
		size= length
	End Function
	'获取元素'
	public Function getAt (byval pos)
		if pos<0 or pos>length-1 then 
			getAt=0
			exit Function
		end if
		getAt=data(pos)
	End Function
	'删除元素'
	public Function delAt (byval pos)
		if pos<0 or pos>length-1 then  '注意退出函数的表达式'
		dim i
		for i=pos to length-2
			data(i)=data(i+1)
		next
		length=length-1
		redim preserve data(length-1)
	End Function
	'插入元素'
	public Function insert (byval pos,byval elem)
		if pos<0 or pos>length-1 then exit Function 
		dim i
		length=length+1
		redim preserve data(length-1)
		for i=length-1 to pos+1 step -1
			data(i)=data(i-1)
		next
		data(pos)=elem
	End Function
end class


public Function extendProcess (byval exe,byval arr) '程序名、数组'
	dim i
	dim cmd
	cmd=exe
	For i=0 To ubound(arr)
		cmd=cmd & " " & arr(i)
	Next
	dim objShell	'执行命令'
	Set objShell = CreateObject("Wscript.Shell")
	objShell.Run(cmd)
	'读入输出文件val.txt'
	Set fs = CreateObject("Scripting.FileSystemObject")
    Set file = fs.OpenTextFile("val.txt", 1, false)
    info=file.readall
    file.close
    set fs=nothing 
    '处理信息'
    extendProcess=split(info)	'用空格分隔'
End Function

set v = new Vector
v.Add 1
v.Add 3
v.Add 5
v.Add 4
v.Add 6
v.Add 4
v.Add 7
v.Add 8
ans=extendProcess("mysort",v.data)	'如果函数有返回,就必须要有括号。如果无返回,并且参数数目大于1,不能带括号'
set v2 = new Vector
for each x in ans
	v2.Add x
next
v2.display

' dim objShell
' Set objShell = CreateObject("Wscript.Shell")
' objShell.Run("%comspec% /k ipconfig /all")
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部