Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module Module1
- Sub main()
- Do
- Console.WriteLine("輸入兩個數字 進行加減乘除(90字以內)")
- Console.WriteLine("若輸入都是0 則進行讀檔 若都輸入-1 則關閉程式")
- Console.Write("第一數 : ")
- Dim s1, s2 As String
- s1 = Console.ReadLine : Console.Write("第二數 : ") : s2 = Console.ReadLine
- If s1 = "" Then s1 = "0" : If s2 = "" Then s2 = "0"
- If s1 = "0" And s2 = "0" Then
- Dataread() : Exit Sub
- ElseIf s1 = "-1" And s2 = "-1" Then
- Exit Sub
- Else
- edm_cmd(s1, s2)
- End If
- Loop
- End Sub
- Sub Dataread()
- FileOpen(1, "in1.txt", 1)
- FileOpen(2, "in2.txt", 1)
- FileOpen(3, "out.txt", 2)
- For tx = 1 To 2
- Dim a As Integer : Input(tx, a)
- For ty = 1 To a
- Dim s1, s2 As String : s1 = LineInput(tx) : s2 = LineInput(tx)
- If s1 = "" Then s1 = "0" : If s2 = "" Then s2 = "0"
- PrintLine(3, edm_read(s1, s2))
- Next
- PrintLine(3)
- Next
- Console.WriteLine("讀檔輸出完成 程式即將關閉")
- End Sub
- Sub edm_cmd(ByVal a As String, ByVal b As String)
- Dim rua, rub As String : rua = a : rub = b '將原始兩數儲存
- Dim nega, negb As Boolean : nega = False : negb = False '先判斷是否有負數
- If Mid(a, 1, 1) = "-" Then
- nega = True : a = Mid(a, 2, a.Length) 'a如果是負數 負數提出 nega= true 並將a設為正數
- End If
- If Mid(b, 1, 1) = "-" Then
- negb = True : b = Mid(b, 2, b.Length) 'b如果是負數 負數提出 negb= true 並將b設為正數
- End If
- Dim ap, an, am, aq As String : Dim aout As String = "" : If nega Xor negb Then am = "-" : aq = am '如果ab異號 相乘|相除 會是負數 先將結果添加負數
- ap = qad(a, b, nega, negb, numcheck(a, b)) : Console.WriteLine(rua & " + " & rub & " = " & ap)
- an = qan(a, b, nega, negb, numcheck(a, b)) : Console.WriteLine(rua & " - " & rub & " = " & an)
- am &= qm(a, b) : Console.WriteLine(rua & " * " & rub & " = " & am)
- aq &= qa(a, b, numcheck(a, b)) : Console.WriteLine(rua & " / " & rub & " = " & aq)
- End Sub
- Function edm_read(ByVal a As String, ByVal b As String)
- Dim rua, rub As String : rua = a : rub = b '將原始兩數儲存
- Dim nega, negb As Boolean : nega = False : negb = False '先判斷是否有負數
- If Mid(a, 1, 1) = "-" Then
- nega = True : a = Mid(a, 2, a.Length) 'a如果是負數 負數提出 nega= true 並將a設為正數
- End If
- If Mid(b, 1, 1) = "-" Then
- negb = True : b = Mid(b, 2, b.Length) 'b如果是負數 負數提出 negb= true 並將b設為正數
- End If
- Dim ap, an, am, aq As String : Dim aout As String = "" : If nega Xor negb Then am = "-" : aq = am '如果ab異號 相乘|相除 會是負數 先將結果添加負數
- ap = qad(a, b, nega, negb, numcheck(a, b)) : ap = rua & " + " & rub & " = " & ap & Chr(13)
- an = qan(a, b, nega, negb, numcheck(a, b)) : an = rua & " - " & rub & " = " & an & Chr(13)
- am &= qm(a, b) : am = rua & " * " & rub & " = " & am & Chr(13)
- aq &= qa(a, b, numcheck(a, b)) : aq = rua & " / " & rub & " = " & aq
- Return aout.ToString
- End Function
- Function numcheck(ByVal a As String, ByVal b As String) '兩數大小比對
- '設定a,b字串長度 並判斷大小 lx>>2:a大 1:b大 0:都一樣大
- If a = b Then Return 0 : Exit Function '如果a=b 兩數一樣大
- Dim la, lb, lx As Integer : la = a.Length : lb = b.Length '先設定la,lb 為兩數長度
- If la > lb Then lx = 2 Else If la < lb Then lx = 1 '如果la>lb a大 反之
- If la = lb Then '如果la=lb 進行每個字判斷(由大數->小數)
- For i = 1 To a.Length
- If Val(Mid(a, i, 1)) > Val(Mid(b, i, 1)) Then '比對相同位數的數字大小
- lx = 2 : Exit For
- ElseIf Val(Mid(a, i, 1)) < Val(Mid(b, i, 1)) Then
- lx = 1 : Exit For
- End If
- Next
- End If
- Return lx '完成ab大小判斷
- End Function
- Function qa(ByVal a As String, ByVal b As String, ByVal lx As Integer) '除法
- If b = "0" Or b = "" Then Return "除數不得為0" : Exit Function
- If a = "0" Or a = "" Then Return "被除數為0 = 0" : Exit Function
- If lx = 1 Then Return "0" : Exit Function
- Dim la, lb As Integer : la = a.Length : lb = b.Length
- Dim tn As String = ""
- For i = lb To la
- Dim stn As String = b
- For j = la - i To 1 Step -1
- stn &= "0"
- Next
- Dim stn2 As String = stn : Dim qot As String = "" : Dim qt As Integer = 0
- Do
- Dim unc As Integer = numcheck(a, stn)
- If unc = 2 Then
- stn = add(stn, stn2) : qt += 1
- ElseIf unc = 1 Or unc = 0 Then
- stn = rv(stn, stn2) : Exit Do
- End If
- Loop
- a = rv(a, stn)
- tn &= qt
- Next
- Dim tnl As Integer = 0
- For i = 1 To tn.Length
- If Mid(tn, 1, 1) <> "0" Then Exit For Else tn = Mid(tn, 2, tn.Length)
- Next
- Return tn.ToString
- End Function
- Function qm(ByVal a As String, ByVal b As String) '乘法
- If a = "0" Or b = "0" Or a = "" Or b = "" Then Return 0 : Exit Function
- Dim lb As Integer = b.Length : Dim lmp(180), lmo As String : lmo = "0"
- For i = 1 To lb
- If lmp(i) = "" Then lmp(i) = "0"
- For j = Val(Mid(b, lb - i + 1, 1)) To 1 Step -1
- lmp(i) = add(lmp(i), a)
- Next
- Next
- For i = 1 To 180
- If lmp(i) = Nothing Then ReDim Preserve lmp(i - 1) : Exit For
- Next
- If lmp.Length > 2 Then
- For i = 2 To lmp.Length - 1
- For j = 2 To i
- lmp(i) = lmp(i) & "0"
- Next
- Next
- End If
- For i = 1 To lmp.Length - 1
- lmo = add(lmo, lmp(i))
- Next
- Return lmo.ToString
- End Function
- Function qad(ByVal a As String, ByVal b As String, ByVal nega As Boolean, ByVal negb As Boolean, ByVal lx As Integer) '加法 前置
- If a = "0" Or a = "" Then Return b.ToString : Exit Function
- If b = "0" Or b = "" Then Return a.ToString : Exit Function
- Dim outsp As String = "" : Dim tep As String = ""
- If lx = 0 Then
- If nega Xor negb Then outsp &= 0 Else tep = add(a, a)
- If nega = True And negb = True Then outsp &= "-" & tep
- If nega = False And negb = False Then outsp &= tep
- ElseIf lx = 1 Then
- If nega Xor negb Then tep = rv(b, a) Else tep = add(b, a)
- If negb = True Then outsp &= "-"
- outsp &= tep
- ElseIf lx = 2 Then
- If nega Xor negb Then tep = rv(a, b) Else tep = add(a, b)
- If nega = True Then outsp &= "-"
- outsp &= tep
- End If
- Return outsp.ToString
- End Function
- Function qan(ByVal a As String, ByVal b As String, ByVal nega As Boolean, ByVal negb As Boolean, ByVal lx As Integer) '減法 前置
- Dim outsp As String = "" : Dim tep As String = ""
- If lx = 0 Then
- If nega = negb Then outsp &= 0 Else tep = add(a, b)
- If nega = True And negb = False Then outsp &= "-" & tep
- If nega = False And negb = True Then outsp &= tep
- ElseIf lx = 1 Then
- If nega Xor negb Then tep = add(b, a) Else tep = rv(b, a)
- If negb = False Then outsp &= "-"
- outsp &= tep
- ElseIf lx = 2 Then
- If nega Xor negb Then tep = add(a, b) Else tep = rv(a, b)
- If nega = True Then outsp &= "-"
- outsp &= tep
- End If
- Return outsp.ToString
- End Function
- Function add(ByVal a As String, ByVal b As String) '加法(帶進任意兩數相加)
- Dim x(180), y(180) As Integer : ReDim x(180), y(180)
- Dim s_ad As String = ""
- Dim lo, r As Integer : lo = 0 : r = 0
- For i = a.Length To 1 Step -1
- x(a.Length - i) = Val(Mid(a, i, 1))
- Next
- For i = b.Length To 1 Step -1
- y(b.Length - i) = Val(Mid(b, i, 1))
- Next
- If a.Length > b.Length Then lo = a.Length - 1 Else lo = b.Length
- For i = 0 To lo - 1
- x(i) += y(i)
- Next
- Do
- If x(r) >= 10 Then x(r + 1) += 1 : x(r) -= 10 Else r += 1
- Loop Until r = lo
- If x(lo) = 0 Then lo -= 1
- For i = lo To 0 Step -1
- s_ad &= x(i)
- Next
- Return s_ad.ToString
- End Function
- Function rv(ByVal a As String, ByVal b As String) '減法(帶進任意兩數相減) 前提 a > b
- If a = b Then Return 0 : Exit Function
- If b = "0" Then Return a : Exit Function
- Dim x(180), y(180) As Integer : ReDim x(180), y(180)
- Dim s_ad As String = ""
- Dim lo, r As Integer : lo = 0 : r = 0
- For i = a.Length To 1 Step -1
- x(a.Length - i) = Val(Mid(a, i, 1))
- Next
- For i = b.Length To 1 Step -1
- y(b.Length - i) = Val(Mid(b, i, 1))
- Next
- If a.Length > b.Length Then lo = a.Length - 1 Else lo = b.Length
- For i = 0 To lo - 1
- x(i) -= y(i)
- Next
- Do
- If x(r) < 0 Then x(r + 1) -= 1 : x(r) += 10 Else r += 1
- Loop Until r = lo
- Do
- If x(lo) = 0 Then lo -= 1
- Loop Until x(lo) > 0
- For i = lo To 0 Step -1
- s_ad &= x(i)
- Next
- Return s_ad.ToString
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement