1. 程式人生 > >vbs與其他語言進行交互編程(外存傳參)

vbs與其他語言進行交互編程(外存傳參)

one 自定義排序 RR 調用方法 i++ 命令 argc urn 例如

vbs沒有自定義排序函數。無需自己造輪子,可以用其他語言來完成這個任務(在傳遞數據比較簡單的情況下,例如只傳遞數組)。

首先用5分鐘寫一個C++排序的代碼。命名為“mysort.cpp”:

#include<bits/stdc++.h>

using namespace std;

int main(int argc,char * argv[]){
    freopen("val.txt","w",stdout) ;    //重定向輸出 
    vector<int> v;
    int t;
    for(int i=1;i<argc;i++){    //從命令行參數中獲取變量 
sscanf(argv[i],"%d",&t); v.push_back(t); } sort(v.begin(),v.end()); for(int i=0;i<v.size();i++){ printf("%d ",v[i]); } return 0; }

編譯並測試之後,開始編寫vbs代碼。

這裏借用昨天編寫的vbs的vector類:

技術分享圖片
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 exit Function 註意退出函數的表達式‘ 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
vector類

註意到外存傳參這個操作可以抽象拿出來作為一個函數。編寫extendProcess函數

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

(在這裏,程序名可以是cpp編譯出來的exe,也可以是java字節碼,調用方法:java 主類,也可以是python代碼,調用方法: python scrip.py,甚至可以是MATLAB腳本,這裏不贅述)

然後我們來愉快的測試吧,看看效果:

測試代碼:

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

測試效果:

技術分享圖片

完整代碼:

技術分享圖片
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 exit Function 註意退出函數的表達式‘
        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")
完整vbs代碼

vbs與其他語言進行交互編程(外存傳參)