[设为首页]
中国-东莞·教育导航
  主页 | 教育资讯 | 推荐课程 | 公开课 | 考试 | 资格认证 | 外语 | 硕士考研 | 自考成考 | IT培训 | 金融财会 | 名校 | 学习资料
  导航:首页 - 计算机等级考试二级VB常用算法-4

计算机等级考试二级VB常用算法-4
作者:城市学习网 来源:xue.net 更新日期:2007-12-18 阅读次数:

1)        十进制正整数m转换为R(2-16)进制的字符串。
         思路: 将m不断除r取余数,直到商为0,将余数反序即得到结果。
         算法实现:
Private Function Tran(ByVal m As Integer, ByVal r As Integer) As String
    Dim StrDtoR As String, n As Integer
    Do While m <> o
        n = m Mod r
        m = m \ r
        If n > 9 Then
            StrDtoR = Chr(65 + n - 10) & StrDtoR
        Else
            StrDtoR = n & StrDtoR
        End If
    Loop
    Tran = StrDtoR
End Function  
2)        R(2-16)进制字符串转换为十进制正整数。
         思路:R进制数每位数字乘以权值之和即为十进制数。
         算法实现:
Private Function Tran(ByVal s As String, ByVal r As Integer) As integer
    Dim n As Integer, dec As Integer
    s = UCase(Trim(s))
    For i% = 1 To Len(s)
               If Mid(s, i, 1) >= "A" Then
                        n = Asc(Mid(s, i, 1)) - Asc("A") + 10
               Else
                        n = Val(Mid(s, i, 1))
               End If
               dec = dec + n * r ^ (Len(s) - i)
    Next i
    Tran = dec
End Function 

解题技巧
        进制转化的原理要清楚,同时编写代码时候要留意16进制中的A-F字符的处理。

 2、实战练习

1)        补充代码(2002秋二(9))
         本程序是把给定的二进制整数转换为八进制整数。
                  Private Sub Command1_Click()
                           Dim a As String, b As String, c As String
                           Dim L As Integer, m As Integer, n As Integer
                           a = InputBox("请输入一个二进制数", "输入框")
                              (1)    
                           a = String(L, "0") & a
                              (2)    
                           For m = 1 To n / 3
                             b = Mid(a, 3 * m - 2, 3)
                                 (3)     
                           Next m
                           Text1.Text = c
                  End Sub

                   Private Function zh(s As String) As String
                           Dim i As Integer, n As Integer, p As Integer
                           p = 1
                           For i = 2 To 0 Step -1
                                       (4)     
                              p = p + 1
                           Next i
                           zh = Str(n)
                  End Function
2)        补充代码(2001春二(7))
         下面程序是把给定的16进制正整数转换为10进制数。
                  Option Explicit

                   Private Sub Form_Click()
                           Dim St As Integer, Dem As Long
                           St=InputBox("输入一个十六进制数")
                           Dem=Convert(St)
                           Print St; ">="; Dem
                  End Sub

                   Private Function Convert(S As String)As Long
                           Dim N As Integer, I As Integer,Substring As String*1
                           Dim P As long, K As Long,Asc1 As Integer
                           N=  (1)  
                           P=16^N
                           For I=1 To N
                                    P=P/16
                                    Substring=  (2)  
                                    Select Case Substring
                                             Case "0" To "9"
                                                      K=K+P*Val(Substring)
                                             Case   (3)  
                                                      Asc1=Asc(Substring)-Asc("A")+10
                                                        (4)  
                                    End Select
                           Next I
                             (5)  

 


报 名 此 课 程 / 咨 询 相 关 信 息
【预约登门】 【网上咨询】 【订座试听】 【现在报名】
课程名称
计算机等级考试二级VB常用算法-4
真实姓名
* 性 别
联系电话
* E-mail:
所在地区
咨询内容

      

相关文章:

Copyright© 2014 www.dgedu.com.cn 东莞教育在线 版权所有
中国·东莞
粤ICP备06023013号