第8講 Functionプロシージャ

第5話 成績一覧表作成ソフトのプロシージャによる改良その3
前話問題解答例
Public Class Form1
   Dim a(3, 39, 6) As Integer
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     '見出し表示やデータ作成などの基本処理
     g()

     '各学期処理
     s()

     '年間処理
     t()

   End Sub

   '基本処理
   Sub g()

     '187行追加
     f1()

     '見出しを入れる
     f2()

     '出席番号とランダムデータの発生
     f3()

     '出席番号とランダムデータをDataGridViewに表示させる
     f4()

   End Sub

   '各学期処理
   Sub s()

     '各学期合計値・平均値の算出・表示
     f5()

     '各学期各生徒の最高点・最低点の算出・表示
     f6()

     '各学期合否・講評の算出・表示
     f7()

     '各学期各教科の合計・平均の算出・表示
     f8()

     '各学期各教科の最高点・算出の算出・表示
     f9()

     '各学期の並び替え
     f10()

     '各学期順位付け
     f11()

     '各学期出席番号順並び替え
     f12()

   End Sub

   '年間処理
   Sub t()

     'データ年間合計処理
     t1()

     '各生徒の最高点・最低点の算出と表示
     t2()

     '成績順並び替え
     t3(0)

     '順位付け
     t4()

     '出席番号並び替え
     t3(1)

     '合否判定と講評
     t5()

     '各教科の合計と平均の算出と表示
     t6()

     '各教科または合計または平均の最高点・最低点の算出と表示
     t7()

   End Sub

   'データ年間合計処理
   Sub t1()
     For i = 1 To 40
       t10(i) '各生徒処理
     Next
   End Sub
   '各生徒処理
   Sub t10(ByVal p As Integer)
     For i = 1 To 6
       DataGridView1(i, 142 + p).Value = t11(p, i) '項目別処理
     Next
     DataGridView1(7, 142 + p).Value = t11(p, 6) / 5
   End Sub
   '項目別処理
   Function t11(ByVal p As Integer, ByVal q As Integer)
     Dim w As Integer
     w = 0
     For i = 0 To 2
       w += a(i, p - 1, q)
     Next
     t11 = w
   End Function

   '各生徒の最高点・最低点の算出と表示
   Sub t2()
     For i = 1 To 40
       DataGridView1(8, 142 + i).Value = t20(i) '最高点の算出
       DataGridView1(9, 142 + i).Value = t21(i) '最低点の算出
     Next
   End Sub
   '最高点の算出
   Function t20(ByVal p As Integer) As Integer
     Dim max As Integer
     max = 0
     For i = 1 To 5
       If DataGridView1(i, 142 + p).Value > max Then max = DataGridView1(i, 142 + p).Value
     Next
     t20 = max
   End Function
   '最低点の算出
   Function t21(ByVal p As Integer) As Integer
     Dim min As Integer
     min = 100
     For i = 1 To 5
       If DataGridView1(i, 142 + p).Value < min Then min = DataGridView1(i, 142 + p).Value
     Next
     t21 = min
   End Function

   '成績順並び替え
   Sub t3(ByVal h As Integer)
     For i = 40 To 2 Step -1
       t30(h, i) '対象範囲内の最高点(or対象範囲内の出席番号最大値)の算出と対象範囲内最下行との交換
     Next
   End Sub
   '対象範囲内の最高点の算出と対象範囲内最下行との交換
   Sub t30(ByVal h As Integer, ByVal p As Integer)
     Dim w As Integer
     For i = 1 To p
       w = t31(h, i) '対象内最低点者の出席番号(or最大の出席番号)の算出
     Next
     t32(p, w) '対象内最低点生徒(or対象範囲内の出席番号最大生徒)のデータを対象内最下行と交換する。
   End Sub
   '対対象内最低点者の出席番号の算出
   Function t31(ByVal h As Integer, ByVal p As Integer) As Integer
     Dim max, min, bn As Integer
     If h = 0 Then
       min = 1000
       For i = 1 To p
         If DataGridView1(6, 142 + i).Value < min Then
           min = DataGridView1(6, 142 + i).Value
           bn = i
         End If
       Next
     End If
     If h = 1 Then
       max = 0
       For i = 1 To p
         If DataGridView1(0, 142 + i).Value > max Then
           max = DataGridView1(0, 142 + i).Value
           bn = i
         End If
       Next
     End If
     t31 = bn
   End Function
   '対象内最低点生徒のデータを対象内最下行と交換する。
   Sub t32(ByVal p As Integer, ByVal bn As Integer)
     Dim w As String
     For i = 0 To 12
       w = DataGridView1(i, 142 + bn).Value
       DataGridView1(i, 142 + bn).Value = DataGridView1(i, 142 + p).Value
       DataGridView1(i, 142 + p).Value = w
     Next
   End Sub

   '順位付け
   Sub t4()
     For i = 1 To 40
       DataGridView1(10, 142 + i).Value = i
     Next
     For i = 2 To 40
       If DataGridView1(6, 141 + i).Value = DataGridView1(6, 142 + i).Value Then DataGridView1(10, 142 + i).Value = DataGridView1(10, 141 + i).Value
     Next
   End Sub

   '合否判定と講評
   Sub t5()
     '合否判定
     For i = 1 To 40
       If DataGridView1(6, 142 + i).Value >= 700 Then DataGridView1(11, 142 + i).Value = "合格"
       If DataGridView1(6, 142 + i).Value < 700 Then DataGridView1(11, 142 + i).Value = "不合格"
     Next
     '講評
     For i = 1 To 40
       If DataGridView1(6, 142 + i).Value >= 800 Then DataGridView1(12, 142 + i).Value = "大変優秀です。"
       If DataGridView1(6, 142 + i).Value < 800 And DataGridView1(6, 142 + i).Value >= 700 Then DataGridView1(12, 142 + i).Value = "優秀です。"
       If DataGridView1(6, 142 + i).Value < 700 And DataGridView1(6, 142 + i).Value >= 600 Then DataGridView1(12, 142 + i).Value = "ぎりぎり合格です。"
       If DataGridView1(6, 142 + i).Value < 600 And DataGridView1(6, 142 + i).Value >= 500 Then DataGridView1(12, 142 + i).Value = "合格まであと少しです。"
       If DataGridView1(6, 142 + i).Value < 500 Then DataGridView1(12, 142 + i).Value = "かなりのがんばりが必要。"
     Next
   End Sub

   '各教科の合計と平均の算出と表示
   Sub t6()
     For i = 1 To 7
       DataGridView1(i, 183).Value = t60(i) '合計算出
       DataGridView1(i, 184).Value = t60(i) / 40 '平均算出
     Next
   End Sub
   '合計算出
   Function t60(ByVal p As Integer) As Integer
     t60 = 0 'このように使うと変数が1つ節約できる。
     For i = 1 To 40
       t60 += DataGridView1(p, 142 + i).Value 't60 += DataGridView1(p, 142 + i).Valueはt60 =t60 + DataGridView1(p, 142 + i).Valueの簡略表現
     Next
   End Function

   '各教科または合計または平均の最高点・最低点の算出と表示
   Sub t7()
     For i = 1 To 7
       DataGridView1(i, 185).Value = t70(i) '最高点の算出
       DataGridView1(i, 186).Value = t71(i) '最低点の算出
     Next
   End Sub
   '最高点の算出
   Function t70(ByVal p As Integer) As Integer
     t70 = 0 'このように使うと変数が1つ節約できる。これはmaxに相当する。
     For i = 1 To 40
       If DataGridView1(p, 142 + i).Value > t70 Then t70 = DataGridView1(p, 142 + i).Value
     Next
   End Function
   '最高点の算出
   Function t71(ByVal p As Integer) As Integer
     t71 = 1000 'このように使うと変数が1つ節約できる。これはminに相当する。
     For i = 1 To 40
       If DataGridView1(p, 142 + i).Value < t71 Then t71 = DataGridView1(p, 142 + i).Value
     Next
   End Function

   '187行追加
   Sub f1()
     Dim i As Integer
     For i = 1 To 187
       DataGridView1.Rows.Add()
     Next
   End Sub

   '見出しを入れる
   Sub f2()
     Dim i As Integer
     For i = 0 To 3
       '見出しを入れる
       If i = 0 Then DataGridView1(0, 47 * i).Value = "1学期"
       If i = 1 Then DataGridView1(0, 47 * i).Value = "2学期"
       If i = 2 Then DataGridView1(0, 47 * i).Value = "3学期"
       If i = 3 Then DataGridView1(0, 47 * i).Value = "1年間"

       '列見出しを入れる。
       DataGridView1(0, 1 + 47 * i).Value = "番号 "
       DataGridView1(1, 1 + 47 * i).Value = "国語 "
       DataGridView1(2, 1 + 47 * i).Value = "社会 "
       DataGridView1(3, 1 + 47 * i).Value = "数学 "
       DataGridView1(4, 1 + 47 * i).Value = "理科 "
       DataGridView1(5, 1 + 47 * i).Value = "英語 "
       DataGridView1(6, 1 + 47 * i).Value = "合計 "
       DataGridView1(7, 1 + 47 * i).Value = "平均 "
       DataGridView1(8, 1 + 47 * i).Value = "最高点"
       DataGridView1(9, 1 + 47 * i).Value = "最低点"
       DataGridView1(10, 1 + 47 * i).Value = "順位 "
       DataGridView1(11, 1 + 47 * i).Value = "合否 "
       DataGridView1(12, 1 + 47 * i).Value = "講評 "

       '行見出し
       DataGridView1(0, 42 + 47 * i).Value = "合計 "
       DataGridView1(0, 43 + 47 * i).Value = "平均 "
       DataGridView1(0, 44 + 47 * i).Value = "最高点"
       DataGridView1(0, 45 + 47 * i).Value = "最低点"
     Next
   End Sub

   '出席番号とランダムデータの発生
   Sub f3()
     Dim k As Integer
     For i = 0 To 3
       For j = 1 To 40
         a(i, j - 1, 0) = j
       Next
     Next
     For i = 0 To 2
       For j = 1 To 40
         For k = 1 To 5
           a(i, j - 1, k) = Int(101 * Rnd())
         Next
       Next
     Next
   End Sub

   '出席番号とランダムデータをDataGridViewに表示させる
   Sub f4()
     Dim i As Integer, j As Integer
     For i = 0 To 3
       For j = 1 To 40
         DataGridView1(0, i * 47 + j + 1).Value = a(i, j - 1, 0)
       Next
     Next
     For i = 0 To 2
       For j = 1 To 40
         For k = 1 To 5
           DataGridView1(k, i * 47 + j + 1).Value = a(i, j - 1, k)
         Next
       Next
     Next
   End Sub

   '各学期合計値・平均値の算出・表示
   Sub f5()
     Dim i As Integer, j As Integer
     For i = 0 To 2
       For j = 1 To 40
         a(i, j - 1, 6) = 0
         For k = 1 To 5
           a(i, j - 1, 6) = a(i, j - 1, 6) + a(i, j - 1, k)
         Next
         DataGridView1(6, i * 47 + j + 1).Value = a(i, j - 1, 6)
         DataGridView1(7, i * 47 + j + 1).Value = a(i, j - 1, 6) / 5
       Next
     Next
   End Sub

   '各学期各生徒の最高点・最低点の算出・表示
   Sub f6()
     Dim i As Integer, j As Integer
     For i = 0 To 2
       For j = 1 To 40
         DataGridView1(8, i * 47 + j + 1).Value = g61(i, j)
         DataGridView1(9, i * 47 + j + 1).Value = g62(i, j)
       Next
     Next
   End Sub
