其实题目并不难,算法都知道,但真要写出实际的函数来还真需要点耐心和细心。
1、不使用数学转换,使用原始算法将一个10进制数字转换为16进制。
2、计算两个大数的乘积。
第一道不难,很快搞定
首先是转换16进制的字符:
1 '将数字转换为16进制的数字
2 Public Shared Function ConvertHexChar(ByVal number As Integer) As String
3
4 Select Case number
5 Case Is < 10
6 Return number.ToString()
7 Case 10
8 Return "A"
9 Case 11
10 Return "B"
11 Case 12
12 Return "C"
13 Case 13
14 Return "D"
15 Case 14
16 Return "E"
17 Case 15
18 Return "F"
19 End Select
20
21 End Function
2 Public Shared Function ConvertHexChar(ByVal number As Integer) As String
3
4 Select Case number
5 Case Is < 10
6 Return number.ToString()
7 Case 10
8 Return "A"
9 Case 11
10 Return "B"
11 Case 12
12 Return "C"
13 Case 13
14 Return "D"
15 Case 14
16 Return "E"
17 Case 15
18 Return "F"
19 End Select
20
21 End Function
然后用除法递规取余数,最后表现出来即可
1 Private Const normal As Integer = 10
2 Private Const divisor As Integer = 16
3
4 Public Shared Function ConvertToHex(ByVal number As Integer) As String
5
6 Dim quotient As Integer
7 Dim reminder As Integer
8 Dim resultStack As New Stack
9 Dim result As String
10
11 Do
12 quotient = number \ divisor
13 reminder = number Mod divisor
14 resultStack.Push(ConvertHexChar(reminder))
15 number = quotient
16 Loop While quotient > 0
17
18 While resultStack.Count > 0
19 result = result & (resultStack.Pop())
20 End While
21
22 Return result
23
24 End Function
2 Private Const divisor As Integer = 16
3
4 Public Shared Function ConvertToHex(ByVal number As Integer) As String
5
6 Dim quotient As Integer
7 Dim reminder As Integer
8 Dim resultStack As New Stack
9 Dim result As String
10
11 Do
12 quotient = number \ divisor
13 reminder = number Mod divisor
14 resultStack.Push(ConvertHexChar(reminder))
15 number = quotient
16 Loop While quotient > 0
17
18 While resultStack.Count > 0
19 result = result & (resultStack.Pop())
20 End While
21
22 Return result
23
24 End Function
第二题使用字符数组存储,使用加法计算是必然的。但是,我查了好多资料都是说直接以一个数按位数与另一个数相乘,这点上偶不敢苟同。按我的想法是首先必须写两个大数的加法,然后按位数的乘法转换为这种加法来计算。当然,我不知道能不能使用到位运算符,要是能用到效率将更高。
第二题暂没有测试好的程序,以后贴出。