小考奇數偶數卷
'P1-全班平均
'開
'輸入
FileOpen(1, "P1.in", OpenMode.Input)
Dim ans = ""
Dim no() As String
Dim chi(), eng(), mat(), ttlc, ttle, ttlm As Integer
Dim cnt = -1
Do While Not EOF(1)
cnt += 1
'動態宣告
ReDim Preserve no(cnt), chi(cnt), eng(cnt), mat(cnt)
Input(1, no(cnt)) : Input(1, chi(cnt)) : Input(1, eng(cnt)) : Input(1, mat(cnt))
Loop
'關
FileClose()
'處理
For i = 0 To cnt
ttlc += chi(i)
ttle += eng(i)
ttlm += mat(i)
Next
'輸出
'(平均)
Dim avgc, avge, avgm As Single
avgc = ttlc / (cnt + 1)
avge = ttle / (cnt + 1)
avgm = ttlm / (cnt + 1)
For i = 0 To cnt
ans &= no(i) & "," & chi(i) & "," & eng(i) & "," & mat(i) & vbNewLine
Next
ans &= "," & Format(avgc, "##0.0") & "," & Format(avge, "##0.0") & "," & Format(avgm, "##0.0")
Me.TextBox1.Text = ans
My.Computer.FileSystem.WriteAllText("P1-1.out", ans, False)
'P1
'輸入
'開
FileOpen(1, "P1.in", OpenMode.Input)
Dim ans, no As String
Dim chi, eng, mat As Integer
Do While Not EOF(1)
Input(1, no) : Input(1, chi) : Input(1, eng) : Input(1, mat)
Dim avg As Single
avg = chi + eng + mat
ans &= no & "," & Format(avg, "##0.0") → 要寫在裡面
✩ans &= If(EOF(1), "", vbNewLine) → 不可多一行
Loop
'關
FileClose()
Me.TextBox2.Text = ans
My.Computer.FileSystem.WriteAllText("P1.out", ans, False)
'P2
'開
'輸入
FileOpen(1, "P2.in", OpenMode.Input)
Dim ans = ""
Dim n As String
Do Until EOF(1)
Input(1, n)
ans &= Bin(n)
ans &= If(EOF(1), "", vbNewLine)
Loop
'關
FileClose()
'輸出
Me.TextBox3.Text = ans
My.Computer.FileSystem.WriteAllText("P2.out", ans, False)
'P3
'開
'輸入
FileOpen(1, "P3.in", OpenMode.Input)
Dim ans = ""
Dim s As String
Do Until EOF(1)
Input(1, s)
Loop
'關
FileClose()
'處理
Dim i As Integer
For i = 1 To s
For j = 1 To i
ans &= i
Next
ans &= If(i = s, "", vbNewLine)
Next
'輸出
Me.TextBox4.Text = ans
My.Computer.FileSystem.WriteAllText("P3.out", ans, False)
'P4
'輸入
Dim ans = ""
Dim a, b As Integer
a = InputBox("a = ", "P4.求最小公倍數lcm", 0)
b = InputBox("b = ", "P4.求最小公倍數lcm", 0)
ans &= lcm(a, b)
'輸出
Me.TextBox5.Text = ans
'P5
'開
'輸入
FileOpen(1, "P5.in", OpenMode.Input)
Dim ans = ""
Dim a, b As Integer
Do While Not EOF(1)
Input(1, a) : Input(1, b)
ans &= lcm(a, b)
ans &= If(EOF(1), "", vbNewLine)
Loop
'關
FileClose()
'處理
'輸出
Me.TextBox6.Text = ans
My.Computer.FileSystem.WriteAllText("P5.out", ans, False)
'Q1
'開
'輸入
FileOpen(1, "Q1.in", OpenMode.Input)
Dim ans = ""
Dim s As String
Do Until EOF(1)
Input(1, s)
Loop
'關
FileClose()
'處理
Dim i As Integer
For i = 1 To s
For j = 1 To i
ans &= j
Next
ans &= If(i = s, "", vbNewLine)
Next
'輸出
Me.TextBox9.Text = ans
My.Computer.FileSystem.WriteAllText("Q1.out", ans, False)
'Q2
'開
'輸入
FileOpen(1, "Q2.in", OpenMode.Input)
Dim ans = ""
Dim n As String
Do Until EOF(1)
Input(1, n)
ans &= Quo(n)
ans &= If(EOF(1), "", vbNewLine)
Loop
'關
FileClose()
'輸出
Me.TextBox10.Text = ans
My.Computer.FileSystem.WriteAllText("Q2.out", ans, False)
'Q3
'輸入
Dim ans = ""
Dim a, b As Integer
a = InputBox("a = ", "Q3.求GCD1", 0)
b = InputBox("b = ", "Q3.求GCD1", 0)
ans &= GCD1(a, b)
'輸出
Me.TextBox8.Text = ans
'Q4
'開
'輸入
FileOpen(1, "Q4.in", OpenMode.Input)
Dim ans = ""
Dim a, b As Integer
Do While Not EOF(1)
Input(1, a) : Input(1, b)
ans &= GCD1(a, b)
ans &= If(EOF(1), "", vbNewLine)
Loop
'關
FileClose()
'處理
'輸出
Me.TextBox7.Text = ans
My.Computer.FileSystem.WriteAllText("Q4.out", ans, False)
'Q5
'輸入
'開
FileOpen(1, "Q5.in", OpenMode.Input)
Dim ans, no As String
Dim chi, eng, mat As Integer
Do While Not EOF(1)
Input(1, no) : Input(1, chi) : Input(1, eng) : Input(1, mat)
Dim avg As Single
avg = (chi + eng + mat) / 3
ans &= no & "," & Format(avg, "##0.0")
ans &= If(EOF(1), "", vbNewLine)
Loop
'關
FileClose()
Me.TextBox11.Text = ans
My.Computer.FileSystem.WriteAllText("Q5.out", ans, False)
~~~~~~~~~~~~~~~~~~~~~~~模組~~~~~~~~~~~~~~~~~~~~~~~
Function Bin(n)
If n < 2 Then
Return n
Else
Return Bin(n \ 2) & (n Mod 2)
End If
End Function
'GCD
Function GCD(a, b)
If b = 0 Then
Return a
Else
Return GCD(b, a Mod b)
End If
End Function
'lcm
Function lcm(a, b)
Return a * b / GCD(a, b)
End Function
Function Quo(n)
If n < 4 Then
Return n
Else
Return Quo(n \ 4) & (n Mod 4)
End If
End Function
'GCD1
Function GCD1(a, b)
If b = 0 Then
Return a
Else
Return GCD1(b, a Mod b)
End If
~~~~~~~~~~~~~~~~~~~~~~~結果~~~~~~~~~~~~~~~~~~~~~~~
沒有留言:
張貼留言