Function g61(ByVal p As Integer, ByVal q As Integer) As Integer
     Dim max As Integer
     max = 0
     For i = 1 To 5
       If a(p, q - 1, i) > max Then max = a(p, q - 1, i)
     Next
     Return max
   End Function
   Function g62(ByVal p As Integer, ByVal q As Integer) As Integer
     Dim min As Integer
     min = 100
     For i = 1 To 5
       If a(p, q - 1, i) < min Then min = a(p, q - 1, i)
     Next
     Return min
   End Function

  
   '各学期合否・講評の算出・表示
   Sub f7()
     Dim i As Integer, j As Integer
     For i = 0 To 2
       For j = 1 To 40
         If a(i, j - 1, 6) >= 250 Then DataGridView1(11, i * 47 + j + 1).Value = "合格"
         If a(i, j - 1, 6) < 250 Then DataGridView1(11, i * 47 + j + 1).Value = "不合格"
       Next
       For j = 1 To 40
         If a(i, j - 1, 6) >= 350 Then DataGridView1(12, i * 47 + j + 1).Value = "上位合格です。"
         If a(i, j - 1, 6) < 350 And a(i, j - 1, 6) >= 280 Then DataGridView1(12, i * 47 + j + 1).Value = "余裕で合格です。"
         If a(i, j - 1, 6) < 280 And a(i, j - 1, 6) >= 250 Then DataGridView1(12, i * 47 + j + 1).Value = "ぎりぎり合格です。"
         If a(i, j - 1, 6) < 250 And a(i, j - 1, 6) >= 230 Then DataGridView1(12, i * 47 + j + 1).Value = "合格まで後一歩です。"
         If a(i, j - 1, 6) < 230 Then DataGridView1(12, i * 47 + j + 1).Value = "よく勉強して再挑戦!"
       Next
     Next
   End Sub

   '各学期各教科の合計・平均の算出・表示
   Sub f8()
     Dim i As Integer, j As Integer
     For i = 0 To 2
       For j = 1 To 6
         DataGridView1(j, i * 47 + 42).Value =g8(i, j)
         DataGridView1(j, i * 47 + 43).Value =g8(i, j) / 40
       Next
       DataGridView1(7, i * 47 + 42).Value = DataGridView1(6, i * 47 + 42).Value / 5
       DataGridView1(7, i * 47 + 43).Value = DataGridView1(7, i * 47 + 42).Value / 40
     Next
   End Sub
   Function g8(ByVal p As Integer, ByVal q As Integer) As Integer
     Dim w As Integer
     w = 0
     For i = 1 To 40
       w = w + a(p, i - 1, q)
     Next
     Return w
   End Function

   '各学期各教科の最高点・算出の算出・表示
   Sub f9()
     Dim i As Integer, j As Integer
     For i = 0 To 2
       For j = 1 To 6
         DataGridView1(j, i * 47 + 44).Value = g91(i, j)
         DataGridView1(j, i * 47 + 45).Value = g92(i, j)
       Next
     Next
   End Sub
   Function g91(ByVal p As Integer, ByVal q As Integer) As Integer
     Dim max As Integer
     max = 0
     For i = 1 To 40
       If a(p, i - 1, q) > max Then max = a(p, i - 1, q)
     Next
     Return max
   End Function
   Function g92(ByVal p As Integer, ByVal q As Integer) As Integer
     Dim min As Integer
     min = 0
     For i = 1 To 40
       If a(p, i - 1, q) < min Then min = a(p, i - 1, q)
     Next
     Return min
   End Function

   '各学期の並び替え
   Sub f10()
     Dim i As Integer, j As Integer
     For i = 0 To 2
       For j = 40 To 2 Step -1
         h10(i, j, g10(i, j))
       Next
     Next
   End Sub
   Function g10(ByVal p As Integer, ByVal q As Integer) As Integer
     Dim min, bn, i, w As Integer
     min = 500
     For i = 1 To q
       If a(p, i - 1, 6) < min Then
         min = a(p, i - 1, 6)
         bn = i
       End If
     Next
     Return bn
   End Function
   Sub h10(ByVal p As Integer, ByVal q As Integer, ByVal bn As Integer)
     Dim i As Integer, w As Integer, w1 As String
     For i = 0 To 6
       w = a(p, bn - 1, i)
       a(p, bn - 1, i) = a(p, q - 1, i)
       a(p, q - 1, i) = w
     Next
     For i = 0 To 12
       w1 = DataGridView1(i, p * 47 + bn + 1).Value
       DataGridView1(i, p * 47 + bn + 1).Value = DataGridView1(i, p * 47 + q + 1).Value
       DataGridView1(i, p * 47 + q + 1).Value = w1
     Next
   End Sub

   '各学期順位付け
   Sub f11()
     Dim i As Integer, j As Integer
     For i = 0 To 2
       For j = 1 To 40
         DataGridView1(10, i * 47 + j + 1).Value = j
       Next
       For j = 2 To 40
         If a(i, j - 1, 6) = a(i, j - 2, 6) Then DataGridView1(10, i * 47 + j + 1).Value = DataGridView1(10, i * 47 + j).Value
       Next
     Next
   End Sub

   '各学期出席番号順並び替え
   Sub f12()
     Dim i As Integer, j As Integer
       For i = 0 To 2
         For j = 40 To 2 Step -1
           h12(i, j, g12(i, j))
         Next
       Next
   End Sub
   Function g12(ByVal p As Integer, ByVal q As Integer) As Integer
     Dim i As Integer, bn As Integer, max As Integer
     max = 0
     For i = 1 To q
       If a(p, i - 1, 0) > max Then
         max = a(p, i - 1, 0)
         bn = i
       End If
     Next
     Return bn
   End Function
   Sub h12(ByVal p As Integer, ByVal q As Integer, ByVal bn As Integer)
     Dim i As Integer, w As Integer, w1 As String
     For i = 0 To 6
       w = a(p, bn - 1, i)
       a(p, bn - 1, i) = a(p, q - 1, i)
       a(p, q - 1, i) = w
     Next
     For i = 0 To 12
       w1 = DataGridView1(i, p * 47 + bn + 1).Value
       DataGridView1(i, p * 47 + bn + 1).Value = DataGridView1(i, p * 47 + q + 1).Value
       DataGridView1(i, p * 47 + q + 1).Value = w1
     Next
   End Sub

End Class


以上で第8講は終了とします。卒業まで残り2講のみです。がんばりましょう。

第4話へ 第9講第1話へ

006

VC++講義第1部へ
vb講義へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座

数学研究室に戻る