基本情報技術者試験H28春期午後問8(疑似言語)VBAプログラム

More than 1 year has passed since last update.

Option Explicit

Public Data(24) As String
Public MemoCnt As Integer, MemoMax As Integer, Memo(4) As Integer
Public DataLen As Integer, DataMax As Integer
Public textLen As Integer, text As String

Sub main()
Dim i As Integer
MemoMax = 5
DataMax = 25
Call resetMemo
Call addMemo(4, "Aoki")
Call output
Call addMemo(4, "Imai")
Call output
Call addMemo(3, "Uno")
Call output
Call addMemo(4, "Endo")
Call output
Call deleteMemo(0)
Call output
Call changeMemo(2, 3, "Abe")
Call output
Call moveMemo(2, 0)
Call output
End Sub

Sub resetMemo()
Dim i As Integer
MemoCnt = 0
DataLen = 0
For i = 0 To 4
Memo(i) = 0
Next
For i = 0 To 24
Data(i) = ""
Next
End Sub

Sub addMemo(textLen, text)
Dim i As Integer
Memo(MemoCnt) = DataLen
MemoCnt = MemoCnt + 1
Data(DataLen) = textLen
DataLen = DataLen + 1
For i = 0 To textLen - 1
Data(DataLen + i) = Mid(text, i + 1, 1)
Next
DataLen = DataLen + textLen
End Sub

Sub deleteMemo(pos)
Dim i As Integer
i = pos + 1
Do While i < MemoCnt
Memo(i - 1) = Memo(i)
i = i + 1
Loop
MemoCnt = MemoCnt - 1
End Sub

Sub changeMemo(pos, textLen, text)
Dim i As Integer
Memo(pos) = DataLen
Data(DataLen) = textLen
DataLen = DataLen + 1
For i = 0 To textLen - 1
Data(DataLen + i) = Mid(text, i + 1, 1)
Next
DataLen = DataLen + textLen
End Sub

Sub moveMemo(fromPos, toPos)
Dim i As Integer, m As Integer
m = Memo(fromPos)
If fromPos < toPos Then
For i = fromPos To toPos - 1
Memo(i) = Memo(i + 1)
Next
End If
If fromPos > toPos Then
For i = fromPos To toPos + 1 Step -1
Memo(i) = Memo(i - 1)
Next
End If
Memo(toPos) = m
End Sub

Sub output()
Dim i As Integer
For i = 0 To 4
Debug.Print "Memo("; i; ")="; Memo(i)
Next
For i = 0 To DataLen - 1
Debug.Print "Data("; i; ")="; Data(i)
Next
Debug.Print "------------------------------"
End Sub

実行結果

Memo( 0 )= 0 

Memo( 1 )= 0
Memo( 2 )= 0
Memo( 3 )= 0
Memo( 4 )= 0
Data( 0 )=4
Data( 1 )=A
Data( 2 )=o
Data( 3 )=k
Data( 4 )=i
------------------------------
Memo( 0 )= 0
Memo( 1 )= 5
Memo( 2 )= 0
Memo( 3 )= 0
Memo( 4 )= 0
Data( 0 )=4
Data( 1 )=A
Data( 2 )=o
Data( 3 )=k
Data( 4 )=i
Data( 5 )=4
Data( 6 )=I
Data( 7 )=m
Data( 8 )=a
Data( 9 )=i
------------------------------
Memo( 0 )= 0
Memo( 1 )= 5
Memo( 2 )= 10
Memo( 3 )= 0
Memo( 4 )= 0
Data( 0 )=4
Data( 1 )=A
Data( 2 )=o
Data( 3 )=k
Data( 4 )=i
Data( 5 )=4
Data( 6 )=I
Data( 7 )=m
Data( 8 )=a
Data( 9 )=i
Data( 10 )=3
Data( 11 )=U
Data( 12 )=n
Data( 13 )=o
------------------------------
Memo( 0 )= 0
Memo( 1 )= 5
Memo( 2 )= 10
Memo( 3 )= 14
Memo( 4 )= 0
Data( 0 )=4
Data( 1 )=A
Data( 2 )=o
Data( 3 )=k
Data( 4 )=i
Data( 5 )=4
Data( 6 )=I
Data( 7 )=m
Data( 8 )=a
Data( 9 )=i
Data( 10 )=3
Data( 11 )=U
Data( 12 )=n
Data( 13 )=o
Data( 14 )=4
Data( 15 )=E
Data( 16 )=n
Data( 17 )=d
Data( 18 )=o
------------------------------
Memo( 0 )= 5
Memo( 1 )= 10
Memo( 2 )= 14
Memo( 3 )= 14
Memo( 4 )= 0
Data( 0 )=4
Data( 1 )=A
Data( 2 )=o
Data( 3 )=k
Data( 4 )=i
Data( 5 )=4
Data( 6 )=I
Data( 7 )=m
Data( 8 )=a
Data( 9 )=i
Data( 10 )=3
Data( 11 )=U
Data( 12 )=n
Data( 13 )=o
Data( 14 )=4
Data( 15 )=E
Data( 16 )=n
Data( 17 )=d
Data( 18 )=o
------------------------------
Memo( 0 )= 5
Memo( 1 )= 10
Memo( 2 )= 19
Memo( 3 )= 14
Memo( 4 )= 0
Data( 0 )=4
Data( 1 )=A
Data( 2 )=o
Data( 3 )=k
Data( 4 )=i
Data( 5 )=4
Data( 6 )=I
Data( 7 )=m
Data( 8 )=a
Data( 9 )=i
Data( 10 )=3
Data( 11 )=U
Data( 12 )=n
Data( 13 )=o
Data( 14 )=4
Data( 15 )=E
Data( 16 )=n
Data( 17 )=d
Data( 18 )=o
Data( 19 )=3
Data( 20 )=A
Data( 21 )=b
Data( 22 )=e
------------------------------
Memo( 0 )= 19
Memo( 1 )= 5
Memo( 2 )= 10
Memo( 3 )= 14
Memo( 4 )= 0
Data( 0 )=4
Data( 1 )=A
Data( 2 )=o
Data( 3 )=k
Data( 4 )=i
Data( 5 )=4
Data( 6 )=I
Data( 7 )=m
Data( 8 )=a
Data( 9 )=i
Data( 10 )=3
Data( 11 )=U
Data( 12 )=n
Data( 13 )=o
Data( 14 )=4
Data( 15 )=E
Data( 16 )=n
Data( 17 )=d
Data( 18 )=o
Data( 19 )=3
Data( 20 )=A
Data( 21 )=b
Data( 22 )=e
------------------------------