編寫VB程式編寫Sub過程把任意十進位制數N分別轉換成二進位制八進位制十六進位制表示的數

2021-04-08 23:56:45 字數 9506 閱讀 4536

1樓:匿名使用者

設計介面

:**:

private sub command1_click()

dim integerpart as long, decimalpart as double

dim carry(), charactercode(15) as string

carry = array(2, 8, 16)

integerpart = int(val(text1.text))

decimalpart = val(text1.text) - int(val(text1.text))

for i = lbound(charactercode) to ubound(charactercode)

select case i

case 0 to 9

charactercode(i) = chr(48 + i)

case else

charactercode(i) = chr(55 + i)

end select

next i

for i = lbound(carry) to ubound(carry)

label2(i) = transformation(carry(i), charactercode, integerpart, decimalpart)

next i

end sub

private function transformation(byval carrysystem as integer, byref charactercode() as string, byval integerpart as long, byval decimalpart as double) as string

dim r as integer, strintegerpart as string, q as integer, strdecimalpart as string

dor = integerpart mod carrysystem

integerpart = integerpart \ carrysystem

strintegerpart = charactercode(r) + strintegerpart

loop until integerpart = 0

doq = int(decimalpart * carrysystem)

decimalpart = decimalpart * carrysystem - q

strdecimalpart = strdecimalpart + charactercode(q)

loop until decimalpart = 0

transformation = strintegerpart + "." + strdecimalpart

end function

執行介面:

用vb編寫程式,把任意一個十進位制n轉換成分別用二進位制,八進位制和十六進位制的數(用sub實現數值轉換)

2樓:fm網路

' 用途:將十進位制轉化為二 進 制

' 輸入:dec(十進位制數)

' 輸入資料型別:long

' 輸出:dec_to_bin(二 進 制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647,輸出最大數為1111111111111111111111111111111(31個1)

public function dec_to_bin(byval dec as long) as string

dec_to_bin = ""

do while dec > 0

dec_to_bin = dec mod 2 & dec_to_bin

dec = dec \ 2

loop

end function

' 用途:將二 進 制轉化為十進位制

' 輸入:bin(二 進 制數)

' 輸入資料型別:string

' 輸出:bin_to_dec(十進位制數)

' 輸出資料型別:long

' 輸入的最大數為1111111111111111111111111111111(31個1),輸出最大數為2147483647

public function bin_to_dec(byval bin as string) as long

dim i as long

for i = 1 to len(bin)

bin_to_dec = bin_to_dec * 2 + val(mid(bin, i, 1))

next i

end function

' 用途:將十六進位制轉化為二 進 制

' 輸入:hex(十六進位制數)

' 輸入資料型別:string

' 輸出:hex_to_bin(二 進 制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647個字元

public function hex_to_bin(byval hex as string) as string

dim i as long

dim b as string

hex = ucase(hex)

for i = 1 to len(hex)

select case mid(hex, i, 1)

case "0": b = b & "0000"

case "1": b = b & "0001"

case "2": b = b & "0010"

case "3": b = b & "0011"

case "4": b = b & "0100"

case "5": b = b & "0101"

case "6": b = b & "0110"

case "7": b = b & "0111"

case "8": b = b & "1000"

case "9": b = b & "1001"

case "a": b = b & "1010"

case "b": b = b & "1011"

case "c": b = b & "1100"

case "d": b = b & "1101"

case "e": b = b & "1110"

case "f": b = b & "1111"

end select

next i

while left(b, 1) = "0"

b = right(b, len(b) - 1)

wend

hex_to_bin = b

end function

' 用途:將二 進 制轉化為十六進位制

' 輸入:bin(二 進 制數)

' 輸入資料型別:string

' 輸出:bin_to_hex(十六進位制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647個字元

public function bin_to_hex(byval bin as string) as string

dim i as long

dim h as string

if len(bin) mod 4 <> 0 then

bin = string(4 - len(bin) mod 4, "0") & bin

end if

for i = 1 to len(bin) step 4

select case mid(bin, i, 4)

case "0000": h = h & "0"

case "0001": h = h & "1"

case "0010": h = h & "2"

case "0011": h = h & "3"

case "0100": h = h & "4"

case "0101": h = h & "5"

case "0110": h = h & "6"

case "0111": h = h & "7"

case "1000": h = h & "8"

case "1001": h = h & "9"

case "1010": h = h & "a"

case "1011": h = h & "b"

case "1100": h = h & "c"

case "1101": h = h & "d"

case "1110": h = h & "e"

case "1111": h = h & "f"

end select

next i

while left(h, 1) = "0"

h = right(h, len(h) - 1)

wend

bin_to_hex = h

end function

' 用途:將十六進位制轉化為十進位制

' 輸入:hex(十六進位制數)

' 輸入資料型別:string

' 輸出:hex_to_dec(十進位制數)

' 輸出資料型別:long

' 輸入的最大數為7fffffff,輸出的最大數為2147483647

public function hex_to_dec(byval hex as string) as long

dim i as long

dim b as long

hex = ucase(hex)

for i = 1 to len(hex)

select case mid(hex, len(hex) - i + 1, 1)

case "0": b = b + 16 ^ (i - 1) * 0

case "1": b = b + 16 ^ (i - 1) * 1

case "2": b = b + 16 ^ (i - 1) * 2

case "3": b = b + 16 ^ (i - 1) * 3

case "4": b = b + 16 ^ (i - 1) * 4

case "5": b = b + 16 ^ (i - 1) * 5

case "6": b = b + 16 ^ (i - 1) * 6

case "7": b = b + 16 ^ (i - 1) * 7

case "8": b = b + 16 ^ (i - 1) * 8

