Sub Main Dim v As Boolean True False
ランプの回路:プログラム例 Sub Main() Dim v() As Boolean = {True, False} Dim i_A, i_B, i_C, i_D As Integer Console. Write. Line(" A B C D L") Console. Write. Line("-----------------") For i_A = 0 To 1 For i_B = 0 To 1 For i_C = 0 To 1 For i_D = 0 To 1 Console. Write. Line("{0, 6} {1, 6} {2, 6} {3, 6} {4, 6}", _ v(i_A), v(i_B), v(i_C), v(i_D), _ lamp(v(i_A), v(i_B), v(i_C), v(i_D))) Next 電気回路に相当 Next する評価関数 End Sub
ランプの回路:プログラム例 Function lamp(a, b, c, d) Dim L As Boolean L = a Or ((b Or c) And d) Return L End Function 電気回路に相当 する評価関数
狩人の問題:プログラム例 Dim v() As Boolean = {True, False} Console. Write. Line(" a b c d") Console. Write. Line("--------------") For i As Integer = 0 To 1 For j As Integer = 0 To 1 For k As Integer = 0 To 1 For m As Integer = 0 To 1 If v(i) And v(j) Then Go. To point 1 If v(j) = True And v(m) = False _ Then Go. To point 1 If (v(j) And v(m)) And v(k) _ Then Go. To point 1 条件に当てはまらない場合は すべて,point 1へ飛ぶ
完全数:(あまりよくない)プログラミング例 Dim d, s, q As Integer For n As Integer = 6 To 100 d = 1 s = 1 point 1: d = d + 1 q = Int(n / d) If d > q Then Go. To point 2 If n > q * d Then Go. To point 1 s = s + d If q = d Then Go. To point 2 s = s + q If s > n Then Go. To point 3 Go. To point 1 point 2: If s = n Then Console. Write. Line(n) point 3: Next n Go. To文がいっぱいで,全体の構造をつかみにくい
方程式の解,数値積分:プログラミング例 Sub Main() Dim x 1, x 2, xm, xs, xl, h, s As Single Dim eps As Single = 10 ^ -6 Dim n As Integer = 100 積分の分割数 xs = -1 : xl = 0 While Math. Abs(xs - xl) > eps xm = (xs + xl) / 2 If f(xs) * f(xm) > 0 Then xs = xm Else xl = xm End If End While x 1 = xm 一つ目の解
方程式の解,数値積分:プログラミング例 xs = 1 : xl = 2 While Math. Abs(xs - xl) > eps xm = (xs + xl) / 2 If f(xs) * f(xm) > 0 Then xs = xm Else xl = xm End If End While x 2 = xm 二つ目の解 Console. Write. Line("x 1={0, 10}, x 2={1, 10}", x 1, x 2) h = (x 2 - x 1) / n 積分区間の分割 解の書き出し
方程式の解,数値積分:プログラミング例 台形公式の計算 s = 0 For i As Integer = 1 To n s = s + (f(x 1 + (i - 1) * h) + f(x 1 + i * h)) * h / 2 Next Console. Write. Line("囲まれる面積はS={0, 10}", Math. Abs(s)) End Sub Function f(x) Dim f 1 As Single f 1 = x ^ 2 - x - 1 Return f 1 End Function
- Slides: 18