设个VB程序,以不同方式(直角三角形、金字塔)显示杨辉三角
- 提问者网友:长安小才冯
- 2021-04-27 18:33
- 二级知识专家网友:为你轻狂半世殇
- 2021-04-27 19:15
'我原来给人家写的试试
'加入1个text、2个command控件
Option Explicit
Dim arr() As Long, n As Integer
Private Sub Command1_Click() '直角
Dim i As Integer, j As Integer, s As String
Cls
For i = 1 To Val(Text1)
For j = 1 To i
s = s & arr(i, j) & Space(6 - Len(CStr(arr(i, j))))
Next
Print s
s = ""
Next
End Sub
Private Sub Command2_Click() '等腰
Dim i As Integer, j As Integer, s As String
Cls
For i = 1 To Val(Text1)
For j = 1 To i
s = s & arr(i, j) & Space(6 - Len(CStr(arr(i, j))))
Next
s = IIf(i Mod 2 = 1, Space(6 * Int((Val(Text1) - i + 1) / 2)) & s, Space(6 * Int((Val(Text1) - i) / 2) + 3) & s)
Print s
s = ""
Next
End Sub
Function Fc(n As Long) '装入数组
Dim i As Integer, j As Integer
If n = 0 Then Exit Function
ReDim arr(1 To n, 1 To n) '重定义数组
For i = 1 To n
For j = 1 To i
If j = 1 Then
arr(i, j) = 1
Else
arr(i, j) = arr(i - 1, j - 1) + arr(i - 1, j)
End If
Next
Next
End Function
Private Sub Form_Activate()
Text1 = 20
Command1.Caption = "直角": Command2.Caption = "等腰"
End Sub
Private Sub Text1_Change()
Cls
If IsNumeric(Text1) And Val(Text1) > 0 And Val(Text1) <= 20 Then '最大20
Fc Val(Text1)
Command1.Enabled = True: Command2.Enabled = True
Else
Text1 = ""
Command1.Enabled = False: Command2.Enabled = False
End If
End Sub
- 1楼网友:冷眼_看世界
- 2021-04-27 20:47
Private Sub Command2_Click()
Dim i, n As Integer
Dim c As String
Do
n = Int(Val(InputBox("请输入奇数 n=", 0)))
If n > 0 Then Exit Do
Loop
If n Mod 2 = 0 Then n = n + 1
c = ""
For i = 1 To n
c = c & "*"
Next
For i = n To 1 Step -2
Print Space((n - i) / 2) & Left(c, i)
Next
End Sub