Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.00 KB | None | 0 0
  1. Module Module1
  2.  
  3. Sub main()
  4. Do
  5. Console.WriteLine("輸入兩個數字 進行加減乘除(90字以內)")
  6. Console.WriteLine("若輸入都是0 則進行讀檔 若都輸入-1 則關閉程式")
  7. Console.Write("第一數 : ")
  8. Dim s1, s2 As String
  9. s1 = Console.ReadLine : Console.Write("第二數 : ") : s2 = Console.ReadLine
  10. If s1 = "" Then s1 = "0" : If s2 = "" Then s2 = "0"
  11. If s1 = "0" And s2 = "0" Then
  12. Dataread() : Exit Sub
  13. ElseIf s1 = "-1" And s2 = "-1" Then
  14. Exit Sub
  15. Else
  16. edm_cmd(s1, s2)
  17. End If
  18. Loop
  19. End Sub
  20. Sub Dataread()
  21. FileOpen(1, "in1.txt", 1)
  22. FileOpen(2, "in2.txt", 1)
  23. FileOpen(3, "out.txt", 2)
  24. For tx = 1 To 2
  25. Dim a As Integer : Input(tx, a)
  26. For ty = 1 To a
  27. Dim s1, s2 As String : s1 = LineInput(tx) : s2 = LineInput(tx)
  28. If s1 = "" Then s1 = "0" : If s2 = "" Then s2 = "0"
  29. PrintLine(3, edm_read(s1, s2))
  30. Next
  31. PrintLine(3)
  32. Next
  33. Console.WriteLine("讀檔輸出完成 程式即將關閉")
  34. End Sub
  35. Sub edm_cmd(ByVal a As String, ByVal b As String)
  36. Dim rua, rub As String : rua = a : rub = b '將原始兩數儲存
  37. Dim nega, negb As Boolean : nega = False : negb = False '先判斷是否有負數
  38. If Mid(a, 1, 1) = "-" Then
  39. nega = True : a = Mid(a, 2, a.Length) 'a如果是負數 負數提出 nega= true 並將a設為正數
  40. End If
  41. If Mid(b, 1, 1) = "-" Then
  42. negb = True : b = Mid(b, 2, b.Length) 'b如果是負數 負數提出 negb= true 並將b設為正數
  43. End If
  44. Dim ap, an, am, aq As String : Dim aout As String = "" : If nega Xor negb Then am = "-" : aq = am '如果ab異號 相乘|相除 會是負數 先將結果添加負數
  45. ap = qad(a, b, nega, negb, numcheck(a, b)) : Console.WriteLine(rua & " + " & rub & " = " & ap)
  46. an = qan(a, b, nega, negb, numcheck(a, b)) : Console.WriteLine(rua & " - " & rub & " = " & an)
  47. am &= qm(a, b) : Console.WriteLine(rua & " * " & rub & " = " & am)
  48. aq &= qa(a, b, numcheck(a, b)) : Console.WriteLine(rua & " / " & rub & " = " & aq)
  49. End Sub
  50. Function edm_read(ByVal a As String, ByVal b As String)
  51. Dim rua, rub As String : rua = a : rub = b '將原始兩數儲存
  52. Dim nega, negb As Boolean : nega = False : negb = False '先判斷是否有負數
  53. If Mid(a, 1, 1) = "-" Then
  54. nega = True : a = Mid(a, 2, a.Length) 'a如果是負數 負數提出 nega= true 並將a設為正數
  55. End If
  56. If Mid(b, 1, 1) = "-" Then
  57. negb = True : b = Mid(b, 2, b.Length) 'b如果是負數 負數提出 negb= true 並將b設為正數
  58. End If
  59. Dim ap, an, am, aq As String : Dim aout As String = "" : If nega Xor negb Then am = "-" : aq = am '如果ab異號 相乘|相除 會是負數 先將結果添加負數
  60. ap = qad(a, b, nega, negb, numcheck(a, b)) : ap = rua & " + " & rub & " = " & ap & Chr(13)
  61. an = qan(a, b, nega, negb, numcheck(a, b)) : an = rua & " - " & rub & " = " & an & Chr(13)
  62. am &= qm(a, b) : am = rua & " * " & rub & " = " & am & Chr(13)
  63. aq &= qa(a, b, numcheck(a, b)) : aq = rua & " / " & rub & " = " & aq
  64. Return aout.ToString
  65. End Function
  66. Function numcheck(ByVal a As String, ByVal b As String) '兩數大小比對
  67. '設定a,b字串長度 並判斷大小 lx>>2:a大 1:b大 0:都一樣大
  68. If a = b Then Return 0 : Exit Function '如果a=b 兩數一樣大
  69. Dim la, lb, lx As Integer : la = a.Length : lb = b.Length '先設定la,lb 為兩數長度
  70. If la > lb Then lx = 2 Else If la < lb Then lx = 1 '如果la>lb a大 反之
  71. If la = lb Then '如果la=lb 進行每個字判斷(由大數->小數)
  72. For i = 1 To a.Length
  73. If Val(Mid(a, i, 1)) > Val(Mid(b, i, 1)) Then '比對相同位數的數字大小
  74. lx = 2 : Exit For
  75. ElseIf Val(Mid(a, i, 1)) < Val(Mid(b, i, 1)) Then
  76. lx = 1 : Exit For
  77. End If
  78. Next
  79. End If
  80. Return lx '完成ab大小判斷
  81. End Function
  82. Function qa(ByVal a As String, ByVal b As String, ByVal lx As Integer) '除法
  83. If b = "0" Or b = "" Then Return "除數不得為0" : Exit Function
  84. If a = "0" Or a = "" Then Return "被除數為0 = 0" : Exit Function
  85. If lx = 1 Then Return "0" : Exit Function
  86. Dim la, lb As Integer : la = a.Length : lb = b.Length
  87. Dim tn As String = ""
  88. For i = lb To la
  89. Dim stn As String = b
  90. For j = la - i To 1 Step -1
  91. stn &= "0"
  92. Next
  93. Dim stn2 As String = stn : Dim qot As String = "" : Dim qt As Integer = 0
  94. Do
  95. Dim unc As Integer = numcheck(a, stn)
  96. If unc = 2 Then
  97. stn = add(stn, stn2) : qt += 1
  98. ElseIf unc = 1 Or unc = 0 Then
  99. stn = rv(stn, stn2) : Exit Do
  100. End If
  101. Loop
  102. a = rv(a, stn)
  103. tn &= qt
  104. Next
  105. Dim tnl As Integer = 0
  106. For i = 1 To tn.Length
  107. If Mid(tn, 1, 1) <> "0" Then Exit For Else tn = Mid(tn, 2, tn.Length)
  108. Next
  109. Return tn.ToString
  110. End Function
  111. Function qm(ByVal a As String, ByVal b As String) '乘法
  112. If a = "0" Or b = "0" Or a = "" Or b = "" Then Return 0 : Exit Function
  113. Dim lb As Integer = b.Length : Dim lmp(180), lmo As String : lmo = "0"
  114. For i = 1 To lb
  115. If lmp(i) = "" Then lmp(i) = "0"
  116. For j = Val(Mid(b, lb - i + 1, 1)) To 1 Step -1
  117. lmp(i) = add(lmp(i), a)
  118. Next
  119. Next
  120. For i = 1 To 180
  121. If lmp(i) = Nothing Then ReDim Preserve lmp(i - 1) : Exit For
  122. Next
  123. If lmp.Length > 2 Then
  124. For i = 2 To lmp.Length - 1
  125. For j = 2 To i
  126. lmp(i) = lmp(i) & "0"
  127. Next
  128. Next
  129. End If
  130. For i = 1 To lmp.Length - 1
  131. lmo = add(lmo, lmp(i))
  132. Next
  133. Return lmo.ToString
  134. End Function
  135. Function qad(ByVal a As String, ByVal b As String, ByVal nega As Boolean, ByVal negb As Boolean, ByVal lx As Integer) '加法 前置
  136. If a = "0" Or a = "" Then Return b.ToString : Exit Function
  137. If b = "0" Or b = "" Then Return a.ToString : Exit Function
  138. Dim outsp As String = "" : Dim tep As String = ""
  139. If lx = 0 Then
  140. If nega Xor negb Then outsp &= 0 Else tep = add(a, a)
  141. If nega = True And negb = True Then outsp &= "-" & tep
  142. If nega = False And negb = False Then outsp &= tep
  143. ElseIf lx = 1 Then
  144. If nega Xor negb Then tep = rv(b, a) Else tep = add(b, a)
  145. If negb = True Then outsp &= "-"
  146. outsp &= tep
  147. ElseIf lx = 2 Then
  148. If nega Xor negb Then tep = rv(a, b) Else tep = add(a, b)
  149. If nega = True Then outsp &= "-"
  150. outsp &= tep
  151. End If
  152. Return outsp.ToString
  153. End Function
  154. Function qan(ByVal a As String, ByVal b As String, ByVal nega As Boolean, ByVal negb As Boolean, ByVal lx As Integer) '減法 前置
  155. Dim outsp As String = "" : Dim tep As String = ""
  156. If lx = 0 Then
  157. If nega = negb Then outsp &= 0 Else tep = add(a, b)
  158. If nega = True And negb = False Then outsp &= "-" & tep
  159. If nega = False And negb = True Then outsp &= tep
  160. ElseIf lx = 1 Then
  161. If nega Xor negb Then tep = add(b, a) Else tep = rv(b, a)
  162. If negb = False Then outsp &= "-"
  163. outsp &= tep
  164. ElseIf lx = 2 Then
  165. If nega Xor negb Then tep = add(a, b) Else tep = rv(a, b)
  166. If nega = True Then outsp &= "-"
  167. outsp &= tep
  168. End If
  169. Return outsp.ToString
  170. End Function
  171. Function add(ByVal a As String, ByVal b As String) '加法(帶進任意兩數相加)
  172. Dim x(180), y(180) As Integer : ReDim x(180), y(180)
  173. Dim s_ad As String = ""
  174. Dim lo, r As Integer : lo = 0 : r = 0
  175. For i = a.Length To 1 Step -1
  176. x(a.Length - i) = Val(Mid(a, i, 1))
  177. Next
  178. For i = b.Length To 1 Step -1
  179. y(b.Length - i) = Val(Mid(b, i, 1))
  180. Next
  181. If a.Length > b.Length Then lo = a.Length - 1 Else lo = b.Length
  182. For i = 0 To lo - 1
  183. x(i) += y(i)
  184. Next
  185. Do
  186. If x(r) >= 10 Then x(r + 1) += 1 : x(r) -= 10 Else r += 1
  187. Loop Until r = lo
  188. If x(lo) = 0 Then lo -= 1
  189. For i = lo To 0 Step -1
  190. s_ad &= x(i)
  191. Next
  192. Return s_ad.ToString
  193. End Function
  194. Function rv(ByVal a As String, ByVal b As String) '減法(帶進任意兩數相減) 前提 a > b
  195. If a = b Then Return 0 : Exit Function
  196. If b = "0" Then Return a : Exit Function
  197. Dim x(180), y(180) As Integer : ReDim x(180), y(180)
  198. Dim s_ad As String = ""
  199. Dim lo, r As Integer : lo = 0 : r = 0
  200. For i = a.Length To 1 Step -1
  201. x(a.Length - i) = Val(Mid(a, i, 1))
  202. Next
  203. For i = b.Length To 1 Step -1
  204. y(b.Length - i) = Val(Mid(b, i, 1))
  205. Next
  206. If a.Length > b.Length Then lo = a.Length - 1 Else lo = b.Length
  207. For i = 0 To lo - 1
  208. x(i) -= y(i)
  209. Next
  210. Do
  211. If x(r) < 0 Then x(r + 1) -= 1 : x(r) += 10 Else r += 1
  212. Loop Until r = lo
  213. Do
  214. If x(lo) = 0 Then lo -= 1
  215. Loop Until x(lo) > 0
  216. For i = lo To 0 Step -1
  217. s_ad &= x(i)
  218. Next
  219. Return s_ad.ToString
  220. End Function
  221.  
  222. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement