• vbs与其他语言进行交互编程(外存传参)


    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代码
  • 相关阅读:
    [转]中英文混合字符截取方法样式表解决方法
    Asp.Net小技巧集合
    [分享]整理后的Discuz!NT 2.0项目源码
    [原创]Web2.0之Tag标签原理实现浅析
    [转]CSS完美兼容IE6/IE7/FF的通用hack方法
    [转]常用CSS缩写语法总结
    文本类文件编码转换器及DiscuzNTdotNet2.0 - -
    [驳]ASP伪静态页简单教程
    SELECT中很多程序都不用的NOLOCK“加锁选项”的功能说明
    iOS 微博 OAuth2.0 分享 文字 + 图片 微博的方法
  • 原文地址:https://www.cnblogs.com/TQCAI/p/8877098.html
Copyright © 2020-2023  润新知