JavaScript调用VBS中的InputBox和MsgBox函数

2010年10月30日   By Demon   23,182 views

严格的是应该是JScript,不过很多不明真相的同学不区分JavaScript和JScript,于是标题就写JavaScript了。JScript和VBScript相互调用搞来搞去都是ScriptControl,一点技术含量也没有。

JavaScript版MsgBox函数,后面两个参数忽略掉,用了那么久VBS我从来没有用过MsgBox最后两个参数。

阅读这个条目剩下部分 »

VBS中Property Set和Property Let的区别

2010年10月28日   By Demon   25,561 views

说好不玩VBS来着,但是今天有人问我,简单的写一下吧。

对于这个问题,《VBScript Programmers Reference》第215页说的很清楚:

Functionally, Property Let and Property Set procedures do the same thing. However, the Property Set procedure has two differences:

  • It makes it clearer that the property is an object-based property (any technique that makes the intent of the code more explicit is preferable over any other equally correct technique).
  • Code outside of your class must use the Set Object.Property = Object syntax in order to write to the property (also a good thing, because this is the typical way of doing things).

从功能上说,这两者的作用是一样的。但是Property Set有两点不同:第一,它说明了这个属性是一个与对象有关的属性;第二,在类的外面给属性赋值的时候必须使用Set关键字。

文件说明太抽象了,举个例子:

阅读这个条目剩下部分 »

用VBS精确计算2的100次方

2010年10月27日   By Demon   18,034 views

即Grade school multiplication(小学乘法?)算法的VBS实现。既然Python可以计算2的100次方,那么我就要用VBS实现。不过这个效率嘛,计算2的10000次方Python用了0.009013秒,VBS用了120.9805秒,不是一个等级的,我就不多说什么了。

直接上代码: 阅读这个条目剩下部分 »

Python中的长整型(Long)乘法C源码分析

2010年10月26日   By Demon   19,028 views

最近学Python看的书是《Learning Python》第三版,在Chapter 2里有一个示例

print 'hello world'
print 2 ** 100

然后说了句“我将在这本书的后面解释print语句,以及为什么在Python中计算2的100次方不会溢出”。

I’ll explain the print statement, and why you can raise 2 to the power 100 in Python without overflowing, in later parts of this book.

Python中的长整型(long)和C语言的long有很大的区别(C语言的long对应Python里的plain integer),Python中的long可以实现无限精度(unlimited precision),很好奇这个在C代码中是怎么实现的,于是看了一下Python的C源码。

虽然求幂运算也有对应的算法,但是最终还是依赖于乘法来实现,所以在这里只研究一下Python的长整型乘法。长整型乘法在Python源码中的Objects目录的longobject.c中定义。 阅读这个条目剩下部分 »

会Python的人,你惹不起

2010年10月25日   By Demon   24,808 views

从今天开始学习Python。

首先当然是Hello world。

print "Hello world"

输出挺简单,然后试试输入。

name = raw_input("Enter your name:")
print "Hello " + name

也挺简单,再试试条件和循环,枚举100以内的质数。

for i in range(2, 101) :
    for j in range(2, i):
        if i % j == 0 : 
            break
    else:
        print i

感觉for的用法有点奇怪,最后再试试正则表达式,依然是质数。

import re
regex = re.compile(r"^1?$|^(11+?)\1+$")
for i in range(1, 100) :
    if not regex.match("1" * i) :
        print i

哥也是会Python的了,会Python的人,你惹不起。