0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

vbsの作法 その102

Last updated at Posted at 2025-10-12

概要

vbsの作法、調べてみた。
練習問題やってみた。
リベンジ。copilotに、聞いた。

練習問題

俺言語インタープリタを書け。
fizzbuzzを解け。

サンプルコード


Function mo(s, v)
    	mo = s mod v
End Function

Function iif(s, v)
    	Dim rtn
    	If s Then
    	    	rtn = Cint(v)
    	Else
        	rtn = 0
    	End If
    	iif = rtn
End Function

function jmp(c)
	Dim d
	d = eval(c)
	if d > 0 then
		pc = d
	else
		pc = pc + 10
	end if
end function

function V_a(c)
	a = eval(c)
	pc = pc + 10
end function

function V_b(c)
	b = eval(c)
	pc = pc + 10
end function

function print(c)
	if c = "a" then
		WScript.StdOut.WriteLine a 
	else
		WScript.StdOut.WriteLine c
	end if
	pc = pc + 10
end function

function gyou(n)
	Dim line
	For i = 0 to UBound(lines) - 1
		line = SPLIT(lines(i), " ")
		if CInt(line(0)) = n Then
			gyou = lines(i)
		end if
	Next
end function


Sub run_loop()
	Do While pc < 300
		Dim line, s, m, c
		line = gyou(pc)
		s = SPLIT(line, " ")
		m = Left(s(1), 1)
		c = Mid(s(1), 3, 35)
		If m = "#" Then
			jmp(c)
		ElseIf m = "a" Then
			V_a(c)
		ElseIf m = "b" Then
			V_b(c)
		ElseIf m = "?" Then
			print(c)
		Else
			MsgBox c
			pc = 400
		End If
	Loop
End Sub




Dim pc
Dim a
Dim b
pc = 0
a = 0
b = 0
Dim ore
ore = ""
ore = ore & "10 a=0" & vbCrLf
ore = ore & "20 a=a+1" & vbCrLf
ore = ore & "30 #=iif(a>100,400)" & vbCrLf
ore = ore & "40 b=mo(a,15)" & vbCrLf
ore = ore & "50 #=iif(b=0,120)" & vbCrLf
ore = ore & "60 b=mo(a,5)" & vbCrLf
ore = ore & "70 #=iif(b=0,140)" & vbCrLf
ore = ore & "80 b=mo(a,3)" & vbCrLf
ore = ore & "90 #=iif(b=0,160)" & vbCrLf
ore = ore & "100 ?=a" & vbCrLf
ore = ore & "110 #=20" & vbCrLf
ore = ore & "120 ?=fizzbuzz" & vbCrLf
ore = ore & "130 #=20" & vbCrLf
ore = ore & "140 ?=buzz" & vbCrLf
ore = ore & "150 #=20" & vbCrLf
ore = ore & "160 ?=fizz" & vbCrLf
ore = ore & "170 #=20" & vbCrLf
Dim SEP
SEP = vbCrLf
Dim lines
lines = SPLIT(ore, SEP)
pc = 10
run_loop()

実行結果

>cscript ore2.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
fizz
22
23
fizz
buzz
26
fizz
28
29
fizzbuzz
31
32
fizz
34
buzz
fizz
37
38
fizz
buzz
41
fizz
43
44
fizzbuzz
46
47
fizz
49
buzz
fizz
52
53
fizz
buzz
56
fizz
58
59
fizzbuzz
61
62
fizz
64
buzz
fizz
67
68
fizz
buzz
71
fizz
73
74
fizzbuzz
76
77
fizz
79
buzz
fizz
82
83
fizz
buzz
86
fizz
88
89
fizzbuzz
91
92
fizz
94
buzz
fizz
97
98
fizz
buzz




以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?