《VB选考高精计算专题练习.doc》由会员分享,可在线阅读,更多相关《VB选考高精计算专题练习.doc(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、VB选考高精计算专题练习学号 姓名 班级 第1题VB程序中,长整型变量存储的数值位数最多10位,而且最高位不大于2,要处理位数更多的整数需要通过设计高精度算法解决。小利实现高精度加法运算,基本思想是用字符串类型变量存储数据,然后按顺序处理到中,再模拟加法计算过程。在文本框text1和text2中输入两个数,单击“加法”,在label1上显示计算结果。程序运行后如图所示,请在划线处填写合适的代码。Dim a(0 To 100) As Integer, b(0 To 100) As Integer, ans(1 To 200) As IntegerDim la As Integer, lb As
2、Integer, length As Integer, lmax As IntegerDim s As StringPrivate Sub Command1_Click()Dim a1 As String, b1 As StringDim i As Integer, j As Integer, t As Integera1 = Text1.Text: b1 = Text2.Textla = Len(a1): lb = Len(b1)For i = 1 To Len(a1) a(i) = Mid(a1, Len(a1) - i + 1, 1)Next iFor i = 1 To Len(b1)
3、b(i) = Mid(b1, Len(b1) - i + 1, 1)Next iIf la lb Then lmax = la Else For i = 1 To lmax ans(i) = ans(i)可删除 + a(i) + b(i)Next iFor i = 1 To lmax ans(i + 1) = ans(i) = Next iIf ans(i) = 1 Then s = 1 + sFor i = lmax To 1 Step -1 s = s + Str(ans(i)Next iLabel1.Caption = sEnd Sub第2题高精度减法运算:(1)请在划线处填入合适的代码
4、。Private Sub Command1_Click()Dim s1 As String, s2 As String, ans As String, fh As StringDim t As Integer, f As Boolean, tmp As String, jw As Integers1 = Text1.Text: s2 = Text2.Text: fh = If Len(s1) Len(s2)遗漏长度相等的情况处理如长度相等比较值大小后交换 Then For i = 1 To Len(s1) - Len(s2) s2 = 0 + s2 Next iElse For i = 1 T
5、o Len(s2) - Len(s1) s1 = 0 + s1 Next i tmp = s1: s1 = s2: s2 = tmp ElseIf Len(s1) Len(s2) Then For i = 1 To Len(s2) - Len(s1) s1 = 0 + s1 Next i tmp = s1: s1 = s2: s2 = tmp fh = - ElseIf Val(s1) Val(s2) Then tmp = s1: s1 = s2: s2 = tmp fh = -End Ift = 0: jw = 0: ans = For i = Len(s1) To 1 Step -1 t
6、= Val(Mid(s1, i, 1) - Val(Mid(s2, i, 1) + jw = 1 - t 10 ans = CStr(t Mod 10) & ansNext ii = 1Do While Mid(ans, i, 1) = 0 LoopLabel1.Caption = fh & Mid(ans, i)End Sub第3题高精度除法运算。请在划线处填入合适的代码。Private Sub Command1_Click()Dim x As Integer, y As Integer, n As Integer, s As StringDim k As Doublex = Val(Tex
7、t1.Text)y = Val(Text2.Text)n = Val(Text3.Text)s = s + Str(x y)s = s + .k = x Mod yi = 1Do While i = n k = k * 10 k = k Mod y i = i + 1LoopLabel3.Caption = 商为: + sEnd SubPrivate Sub Command2_Click()Dim xhj(1 To 100) As IntegerDim a As Double, b As DoubleDim n As Integer, i As IntegerDim ls As String, s As Stringa = Val(Text1.Text)b = Val(Text2.Text)m = Val(Text3.Text)ls = Str(a b) + .Do While n = m xhj(n) = a Mod b a = xhj(n) * 10 ls = ls + CStr(a b) For i = 1 To n - 1 If Then Text4.Text = 第 & CStr(i) & 位小数 & CStr(a b) & 开始重复循环 n = m + 1 End If Next iLoopLabel3.Caption = lsEnd Sub 4 / 4
限制150内