关于VBS的一个怪现象
2024-08-20 14:27:57
今天一个同学让我帮忙写一个程序,要求是:
输入一个n,返回从0到n中任意个数的组合,返回取异或结果为0的组合。来看VBS代码
n =
p = ""
for i = to ^n -
s =
for j = to n-
s = s xor (j+) * ((i and ^j) / ^j)
next
if s= then
D2B(i)
end if
next
'Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFile = objFSO.OpenTextFile("result.txt",2,true)
'objFile.WriteLine result
Function D2B(Dec)
if Dec = then
D2B =
else
Do While Dec >
D2B = Dec Mod & D2B
Dec = Dec \
Loop
end if
msgbox D2B
End Function
恩,很短小的代码,但是怪就怪在下面那个函数上了,上面代码这样
if s=0 then
D2B(i)
end if
时,输出结果没有问题,很正常。
但是当把代码改为:
if s=0 then
result = D2B(i)
end if
结果就不正常了。使用
Wscript.exe /X E:\debug.vbs
调试,结果发现是由于参数i传进函数为byref的,所以在函数内部对i做出了改变,于是就死循环了。但是为什么不对函数结果赋值是就没问题呢?待研究
(vb默认byref,vb.net默认byval)
一个很全的VBS博客了给出了解释。。。
标题: VBS过程和函数参数传递的方式默认是ByVal还是ByRef?
作者: Demon
链接: http://demon.tw/programming/vbs-byval-byref.html
果然有盲点啊
附完整版代码:
n =
result = ""
for i = to ^n -
s =
for j = to n-
s = s xor (j+) * ((i and ^j) / ^j)
next
if s= then
temp = i
result = result & D2B(temp) & chr() & chr()
end if
next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("result.txt",,true)
objFile.WriteLine result
Function D2B(Dec)
if Dec = then
D2B = ""
else
Do While Dec >
D2B = Dec Mod & D2B
Dec = Dec \
Loop
end if
for k = to n-len(D2B)
D2B = "" & D2B
next
End Function
最新文章
- php多图上传问题笔记
- dingding post POST请求
- Java的概述以及语法
- 如何使用Native Messaging API 打开window程序
- android onclick onLongClick ontouch dispatchTouchEvent onInterceptTouchEvent
- Robotium学习笔记一
- jq指定行切换
- 委托-异步调用-泛型委托-匿名方法-Lambda表达式-事件【转】
- iText操作word文档总结
- 用CQRS+ES实现DDD
- 2013-09-16 构建C1000K的服务器(1) – 基础
- 使用echarts-for-react 绘制折线图 报错:`series.type should be specified `
- 一些Java基础方面问题的总结
- auto-encoder小记
- Redis自学笔记:1.简介
- 力扣(LeetCode)1002. 查找常用字符
- 【转载】 火爆的996.ICU项目正在酝酿开源许可证 禁止996公司使用
- MySQL DataType--数值类型
- Django + Uwsgi + Nginx 实现生产环境 项目部署
- Oracle EBS INV 创建物料搬运单
热门文章
- jenkins 离线安装插件 ,插件的下载地址
- 【转】Apache JMeter web性能测试实例
- java代码====实现按钮点击改变颜色=======
- jenkins学习(1)
- 根文件系统的构建与分析(四)之瑞士军刀busybox生成系统基本命令
- 虚拟机在 OpenStack 里没有共享存储条件下的在线迁移
- HttpURLConnection连接网页和获取数据的使用实例
- struct 方法使用
- 【知识碎片】JavaScript篇
- 吴恩达-AI-机器学习课后习题解析-第三周