在輸入檔in1.txt中,第1行n表示共有幾組加減運算。
在每組加減運算中,包含3行,分別為x,a,y,其中a為「+」或「-」,每組表示 x + y 或 x - y。
請計算大數 加減法後,輸出到out1.txt檔。
輸入範例:in1.txt
2
123123123123123123
+
789789789789789789789
345345345345345345345345
-
678678678678678678678
輸出範例: out1.txt
789912912912912912912
344666666666666666666667
2011年4月11日 星期一
訂閱:
張貼留言 (Atom)
Dim x As String, y As String
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\out1.txt" For Output As #2
Open App.Path & "\in1.txt" For Input As #1
Input #1, n
For i = 1 To n
Input #1, x, k, y
If k = "+" Then ans = p(x, y)
If k = "-" Then ans = cc(x, y)
Print #2, ans
Next i
Close #1
Close #2
End
End Sub
Function p(a1, b1)
If Len(a1) < Len(b1) Then Call cz(a1, b1)
If Len(a1) > Len(b1) Then Call cz(b1, a1)
Lena1 = Len(a1)
lenb1 = Len(b1)
For i = Len(a1) To 1 Step -1
m1 = Mid(a1, i, 1)
n1 = Mid(b1, i, 1)
o1 = Val(m1) + Val(n1)
X1 = o1 Mod 10
z1 = X1 + Y1
Y1 = o1 \ 10
c1 = z1 & c1
Next i
If Y1 > 0 Then c1 = Y1 & c1
p = c1
End Function
Function cc(a2, b2)
gyn = False
lena2 = Len(a2)
lenb2 = Len(b2)
If lena2 > lenb2 Then Max = a2: Min = b2
If lena2 < lenb2 Then Max = b2: Min = a2: gyn = True
If lena2 = lenb2 Then
If Left(a2, 1) > Left(b2, 1) Then Max = a2: Min = b2 Else Max = b2: Min = a2
End If
If Len(Min) < Len(Max) Then Call cz(Min, Max)
c2 = p(Max, p(bc(Min), 1))
If gyn = True Then cc = "-" & Right(c2, Len(c2) - 1) Else cc = Right(c2, Len(c2) - 1)
End Function
Sub chn(a3, b3)
t3 = a3
a3 = b3
b3 = t3
End Sub
Sub cz(a4, b4)
Do
a4 = "0" & a4
If Len(a4) = Len(b4) Then Exit Do
Loop
End Sub
Function bc(a5)
For i = 1 To Len(a5)
b5 = b5 & (9 - Val(Mid(a5, i, 1)))
Next i
bc = b5
End Function
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in1.txt" For Input As #1
Open App.Path & "\out1.txt" For Output As #2
Dim times, X As String, Y As String, c As String, Ans As String
Input #1, times
For i = 1 To times
Input #1, X
Input #1, c
Input #1, Y
If c = "+" Then Ans = B1(X, Y)
If c = "-" Then Ans = B2(X, Y)
Print #2, Ans
Next
Close #2
Close #1
End
End Sub
Function B1(a As String, b As String)
Dim s1 As Integer, s2 As Integer, tmp, plus, AANS
Do While Len(a) <> Len(b)
If Len(a) > Len(b) Then
b = "0" & b
Else
a = "0" & a
End If
Loop
For i = Len(a) To 1 Step -1
s1 = Mid(a, i, 1)
s2 = Mid(b, i, 1)
plus = ((s1 + s2 + tmp) Mod 10)
tmp = (s1 + s2 + tmp) \ 10
AANS = plus & AANS
Next
AANS = tmp & AANS
Do While Left(AANS, 1) = "0"
AANS = Right(AANS, Len(AANS) - 1)
Loop
B1 = AANS
End Function
Function B2(a As String, b As String)
Dim tmp As Integer, Newb As String, AANS As String, NEWAANS As String
Do While Len(a) <> Len(b)
If Len(a) > Len(b) Then
b = "0" & b
Else
a = "0" & a
End If
Loop
For i = Len(b) To 1 Step -1
tmp = Mid(b, i, 1)
tmp = 9 - tmp
Newb = tmp & Newb
Next
b = B1(Newb, "1")
AANS = B1(a, b)
If Len(AANS) > Len(a) Then
AANS = Right(AANS, Len(AANS) - 1)
Else
For i = Len(AANS) To 1 Step -1
tmp = Mid(AANS, i, 1)
tmp = 9 - tmp
NEWAANS = tmp & NEWAANS
Next
AANS = "-" & B1(NEWAANS, "1")
End If
B2 = AANS
End Function
to >>緣尉
回覆刪除程式有錯誤~
輸入:
1
10
-
2
你的會輸出:
08
正確應該是
-8
-------
然後輸入:
1
9999999999999999999999
+
1
你的會輸出:
99999999999999999999100
緣尉好,
回覆刪除1.
o1 = Val(m1) + Val(n1)
X1 = o1 Mod 10
z1 = X1 + Y1
Y1 = o1 \ 10
c1 = z1 & c1
-->
這幾行的計算順序有問題。
o1=val(m1) + val(n1) + y1
x1 = o1 mod 10
y1= o1 \ 10
c1= x1 & c1
因為你的z1=x1 + y1要是超過10怎麼辦?
2.多學些英文吧,在程式中命名變數,還是用些英文意義的單字,會好些。
像是加法,用myADD ,減法用mySUB,在以後的引用時,比較有字面意思些。
3. 補0的副程式,用addzero命名也是不錯的方式。
Sub cz(a4, b4)
Do
a4 = "0" & a4
If Len(a4) = Len(b4) Then Exit Do
Loop
那個條件該寫成loop until len(a4)=len(b4)就好了。
4.而補0的地方,如果你用do loop熟了,也可以將
If Len(a1) < Len(b1) Then Call cz(a1, b1)
If Len(a1) > Len(b1) Then Call cz(b1, a1)
改成
do while len(a1) < len(b1) : a1="0" & a1 : loop
do while len(a1) > len(b1) : b1="0" & b1 : loop
那麼也就不用呼叫副程式了。
5.減法,你用了好多不必要的東西哦。
先補0,
再判斷結果是正是負,
算小的那個數的10的補數,
將兩個數加在一起,去掉進位的那個數,
去掉前頭多餘的0
依先前判斷的正負號加上去。
function mySub(a,b)
do while len(a) len(b) : b = "0" & b : loop
if a > b then '正
ans = myADD(a, myADD(bc(b),"1"))
ans = right(ans,len(ans)-1)
'去前頭0
do while left(ans,1)="0" : ans = right(ans,len(ans)-1) :loop
elseif a< b then '負改算b-a
ans = myADD(b, myADD(bc(a),"1"))
ans = right(ans,len(ans)-1)
'去前頭0
do while left(ans,1)="0" : ans = right(ans,len(ans)-1) :loop
ans="-" & ans
else 'a=b減的結果就是0
ans="0"
end if
mySUB=ans
end function
arro好,
回覆刪除你的程式幾乎是正確的,除了你自己找出來的緣尉的錯誤的地方,你的應該也是錯的吧。你的加法,有在最後做了去「前頭0」,但是減法沒有。
我是先看了緣尉的程式後,再看你的。
你的減法,是真的用補數的減法來做的,比我的偷吃步更好些。
更正
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in1.txt" For Input As #1
Open App.Path & "\out1.txt" For Output As #2
Dim times, X As String, Y As String, c As String, Ans As String
Input #1, times
For i = 1 To times
Input #1, X
Input #1, c
Input #1, Y
If c = "+" Then Ans = B1(X, Y)
If c = "-" Then Ans = B2(X, Y)
Print #2, Ans
Next
Close #2
Close #1
End
End Sub
Function B1(a As String, b As String)
Dim s1 As Integer, s2 As Integer, tmp, plus, AANS
Do While Len(a) <> Len(b)
If Len(a) > Len(b) Then
b = "0" & b
Else
a = "0" & a
End If
Loop
For i = Len(a) To 1 Step -1
s1 = Mid(a, i, 1)
s2 = Mid(b, i, 1)
plus = ((s1 + s2 + tmp) Mod 10)
tmp = (s1 + s2 + tmp) \ 10
AANS = plus & AANS
Next
AANS = tmp & AANS
Do While Left(AANS, 1) = "0"
AANS = Right(AANS, Len(AANS) - 1)
Loop
B1 = AANS
End Function
Function B2(a As String, b As String)
Dim tmp As Integer, Newb As String, AANS As String, NEWAANS As String
Do While Len(a) <> Len(b)
If Len(a) > Len(b) Then
b = "0" & b
Else
a = "0" & a
End If
Loop
For i = Len(b) To 1 Step -1
tmp = Mid(b, i, 1)
tmp = 9 - tmp
Newb = tmp & Newb
Next
b = B1(Newb, "1")
AANS = B1(a, b)
If Len(AANS) > Len(a) Then
AANS = Right(AANS, Len(AANS) - 1)
Do While Left(AANS, 1) = "0"
AANS = Right(AANS, Len(AANS) - 1)
Loop
Else
For i = Len(AANS) To 1 Step -1
tmp = Mid(AANS, i, 1)
tmp = 9 - tmp
NEWAANS = tmp & NEWAANS
Next
Do While Left(AANS, 1) = "0"
AANS = Right(AANS, Len(AANS) - 1)
Loop
AANS = "-" & B1(NEWAANS, "1")
End If
B2 = AANS
End Function
arro,
回覆刪除ok.
繼續向前哦,有些停住了哦,你們三個都是。