2012年11月5日 星期一

第一次儲備選手選拔試卷第四題


電費計算:從指定目錄"in.txt"讀取一數字作為用電度數計算出電費,輸出至指定目錄"out.txt"。
電費算法為:電錶每月租金30元,用電在40度之內,每度2.2元,40度至100度,每度3元,超過100度,每度3.5元。

輸入範例:80

輸出範例:270

3 則留言:

  1. Dim num As Long
    Dim already As Boolean
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Input #1, num
    Close #1
    already = False

    If already = False And num < 40 Then
    num = num * 2.2
    already = True
    End If

    If already = False And num = 40 Or num = 100 Or (num < 100 And 40 < num) Then
    num = num * 3
    already = True
    End If

    If already = False And num > 100 Then
    num = num * 3.5
    already = True
    End If

    Open App.Path & "\out.txt" For Output As #1
    Print #1, num + 30
    Close #1
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    Select Case x
    Case Is < 40
    n = 2.2 * x
    Case Is >= 40 And x <= 100
    n = 3 * x
    Case Is > 100
    n = 3.5 * x
    End Select
    Print #2, n + 30
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    '電費算法為:電錶每月租金30元,
    '用電在40度之內,每度2.2元,40
    '度至100度,每度3元,超過100度,每度3.5元。
    Me.Hide
    Dim sum As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    sum = 0
    Select Case n
    Case Is < 40
    sum = n * 2.2 + 30
    Case Is > 100
    sum = n * 3.5 + 30
    Case Else
    sum = n * 3 + 30
    End Select
    Print #2, sum
    Close
    Close
    End
    End Sub

    回覆刪除