190 likes | 317 Views
情報基礎実習 I ( 第 10 回). 木 曜4・5限 担当 :北川 晃. 例題: 2 点間 の距離の計算. A 点 と B 点 の座標を入力し, 2 点間の距離を 計算して表示するプログラムを作れ.. 2 点間の距離の計算:プログラム例. Label1 ~ 5 , Textbox1 ~ 5 , Button1, 2 の貼り付け. End. Dim x1, y1, x2, y2, d As Object x1 = Val(TextBox1.Text) y1 = Val(TextBox2.Text) x2 = Val(TextBox3.Text )
E N D
情報基礎実習I(第10回) 木曜4・5限 担当:北川 晃
例題:2点間の距離の計算 A点 とB点 の座標を入力し,2点間の距離を 計算して表示するプログラムを作れ.
2点間の距離の計算:プログラム例 Label1~5,Textbox1~5,Button1, 2の貼り付け End Dim x1, y1, x2, y2, d As Object x1 = Val(TextBox1.Text) y1 = Val(TextBox2.Text) x2 = Val(TextBox3.Text) y2 = Val(TextBox4.Text) d = Math.Sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2) TextBox5.Text = Format(d, "####.0")
Format関数 Format(式,”書式”) • 例: • Format(123.567, “General Number”) 123.567 • Format(123.567, “Fixed”) 123.57 • Format(1235.67, “Standard”) 1,235.67 • Format(123.567, “Percent”) 12356.70% • Format(1235.67, “Currency”) \1,236 • Format(123.567, “Scientific”) 1.24E+03 小数点以下 3桁目を 四捨五入 Fixedの表示に 3桁ごとにコンマ 先頭に\記号と 3桁ごとにコンマ
例: • Format(123.567, “####”) 124 • Format(123.567, “0000”) 0124 • Format(123.567, “###.#”) 123.6 • Format(123.567, “###.#0”) 123.57 • Format(0.5, “###.##%”) 50.% • Format(0.5, “###.#0%”) 50.00% • Format(10000, “##,###円”) 10,000円 • Format(10000, “\\##,###”) \10,000 Format関数(つづき) 0の場合は省略 0であっても表示 小数点以下2桁目を 四捨五入 小数点以下3桁目を 四捨五入 先頭に\記号と 3桁ごとにコンマ
例題:時間の計算 秒数をキーボードから入力し,何時間何分何秒に なるか計算して表示するプログラムを作れ.
時間の計算:プログラム例 Label1~4,Textbox1~4,Button1, 2の貼り付け TextBox1.Text = "" : TextBox2.Text = "" TextBox3.Text = "" : TextBox4.Text = "" Dim b, m, a, h, s As Integer b = Val(TextBox1.Text) : h = b \ 3600 a = b Mod 3600 m = a \ 60 s = a Mod 60 TextBox2.Text = h TextBox3.Text = m TextBox4.Text = s
プログラミング演習 • 「3時21分50秒から8時17分46秒までは何時間 何分 何秒か」 • というように,一般に時分秒の組で表された二つの時刻の • 間の時間を計算したい. • H1時M1分S1秒 から H2時M2分S2秒 までの時間 • H3時間M3分S3秒を計算して表示させるプログラムを作れ.
経過時間の計算:考え方 • 時分秒の計算は,時刻をすべて秒単位に換算して • 計算するのが簡単である.秒への変換は, • 3600×(時)+60×(分)+(秒) • とすればよい. • 秒単位の大きな数Kから時分秒への換算は, • Kを3600で割る……………… その商が「時間」 • 余りを60で割る……………… その商が「分」 • 余りが「秒」 • 各変数を整数型として取り扱うとよい.
経過時間の計算:プログラム例 Label1~9,Textbox1~9,Button1~3の貼り付け End TextBox1.Text = "" : TextBox2.Text = "" TextBox3.Text = "" : TextBox4.Text = "" TextBox5.Text = "" : TextBox6.Text = "" TextBox7.Text = "" : TextBox8.Text = "" TextBox9.Text = ""
経過時間の計算:プログラム例(つづき) 「計算」ボタンのプログラム Dim H1, M1, S1, H2, M2, S2, H3, M3, S3, K1, K2, K, amari As Integer H1 = Val(TextBox1.Text): M1 = Val(TextBox2.Text) S1 = Val(TextBox3.Text) H2 = Val(TextBox4.Text) : M2 = Val(TextBox5.Text) S2 = Val(TextBox6.Text) K1 = 3600 * H1 + 60 * M1 + S1 : K2 = 3600 * H2 + 60 * M2 + S2 K = K2 - K1 H3 = K \ 3600 : amari= K Mod 3600 M3 = amari \ 60 : S3 = amari Mod 60 TextBox7.Text = H3 TextBox8.Text = M3 TextBox9.Text = S3
経過時間の計算:プログラム例 Tabキーを押したとき, カーソルの動く順番
プログラミング演習 名前をn,国語,数学,英語の点数を それぞれjpn,math, engに入力し, 「○○さんの平均点は◇◇点です」 と表示するプログラムを作れ. 背景色は 周りと同じ
平均点の計算:プログラム例 Label1~6,Textbox1~6,Button1, 2の貼り付け Dim n, jpn, math, eng, mean n = TextBox1.Text jpn= Val(TextBox2.Text) math = Val(TextBox3.Text) eng= Val(TextBox4.Text) mean = (jpn + math + eng) / 3 TextBox5.Text = n TextBox6.Text = mean Textbox5, 6の BackColorは”Control”に, BoarderStyleは”None”に セットする
プログラミング演習 2つの数を入力し,「計算」ボタンをクリック すると,それぞれの計算結果を次のように 表示するプログラムを作れ.
四則演算:プログラム例 Label1~8,Textbox1~8,Button1, 2の貼り付け Dim m, n, wa, sa, seki, sho_int As Integer Dim amari As Integer, shoAs Single m = Val(TextBox1.Text) n = Val(TextBox2.Text) wa= m + n : TextBox3.Text = wa sa = m - n : TextBox4.Text = sa seki = m * n : TextBox5.Text = seki sho= m / n : TextBox6.Text = sho sho_int= m \ n : TextBox7.Text = sho_int amari = m Mod n : TextBox8.Text = amari
プログラミング演習 年利率0.5%の定期預金に預金したい. 預ける金額(元金)と預ける年数を入力し, 最終的に受け取る金額の合計を計算する プログラムを作れ. 背景色は 周りと同じ フォーマットを 工夫する フォント14pt, 赤色
定期預金の受取額:プログラム例 Label1~6,Textbox1~5,Button1~3の貼り付け Textbox3~5の BackColorは”Control”に, BoarderStyleは”None”に セットする
定期預金の受取額:プログラム例(つづき) 「計算」ボタンのプログラム Dim result, n, gankinAs Integer gankin = Val(TextBox1.Text) n = Val(TextBox2.Text) TextBox3.Text = Format(gankin, "\\###,###,###") TextBox4.Text = Format(n, "###") result = gankin * (1 + 0.5 / 100) ^ n TextBox5.Text = Format(result,"\\###,###,###")