如何创建一个模拟将小数转换为另一个基数的长除法的函数 [英] How do I create a function that simulates the long division method of converting a decimal to another base

查看:88
本文介绍了如何创建一个模拟将小数转换为另一个基数的长除法的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序的运行应该提示用户输入一个数字,数字的基数,以及要转换为数字的基数,然后打印将数字转换为十进制的结果,然后将小数转换为输出基础。如果要转换的数字是Hex 23B4到Octal,这是一个输出示例:



A run of the program should prompt the user to enter a number, the base of the number, and the base to convert to the number to, and then print the results of converting the number to decimal, and then the decimal to the output base. Here’s an example of output if the number to be converted is Hex 23B4 to Octal:

Enter a number: 23b4
Enter base from: 16
Enter base to convert to: 8
4 x 16 ^ 0 = 4 x 1 = 4
B x 16 ^ 1 = 11 x 16 = 176
3 x 16 ^ 2 = 3 x 256 = 768
2 x 16 ^ 3 = 2 x 4096 = 8192
23b4 base 16 is decimal 9140

9140 / 8 = 1142 , 4
1142 / 8 = 142 , 6
142 / 8 = 17 , 6
17 / 8 = 2 , 1
2 / 8 = 0 , 2
decimal 9140 is 21664 in base 8

So a general process flow is:
•	Ask for number to convert
•	Ask for base of number
•	Ask for base to convert to
•	Set decimal to return of "toDecimal" passing the number-in and base-in
•	Print message number-in base base-in is decimal decimal-value 
•	Set number-out to return of "fromDecimal" passing decimal and base-out
•	Print message decimal is number-out in base base-out

 Outline of the fromDecimal function:
•	Define a string number-out and initialize it to an empty string, i.e. ‘’ or "" (this will allow us to concatenate digits to it in a loop)
•	Set a quotient variable to the decimal passed in
•	Create while loop that checks for quotient great than zero
•	Set a remainder variable to quotient mod base-out
•	Set next-digit to return of valueToDigit passing in the remainder
•	Set a new-quotient variable to quotient integer-division base-out
•	Print this loop as: quotient / base-out = new-quotient , next-digit
•	Append next digit to front of number-out (e.g. sNumberOut = nextDigit + sNumberOut)
•	Set quotient to new-quotient
•	(Loop will continue till quotient is zero)
•	Return number-out





我尝试过:





What I have tried:

def digitToValue(digit, base):
    value = 0
    if digit >= '0' and digit <= '9':
        value = int(digit)
    elif digit >= 'A' and digit <= 'F':
        
        if digit == 'A': 
            value = 10
            
        elif digit == 'B': 
            value = 11
            
        elif digit == 'C': 
            value = 12
            
        elif digit == 'D': 
            value = 13
            
        elif digit == 'E': 
            value = 14
            
        else:
            value = 15 
    else:
        print("Invalid digit!")
        return -1
    
    if value < 0 or value >= base:
        print("Invalid digit for base!")
        return -1

    return value

def toDecimal (sNumber, nBase):
    decimal = 0
    
    for i in range(len(sNumber)):

        digitPos = len(sNumber) - 1 - i

        digit = sNumber[digitPos]

        digitVal = digitToValue(digit, nBase)

        if (digitVal == -1):
            return -1

        posVal = nBase ** i

        product = digitVal * posVal

        print(digit, "x", nBase, "^", i, "=", digitVal, "x", posVal, "=", product)

        decimal += product

        return decimal

    
    
numberIn = input("Enter a number: ")
baseIn = input("Enter base from: ")
convertTo = input("Enter base to convert to: ")
decimalOut = toDecimal(numberIn, baseIn)
print(decimalOut)

推荐答案

以下代码修复了您的错误。现在你必须完成它。

The following code fixes the bugs your one has. Now you have to complete it.
#  ...
def toDecimal (sNumber, nBase):
    decimal = 0

    for i in range(len(sNumber)):

        digitPos = len(sNumber) - 1 - i

        digit = sNumber[digitPos]

        digitVal = digitToValue(digit, nBase)


        if (digitVal == -1):
            return -1

        posVal = nBase ** i

        product = digitVal * posVal

        print(digit, "x", nBase, "^", i, "=", digitVal, "x", posVal, "=", product)

        decimal += product

    return decimal #  your return statement had wrong indentation



numberIn = input("Enter a number: ")
baseIn = input("Enter base from: ")
convertTo = input("Enter base to convert to: ")
decimalOut = toDecimal(str(numberIn), baseIn) # added the call to str function
print(decimalOut)


def toDecimal (sNumber,nBase):

decimal = 0



for i in range(len(sNumber)):



digitPos = len(sNumber) - 1 - i



digit = sNumber [digitPos]



digitVal = digitToValue(数字,nBase)



如果(digitVal == -1):

返回-1



posVal = nBase ** i



product = digitVal * posVal



打印(数字,x,nBase,^,i,=,digitVal,x,posVal,=,产品)



十进制+ =产品



返回小数



< br $>


numberIn =输入(输入数字:)

baseIn = int(输入(输入基数来自:)) />
convertTo = int(输入(E nter base转换为:))

decimalOut = toDecimal(numberIn,baseIn)

print(numberIn,base,baseIn,is decimal,decimalOut)

fromDecimal =

numberOut = fromDecimal(decimal,baseOut)

print(decimal,decimal,is,numberOut, in base,baseOut)
def toDecimal (sNumber, nBase):
decimal = 0

for i in range(len(sNumber)):

digitPos = len(sNumber) - 1 - i

digit = sNumber[digitPos]

digitVal = digitToValue(digit, nBase)

if (digitVal == -1):
return -1

posVal = nBase ** i

product = digitVal * posVal

print(digit, "x", nBase, "^", i, "=", digitVal, "x", posVal, "=", product)

decimal += product

return decimal



numberIn = input("Enter a number: ")
baseIn = int(input("Enter base from: "))
convertTo = int(input("Enter base to convert to: "))
decimalOut = toDecimal(numberIn, baseIn)
print(numberIn, " base " , baseIn, " is decimal ", decimalOut)
fromDecimal = " "
numberOut = fromDecimal(decimal, baseOut)
print("decimal ", decimal, " is " , numberOut , " in base ", baseOut)


这篇关于如何创建一个模拟将小数转换为另一个基数的长除法的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