case "9": b = b + 16 ^ (i - 1) * 9

case "a": b = b + 16 ^ (i - 1) * 10

case "b": b = b + 16 ^ (i - 1) * 11

case "c": b = b + 16 ^ (i - 1) * 12

case "d": b = b + 16 ^ (i - 1) * 13

case "e": b = b + 16 ^ (i - 1) * 14

case "f": b = b + 16 ^ (i - 1) * 15

end select

next i

hex_to_dec = b

end function

' 用途:將十進位制轉化為十六進位制

' 輸入:dec(十進位制數)

' 輸入資料型別:long

' 輸出:dec_to_hex(十六進位制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647,輸出最大數為7fffffff

public function dec_to_hex(dec as long) as string

dim a as string

dec_to_hex = ""

do while dec > 0

a = cstr(dec mod 16)

select case a

case "10": a = "a"

case "11": a = "b"

case "12": a = "c"

case "13": a = "d"

case "14": a = "e"

case "15": a = "f"

end select

dec_to_hex = a & dec_to_hex

dec = dec \ 16

loop

end function

' 用途:將十進位制轉化為八進位制

' 輸入:dec(十進位制數)

' 輸入資料型別:long

' 輸出:dec_to_oct(八進位制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647,輸出最大數為17777777777

public function dec_to_oct(byval dec as long) as string

dec_to_oct = ""

do while dec > 0

dec_to_oct = dec mod 8 & dec_to_oct

dec = dec \ 8

loop

end function

' 用途:將八進位制轉化為十進位制

' 輸入:oct(八進位制數)

' 輸入資料型別:string

' 輸出:oct_to_dec(十進位制數)

' 輸出資料型別:long

' 輸入的最大數為17777777777,輸出的最大數為2147483647

public function oct_to_dec(byval oct as string) as long

dim i as long

dim b as long

for i = 1 to len(oct)

select case mid(oct, len(oct) - i + 1, 1)

case "0": b = b + 8 ^ (i - 1) * 0

case "1": b = b + 8 ^ (i - 1) * 1

case "2": b = b + 8 ^ (i - 1) * 2

case "3": b = b + 8 ^ (i - 1) * 3

case "4": b = b + 8 ^ (i - 1) * 4

case "5": b = b + 8 ^ (i - 1) * 5

case "6": b = b + 8 ^ (i - 1) * 6

case "7": b = b + 8 ^ (i - 1) * 7

end select

next i

oct_to_dec = b

end function

' 用途:將二 進 制轉化為八進位制

' 輸入:bin(二 進 制數)

' 輸入資料型別:string

' 輸出:bin_to_oct(八進位制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647個字元

public function bin_to_oct(byval bin as string) as string

dim i as long

dim h as string

if len(bin) mod 3 <> 0 then

bin = string(3 - len(bin) mod 3, "0") & bin

end if

for i = 1 to len(bin) step 3

select case mid(bin, i, 3)

case "000": h = h & "0"

case "001": h = h & "1"

case "010": h = h & "2"

case "011": h = h & "3"

case "100": h = h & "4"

case "101": h = h & "5"

case "110": h = h & "6"

case "111": h = h & "7"

end select

next i

while left(h, 1) = "0"

h = right(h, len(h) - 1)

wend

bin_to_oct = h

end function

' 用途:將八進位制轉化為二 進 制

' 輸入:oct(八進位制數)

' 輸入資料型別:string

' 輸出:oct_to_bin(二 進 制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647個字元

public function oct_to_bin(byval oct as string) as string

dim i as long

dim b as string

for i = 1 to len(oct)

select case mid(oct, i, 1)

case "0": b = b & "000"

case "1": b = b & "001"

case "2": b = b & "010"

case "3": b = b & "011"

case "4": b = b & "100"

case "5": b = b & "101"

case "6": b = b & "110"

case "7": b = b & "111"

end select

next i

while left(b, 1) = "0"

b = right(b, len(b) - 1)

wend

oct_to_bin = b

end function

' 用途:將八進位制轉化為十六進位制

' 輸入:oct(八進位制數)

' 輸入資料型別:string

' 輸出:oct_to_hex(十六進位制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647個字元

public function oct_to_hex(byval oct as string) as string

dim bin as string

bin = oct_to_bin(oct)

oct_to_hex = bin_to_hex(bin)

end function

' 用途:將十六進位制轉化為八進位制

' 輸入:hex(十六進位制數)

' 輸入資料型別:string

' 輸出:hex_to_oct(八進位制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647個字元

public function hex_to_oct(byval hex as string) as string

dim bin as string

hex = ucase(hex)

bin = hex_to_bin(hex)

hex_to_oct = bin_to_oct(bin)

end function

vb編寫程式計算分段函式,VB編寫程式,計算分段函式

在窗體上放一個命令按鈕,將 複製到窗體裡,程式執行單擊一次命令按鈕可輸入一次n的值 如下 private sub command1 click dim n,y as single n inputbox 請輸入n的值 select case n case is 0 y 2 n 5 case 0 y 0...

編寫vb程式,求1編寫一個vb程式,求11213141n的值。測試資料n50答案

private sub mand1 click print 第3題 dim n as integer,s as doublen inputbox n 50 for i 1 to n s s 1 i next i print sum n format s,0.00 end sub private su...

編寫VB程式,求Sn a aa aaan個a 的值,其中n是表示a的個數,a是

dim sn,a,n,i,m m 0sn 0 a int rnd 10 n text1.text for i 1 to n m 10 i 1 a m sn sn m next i 應該就是這樣了,有點久沒用vb,有點忘了 private sub form click dim sn a int rnd...