浮点型十进制与浮点型二进制的转换(小白版)

时间:2022-01-11 19:13:14

大佬们看一下那有不足,皮皮在此谢过了。

def  bTod (a,p=4):
    '''
    bTod(...)
    bTod(string,integer)
    input: String only contains numeric characters  or  points or minus  sign, the string represents  a binary  number.And the integer is used  to determine the accuracy  of  the  output  data.
    process: Converts  the  binary number that this string  represents  to  a  decimal  number .
    output: Print  the  result(decimal  number) that determines  the  accuracy.
    '''
    p=int(p)
    sign=''
    if a[0]=='-':
        sign=a[0]
        a=a[1:]
    ithpower=0
    intsum=0
    floatsum=0
    for c in a:
        if c not in ('1','0','.'):
            print("输入的数值不是二进制数")
            return 0
    if  a.count(".")>1:
        print("输入错误")
        return 0   
    elif a.count(".")==1:
        cut=a.split(".")
        pointbefore=cut[0]
        pointbefore=pointbefore[::-1]
        pointafter=cut[1]
    else :
        pointbefore=a[::-1]
        pointafter="0"
    for c in pointbefore:
        intsum=intsum+ int(c)*(2**ithpower)
        ithpower=ithpower+1
    ithpower=-1    
    for  c  in  pointafter:
        floatsum=floatsum+int(c)*(2**ithpower)
        ithpower=ithpower-1
    result=intsum+floatsum
    print("二进制数{}转变成十进制数保留{}位小数的结果是{}{:.{}f}" .format(a,p,sign,result,p))
def  dTob(a,p=4):
    '''
    bTod(...)
    bTod(string,integer)
    input: String only contains numeric characters  or  points  or  minus  sign, the string represents  a decimal  number.And the integer is used  to determine the accuracy  of  the  output  data.
    process: Converts  the decimal number that this string  represents  to  a binary  number .
    output: Print  the  result(binary  number) that determines  the  accuracy.
    '''
    p=int(p)
    times=0 
    sign=''
    if a[0]=='-':
        sign=a[0]
        a=a[1:]
    for c in  a:
        if  (c<'0' or c>'9') and c!='.':
            print("输入错误")
            return  0
    if  a.count(".")>1:
        print("输入错误")
        return  0
    elif a.count('.')==1:
        cut=a.split(".")
        pointafter=float("0."+cut[1])
        auto=bin(int(cut[0]))
        intsum=auto[2:]
        floatsum=''
        times=0
        while pointafter :
            t=pointafter*2
            if t<1:
                floatsum=floatsum+'0'
                pointafter=pointafter*2
                times=times+1
            else:
                floatsum=floatsum+'1'
                pointafter=t-1
                times=times+1
            if  times >500:
                print("转化后的小数为无限小数")
                break
        if len(floatsum)>p:
            floatsum=floatsum[:p]
        elif len(floatsum)<p:
            floatsum=floatsum+'0'*(p-len(floatsum))
        else:
            floatsum=floatsum
        result=intsum+'.'+floatsum      
        print("十进制数{}转化为二进制数保留{}位小数的结果是{}{}" .format(a,p,sign,result))    
    else :
        p=int(p)
        auto=bin(int(a))
        result=auto[2:]+'.'+'0'*p      
        print("十进制数{}转化为二进制数保留{}位小数的结果是{}{}" .format(a,p,sign,result))
                
print("请在输入数字前加上数字类型(十进制为d、二进制为b,默认为十进制转二进制,默认精度为小数点后四位),q退出程序")
while 1:
    n=input("请输入要转换的内容:")
    if n=='q':
        print("程序退出")
        break
    else:
        pole=1
        while  pole :
            s=input("请输入精度:")
            if s=='':
                s=4
                break
            for  c in s:
                if '0'<=c<='9':
                    pole=0
                else:
                    print("精度输入错误")
                    pole=1
                    break     
        if  n=='':
            print("输入错误")
        elif n[0]=='d':
            dTob(n[1:],s)
        elif '0' <=n[0]<='9'  or  n[0]=='-':
            dTob(n,s)
        elif n[0]=='b':
            bTod(n[1:],s)
        else:
            print("输入错误")