2015年6月26日 星期五

資處程式 6/26 甄選(第3題)

參考程式碼
' 第3題 字串處理,一字串中只有大寫字母、數字並包含1個運算符號(+-*)之一,
'                 符號左右的數字範圍為 1~999
 Dim line() = Split(TextBox1.Text, vbNewLine)
 Dim n As Integer = Val(line(0))
 For i = 1 To n
   Label1.Text &= vbCrLf
   Dim line1 As String = line(i)
   Dim m = line1.Length
   Dim j As Integer
   Dim num1 As String = "", num2 As String = "", op As String = ""
  '將0~9串接至 Num1 ,直到出現 運算子 + - *
   For j = 1 To m
   Dim c = Mid(line1, j, 1)
     If c >= "0" And c <= "9" Then
        num1 += c    '數字串接至 num1
     ElseIf c = "+" Or c = "-" Or c = "*" Then
        op = c
        Exit For
     End If
   Next
   Dim k = j  ' 運算子出現的位置,從右邊1格開始往後,串接 Num2
   For j = k + 1 To m
      Dim c = Mid(line1, j, 1)
      If c >= "0" And c <= "9" Then
         num2 += c    '數字串接至 num2
      End If
   Next
  '依 op 符號來計算
   Dim sum As Integer = num1
   If op = "+" Then
      sum += num2
   ElseIf op = "-" Then
      sum -= num2
   Else
      sum *= num2
   End If
   Label1.Text &= num1 & op & num2 & " = " & sum
Next i
---------第3題 資料---
5
1A3CF+HK42M6   Ans: 13+426 = 439
X56J0O*2I3            Ans: 560*23 = 12880
X56J0O-7P2I3   Ans: 560-723 = -163
K2J5K+L6D7F8JD   Ans: 25+678 = 703
O1I2*EJ4FI5RL6K   Ans: 12*456 = 5472

0 意見:

張貼留言