2017年1月7日 星期六

1230

        'a001
        '開檔
            FileOpen(1, "a001.in", OpenMode.Input)
        '輸入
        '處理
        Dim s, ans As String
        Do Until EOF(1)   '一直做到檔尾
            Input(1, s)  '讀一筆
            ans &= "hello, " & s & If(EOF(1), "", vbNewLine)
        Loop
        '關檔
        FileClose()  '全關
        '輸出
        Me.TextBox1.Text = ans
        My.Computer.FileSystem.WriteAllText("a001.out", ans, False)
   
        'A7. 讀入第 A1 題的輸入檔(A1.in),由大到小排序後,依序將「奇數」輸出到輸出檔(A7.out)。
        '開檔
        FileOpen(1, "A1.in", OpenMode.Input)
        '輸入
        '處理
        Dim ans = ""
        Dim d() As Integer  '動態宣告
        Dim cnt = -1       '計數器
        Do Until EOF(1)
            cnt += 1
            ReDim Preserve d(cnt)   '調整資料大小
            Input(1, d(cnt))
        Loop
        '關檔
        FileClose()
        Array.Sort(d)
        Array.Reverse(d)    '反轉
        For i = 0 To UBound(d)
            ans &= If(d(i) Mod 2 <> 0, d(i) & vbNewLine, "")
        Next
        '輸出
        Me.TextBox2.Text = ans
        My.Computer.FileSystem.WriteAllText("A7.out", ans, False)
   
        'A5. 讀入學生成績檔(A5.csv, 不只一筆),計算所有學生的數學平均、計概平均。
        '開檔
        FileOpen(1, "A5.csv", OpenMode.Input)
        '表頭
        '輸入
        Dim tt1, tt2, tt3, tt4
        Input(1, tt1) : Input(1, tt2) : Input(1, tt3) : Input(1, tt4)
        Dim no(1000) As String   '座號
        Dim d(1000, 2) As Single '成績
        Dim cnt = -1
        Do While Not EOF(1)
            cnt += 1
            Input(1, no(cnt)) : Input(1, d(cnt, 0)) : Input(1, d(cnt, 1)) : Input(1, d(cnt, 2))
        Loop
        '關檔
        FileClose()
        '處理
        Dim mathttl As Single = 0, bccttl As Double = 0
        For i = 0 To cnt
            bccttl += d(i, 0)
            mathttl += d(i, 2)
        Next
        Dim ans = "數學平均,計概平均" & vbNewLine
        ans &= Format(mathttl / (cnt + 1), "#0.0") & "," & Format(bccttl / (cnt + 1), "#0.0")
        Me.TextBox3.Text = ans
        My.Computer.FileSystem.WriteAllText("A5-out.csv", ans, False)
   
        'A4. 利用廻圈控制指令,由輸入檔(A4.in,只有一筆)讀入整數數字,列印從 1 開始直到該數字為止之直角三角形。
        '開檔
        FileOpen(1, "A4.in", OpenMode.Input)
        '輸入
        '處理
        Dim s As String
        Dim ans = ""
        Do Until EOF(1)
            Input(1, s)
        Loop
        Dim j As Integer
        For j = 1 To s
            For i = 1 To j
                ans &= i
            Next
            ans &= vbNewLine
        Next
        '關檔
        FileClose()
        '輸出
        Me.TextBox4.Text = ans
   
        'A2. 數字轉地支
        '開檔
        FileOpen(1, "A2.in", OpenMode.Input)
        '輸入
        '處理
        Dim s As String
        Dim ans = ""
        Do Until EOF(1)
            Input(1, s)
            ans &= D2Di(s)
        Loop
        '關檔
        FileClose()
        '輸出      
        Me.TextBox5.Text = ans
 
        'B1. 利用廻圈控制指令,由輸入檔(B1.in,只有一筆)讀入整數數字,列印從 1 開始直到該數字為止之直角三角形。
        '開檔
        FileOpen(1, "B1.in", OpenMode.Input)
        '輸入
        '處理
        Dim s As String
        Dim ans = ""
        Do Until EOF(1)
            Input(1, s)
        Loop
        Dim j As Integer
        For j = 1 To s
            For i = 1 To j
                ans &= i
            Next
            ans &= vbNewLine
        Next
        '關檔
        FileClose()
        '輸出
        Me.TextBox7.Text = ans

        'B3. 數字轉天干
        '開檔
        FileOpen(1, "B3.in", OpenMode.Input)
        '處理
        Dim s As String
        Dim ans = ""
        Do Until EOF(1)
            Input(1, s)
            ans &= D2Ten(s)
        Loop
        '關檔
        FileClose()
        '輸出
        Me.TextBox6.Text = ans
   
        'A3. 算術運算+檔案
        '開檔
        FileOpen(1, "A3.in", OpenMode.Input)
        '輸入

        Dim ans = ""
        Dim a(), b() As Integer  '動態宣告
        Dim cnt = -1    '計數器
        Do Until EOF(1)
            cnt += 1
            ReDim Preserve a(cnt)
            ReDim Preserve b(cnt)
            Input(1, a(cnt))
            Input(1, b(cnt))
        Loop
        '處理
        For i = 0 To UBound(a)
            ans &= a(i) \ b(i) & ", " & Math.Abs(a(i) - b(i)) & If(i = UBound(b), "", vbNewLine)
        Next
        '關檔
        FileClose()
        '輸出
        Me.TextBox8.Text = ans
        My.Computer.FileSystem.WriteAllText("A3.out", ans, False)
 
        'A1. 讀入輸入檔(A1.in,內容為 1 至 13 之間的整數若干筆),計算各數字出現的次數。
        '開檔
        FileOpen(1, "A1.in", OpenMode.Input)
        '輸入
        Dim ans = ""
        Dim a(), b() As Integer  '動態宣告
        Dim cnt = -1    '計數器
        Do Until EOF(1)
            cnt += 1
            ReDim Preserve a(cnt)
            ReDim Preserve b(cnt)
            Input(1, a(cnt))
        Loop
        '處理
        For Each p In a  '陣列
            b(p) += 1
        Next
        '關檔
        FileClose()
        '輸出
        ans &= RSet("數字", 2) & "   " & "次數" & vbNewLine
        For i = 1 To 13
            ans &= RSet(i, 2) & "      " & b(i) & vbNewLine
        Next
        Me.TextBox9.Text = ans

~~~~~~~~~~~~~~~~~~~~~~~模組~~~~~~~~~~~~~~~~~~~~~~~
    '查表
    Function D2Di(s)
        Dim c() = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}
        Dim ans = ""
        '轉地支
        For i = 1 To Len(s)
            ans &= c(Val(Mid(s, i, 1)))
        Next
        Return ans
    End Function
    '查表
    Function D2Ten(s)
        Dim c() = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}
        Dim ans = ""
        '轉天干
        For i = 1 To Len(s)
            ans &= c(Val(Mid(s, i, 1)))

        Next
        Return ans
    End Function

~~~~~~~~~~~~~~~~~~~~~~~結果~~~~~~~~~~~~~~~~~~~~~~~


沒有留言:

張貼留言