资源描述
用VB写出螺旋数字打印的代码:
代码实现在窗体上打印如下列文字的效果:
01 02 03 04 05 06 07 08 09 10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
34 63 84 85 86 87 88 71 46 13
33 62 83 96 97 98 89 72 47 14
32 61 82 95 00 99 90 73 48 15
31 60 81 94 93 92 91 74 49 16
30 59 80 79 78 77 76 75 50 17
29 58 57 56 55 54 53 52 51 18
28 27 26 25 24 23 22 21 20 19
Private Function Min(ByVal n1 As Double, ByVal n2 As Double) As Double
Min = IIf(n1 < n2, n1, n2)
End Function
Private Sub Program1()
'根据行、列计算数字, 适合 Print 语句直接输出
Dim m As Integer, n As Integer, s As Integer, Level As Integer
Dim D1 As Integer, D2 As Integer, D3 As Integer, D4 As Integer
Dim i As Integer, j As Integer
m = Val(InputBox("输入行数:", , 10)): n = Val(InputBox("输入列数:", , 10)): s = m + n
For i = 1 To m
For j = 1 To n
Level = Min(i, Min(m - i + 1, Min(j, n - j + 1)))
D1 = 1 + 2 * (2 + s - 2 * Level) * (Level - 1)
D2 = D1 + n - 2 * Level + 1
D3 = D2 + m - 2 * Level + 1
D4 = D3 + n - 2 * Level + 1
If i = Level Then
Print Format$(D1 + j - Level, "000 ");
ElseIf j = Level Then
Print Format$(D4 + m - Level + 1 - i, "000 ");
ElseIf i = m - Level + 1 Then
Print Format$(D3 + n - Level + 1 - j, "000 ");
Else
Print Format$(D2 + i - Level, "000 ");
End If
Next j
Print
Next i
End Sub
Private Sub Program2()
'根据数字计算行、列, 适合填充数组
Dim m As Integer, n As Integer, s As Integer, Level As Integer
Dim D1 As Integer, D2 As Integer, D3 As Integer, D4 As Integer
Dim i As Integer, j As Integer, k As Integer, Num(1 To 100, 1 To 100) As Integer
m = Val(InputBox("输入行数:", , 10)): n = Val(InputBox("输入列数:", , 10)): s = m + n
For k = 1 To m * n
Level = Int((s - Sqr(4 - 4 * k + s * s)) / 4) + 1
D1 = 1 + 2 * (2 + s - 2 * Level) * (Level - 1)
D2 = D1 + n - 2 * Level + 1
D3 = D2 + m - 2 * Level + 1
D4 = D3 + n - 2 * Level + 1
If k - D1 <= D2 - D1 Then
i = Level: j = Level + k - D1
ElseIf k - D1 <= D3 - D1 Then
i = Level + k - D2: j = n - Level + 1
ElseIf k - D1 <= D4 - D1 Then
i = m - Level + 1: j = n - Level + 1 - k + D3
Else
i = m - Level + 1 - k + D4: j = Level
End If
Num(i, j) = k
Next k
For i = 1 To m
For j = 1 To n: Print Format$(Num(i, j), "000 ");: Next j
Print
Next i
End Sub
Option Explicit
Private Sub Form_Click()
Dim X&, Y&, I&, lInt&, Xs&, Ys, C&
Cls
Me.AutoRedraw = True '持久性位图输出
X = Val(InputBox("请输入横排个数!", "请输入数字", 10)) '得到横向个数
Y = Val(InputBox("请输入纵排个数!", "请输入数字", 10)) '得到纵向个数
lInt = X * Y '得到总个数
X = X - 1 '因为从1开始的所以要减1个
Y = Y - 1
For I = 1 To lInt '循环个数
If Xs <= X - C And Ys = C Then '横向打印
Xs = Xs + 1
ElseIf Ys < Y - C And Xs >= X - C Then '纵向打印
Ys = Ys + 1
ElseIf Xs > C + 1 Then '反向横向打印
Xs = Xs - 1
ElseIf Ys > C + 1 Then '反向纵向打印
Ys = Ys - 1
Else
C = C + 1 '每循环完一圈就加一个,成为里面一个圈开始
Xs = Xs + 1 '每循环完一圈第一个开始的坐标加一个
End If
CurrentX = Xs * 400 '设置X、Y坐标
CurrentY = Ys * 400
Print I '打印数字
' Print Format(I, "000") '也可以格式化再打印
Next I
End Sub
Option Explicit
Private Sub Form_Load()
AutoRedraw = True: ForeColor = vbWhite: Width = 6975: Height = 4185: BorderStyle = 3: Caption = "LuoXuan"
Dim i As Long, j As Long, way As Long, mC As Long, mI As Long, k As Long, p As Long, q As Long
i = 9: j = 10: mC = 1: k = 171
Do
mI = mI + 1: k = k - 1: DoEvents
p = i * 400 - 360: q = j * 200 - 160
Line (p, q)-Step(360, 160), vbBlack, BF
CurrentX = p + 40: CurrentY = q: Print CStr(k)
If k = 0 Then Exit Do
Select Case way
Case 0: j = j - 1
Case 1: i = i - 1
Case 2: j = j + 1
Case 3: i = i + 1
End Select
If mI = mC Then mI = 0: mC = mC + 1: way = IIf(way = 3, 0, way + 1)
Loop
End Sub
Option Explicit
Private Sub Form_Click()
Const PI As Single = 3.14159265
Dim R() As Long, M&, N&
Dim x&, y&, i&, a#
'初始化
M = Val(InputBox("请输入横排个数!", "请输入数字", 10)) '得到横向个数
N = Val(InputBox("请输入纵排个数!", "请输入数字", 10)) '得到纵向个数
ReDim R(M + 1, N + 1) As Long
For x = 0 To M + 1
R(x, 0) = -1
R(x, N + 1) = -1
Next
For y = 0 To N + 1
R(0, y) = -1
R(M + 1, y) = -1
Next
'小虫初始状态
x = 1: y = 1: a = 0: R(x, y) = 1
'小虫爬行
For i = 2 To M * N
If R(x + Round(Sin(a)), y + Round(Cos(a))) = 0 Then
x = x + Round(Sin(a))
y = y + Round(Cos(a))
R(x, y) = i
Else
a = a + PI / 2
i = i - 1
End If
Next
'输出结果
For x = 1 To M
For y = 1 To N
Print Format(R(x, y), "@@@@@");
Next
Print
Next
End Sub
Private Sub Program1()
'根据行、列计算数字, 适合 Print 语句直接输出
Dim m As Integer, n As Integer, s As Integer, Level As Integer
Dim D1 As Integer, D2 As Integer, D3 As Integer, D4 As Integer
Dim i As Integer, j As Integer
m = Val(InputBox("输入行数:", , 10)): n = Val(InputBox("输入列数:", , 10)): s = m + n
For i = 1 To m
For j = 1 To n
Level = Min(i, Min(m - i + 1, Min(j, n - j + 1)))
D1 = 1 + 2 * (2 + s - 2 * Level) * (Level - 1)
D2 = D1 + n - 2 * Level + 1
D3 = D2 + m - 2 * Level + 1
D4 = D3 + n - 2 * Level + 1
If i = Level Then
Print Format$(D1 + j - Level, "000 ");
ElseIf j = Level Then
Print Format$(D4 + m - Level + 1 - i, "000 ");
ElseIf i = m - Level + 1 Then
Print Format$(D3 + n - Level + 1 - j, "000 ");
Else
Print Format$(D2 + i - Level, "000 ");
End If
Next j
Print
Next i
End Sub
展开阅读全文