主题:高手来,用QBASIC编个进制转换程序
xuyifan
[专家分:40] 发布于 2008-02-01 21:40:00
高手来,用QBASIC编个进制转换程序
用BASIC,不要软件,要程序,能相互转换任意两进制。
这个难了吧,请给出过程和思路
回复列表 (共8个回复)
沙发
强强 [专家分:4740] 发布于 2008-02-01 21:43:00
回复错了,辗转相除是求最大公约数的,报歉
要转成几进制就不断用那个数除以几,取其余数,直到商为0
板凳
xuyifan [专家分:40] 发布于 2008-02-01 21:49:00
怎么用呢?
3 楼
Mato完整版 [专家分:1270] 发布于 2008-02-02 22:00:00
方法
N进制数转为P进制数分3步:
(1)输入并检测数据。
可以建立一个循环,循环里做3件事:
一、把N进制数M$的每一位分解开。
二、对于一个数字,判断它符不符合要求(在N进制数可以使用的数字、字母范围内)。
三、如果符合要求,就把它转换为10进制数字,存放到数组里。
在这个数组A(I)中,0以上存放整数部分,0和0以下存放小数部分。
(2)把N进制数转换成10进制数。
使用按位展开求和法,即把数组里每一个元素I(元素1为最低位)乘以N的I-1次方,再相加。
(3)把10进制数转换成P进制数。
分别使用“除以P逆向取余”和“乘P取整”。这两种方法应该都知道了,就不讲了。
4 楼
Mato完整版 [专家分:1270] 发布于 2008-02-02 22:08:00
我试用版的里面有:
[url=http://www.programfan.com/club/post-230167.html]这张帖子[/url]的3楼。
为了防止输入某些数(如2.6)出现死循环,把这个程序段:
xs = t - INT(t)
DO UNTIL xs = 0
b$ = b$ + LTRIM$(STR$(INT(xs * p)))
xs = xs * p
xs = xs - INT(xs)
LOOP
改为精度30的:
xs = t - INT(t): iijj = 0
DO UNTIL xs = 0 OR iijj = 30
b$ = b$ + LTRIM$(STR$(INT(xs * p)))
xs = xs * p
xs = xs - INT(xs)
iijj = iijj + 1
LOOP
计数器iijj的作用是存放这里DO--LOOP循环的次数以控制精度。
5 楼
我爱QB [专家分:200] 发布于 2008-02-05 23:36:00
[em2]这个程序太差请见谅!
10进制转换成2进制数:
INPUT "n(n为整数部分例:12)、m(m为小数部分例:0.75,当m为0时请输入0否则无法完成转换)(m、n为正数)="; n, m
s = 1
DO
x = n MOD 2
s = s * 10 + x
n = n \ 2
LOOP UNTIL n = 0
s$ = STR$(s)
z = LEN(s$)
FOR i = 1 TO INT(z / 2)
a$ = MID$(s$, i, 1)
b$ = MID$(s$, z - i + 1, 1)
MID$(s$, i, 1) = b$
MID$(s$, z - i + 1, 1) = a$
NEXT i
q = LEN(s$) - 2
s$ = LEFT$(s$, q)
PRINT s$;
PRINT ".";
IF m = 0 THEN GOTO 20
DO
10 m = 2 * m
IF m - 1 < 0 THEN
PRINT "0";
GOTO 10
ELSEIF m - 1 = 0 THEN
PRINT "1";
m = 0
GOTO 20
ELSEIF m - 1 > 0 THEN
PRINT "1";
m = m - 1
GOTO 10
END IF
20 LOOP UNTIL m = 0
END
6 楼
强强 [专家分:4740] 发布于 2008-02-09 20:50:00
以十进制作为中介进行转换
7 楼
冷石_jasv [专家分:1570] 发布于 2008-02-14 09:52:00
我用的是QB4.5自带的文件夹里的一个演示程序。
我把他当专业库用了。
发上来给你瞧瞧。。
咱也转转贴
' ************************************************
' ** Name: BITS **
' ** Type: Toolbox **
' ** Module: BITS.BAS **
' ** Language: Microsoft QuickBASIC 4.00 **
' ************************************************
'
' Demonstrates the bit manipulation functions
' and subprograms.
'
' USAGE: No command line parameters
' .MAK FILE: (none)
' PARAMETERS: (none)
' VARIABLES: max% Upper limit for the prime number generator
' b$ Bit string for finding prime numbers
' n% Loop index for sieve of Eratosthenes
' bit% Bit retrieved from b$
' i% Bit loop index
' q$ The double quote character
' Functions
DECLARE FUNCTION BinStr2Bin% (b$)
DECLARE FUNCTION Bin2BinStr$ (b%)
' Subprograms
DECLARE SUB BitGet (a$, bitIndex%, bit%)
DECLARE SUB BitPut (b$, bitIndex%, bit%)
' Prime numbers less than max%, using bit fields in B$
CLS
max% = 1000
PRINT "Primes up to"; max%; "using BitGet and BitPut for sieve..."
PRINT
PRINT 1; 2;
b$ = STRING$(max% \ 8 + 1, 0)
FOR n% = 3 TO max% STEP 2
BitGet b$, n%, bit%
IF bit% = 0 THEN
PRINT n%;
FOR i% = 3 * n% TO max% STEP n% + n%
BitPut b$, i%, 1
NEXT i%
END IF
NEXT n%
PRINT
' Demonstration of the Bin2BinStr$ function
PRINT
PRINT "Bin2BinStr$(12345) = "; Bin2BinStr$(12345)
' Demonstration of the BinStr2Bin% function
PRINT
q$ = CHR$(34)
PRINT "BinStr2Bin%("; q$; "1001011"; q$; ") = ";
PRINT BinStr2Bin%("1001011")
' That's all
END
' ************************************************
' ** Name: Bin2BinStr$ **
' ** Type: Function **
' ** Module: BITS.BAS **
' ** Language: Microsoft QuickBASIC 4.00 **
' ************************************************
'
' Returns a string of sixteen "0" and "1" characters
' that represent the binary value of b%.
'
' EXAMPLE OF USE: PRINT Bin2BinStr$(b%)
' PARAMETERS: b% Integer number
' VARIABLES: t$ MAPing string space for forming binary string
' b% Integer number
' mask% Bit isolation mask
' i% Looping index
' MODULE LEVEL
' DECLARATIONS: DECLARE FUNCTION Bin2BinStr$ (b%)
'
FUNCTION Bin2BinStr$ (b%) STATIC
t$ = STRING$(16, "0")
IF b% THEN
IF b% < 0 THEN
MID$(t$, 1, 1) = "1"
END IF
mask% = &H4000
FOR i% = 2 TO 16
IF b% AND mask% THEN
MID$(t$, i%, 1) = "1"
END IF
mask% = mask% \ 2
NEXT i%
END IF
Bin2BinStr$ = t$
END FUNCTION
' ************************************************
' ** Name: BinStr2Bin% **
' ** Type: Function **
' ** Module: BITS.BAS **
' ** Language: Microsoft QuickBASIC 4.00 **
' ************************************************
'
' Returns the integer represented by a string of up
' to 16 "0" and "1" characters.
'
' EXAMPLE OF USE: PRINT BinStr2Bin%(b$)
' PARAMETERS: b$ Binary representation string
' VARIABLES: bin% MAPing variable for finding value
' t$ MAPing copy of b$
' mask% Bit mask for forming value
' i% Looping index
' MODULE LEVEL
' DECLARATIONS: DECLARE FUNCTION BinStr2Bin% (b$)
'
FUNCTION BinStr2Bin% (b$) STATIC
bin% = 0
t$ = RIGHT$(STRING$(16, "0") + b$, 16)
IF LEFT$(t$, 1) = "1" THEN
bin% = &H8000
END IF
mask% = &H4000
FOR i% = 2 TO 16
IF MID$(t$, i%, 1) = "1" THEN
bin% = bin% OR mask%
END IF
mask% = mask% \ 2
NEXT i%
BinStr2Bin% = bin%
END FUNCTION
' ************************************************
' ** Name: BitGet **
' ** Type: Subprogram **
' ** Module: BITS.BAS **
' ** Language: Microsoft QuickBASIC 4.00 **
' ************************************************
'
' Extracts the bit at bitIndex% into a$ and returns
' either 0 or 1 in bit%. The value of bitIndex%
' can range from 1 to 8 * LEN(a$).
'
' EXAMPLE OF USE: BitGet a$, bitIndex%, bit%
' PARAMETERS: a$ String where bit is stored
' bitIndex% Bit position in string
' bit% Extracted bit value, 0 or 1
' VARIABLES: byte% Byte location in string of the bit
' mask% Bit isolation mask for given bit
' MODULE LEVEL
' DECLARATIONS: DECLARE SUB BitGet (a$, bitIndex%, bit%)
'
SUB BitGet (a$, bitIndex%, bit%) STATIC
byte% = (bitIndex% - 1) \ 8 + 1
SELECT CASE bitIndex% MOD 8
CASE 1
mask% = 128
CASE 2
mask% = 64
CASE 3
mask% = 32
CASE 4
mask% = 16
CASE 5
mask% = 8
CASE 6
mask% = 4
CASE 7
mask% = 2
CASE 0
mask% = 1
END SELECT
IF ASC(MID$(a$, byte%, 1)) AND mask% THEN
bit% = 1
ELSE
bit% = 0
END IF
END SUB
' ************************************************
' ** Name: BitPut **
' ** Type: Subprogram **
' ** Module: BITS.BAS **
' ** Language: Microsoft QuickBASIC 4.00 **
' ************************************************
'
' If bit% is non-zero, then the bit at bitIndex% into
' a$ is set to 1; otherwise, it's set to 0. The value
' of bitIndex% can range from 1 to 8 * LEN(a$).
'
' EXAMPLE OF USE: BitPut a$, bitIndex%, bit%
' PARAMETERS: a$ String containing the bits
' bitIndex% Index to the bit of concern
' bit% Value of bit (1 to set, 0 to clear)
' VARIABLES: bytePtr% Pointer to the byte position in the string
' mask% Bit isolation mask
' byteNow% Current numeric value of string byte
' MODULE LEVEL
' DECLARATIONS: DECLARE SUB BitPut (b$, bitIndex%, bit%)
'
SUB BitPut (a$, bitIndex%, bit%) STATIC
bytePtr% = bitIndex% \ 8 + 1
SELECT CASE bitIndex% MOD 8
CASE 1
mask% = 128
CASE 2
mask% = 64
CASE 3
mask% = 32
CASE 4
mask% = 16
CASE 5
mask% = 8
CASE 6
mask% = 4
CASE 7
mask% = 2
CASE 0
mask% = 1
bytePtr% = bytePtr% - 1
END SELECT
byteNow% = ASC(MID$(a$, bytePtr%, 1))
IF byteNow% AND mask% THEN
IF bit% = 0 THEN
MID$(a$, bytePtr%, 1) = CHR$(byteNow% XOR mask%)
END IF
ELSE
IF bit% THEN
MID$(a$, bytePtr%, 1) = CHR$(byteNow% XOR mask%)
END IF
END IF
END SUB
8 楼
强强 [专家分:4740] 发布于 2008-02-14 11:12:00
不错
我来回复