簡體   English   中英

如何將 function 從 C++ 重寫為 Python 將數字從任何數字系統轉換為十進制系統

[英]How to rewrite a function from C++ to Python that converts a number from any number system to the decimal system

我有個問題。 我在 cpp 中有一個 function 可以將數字從任何數字系統轉換為十進制。 但我需要一個像 Python 這樣的 function。 我只是不知道如何重寫它以在 Python 中工作。 而且我不能使用給定語言中存在的內置函數來替換數字。

這是 cpp 中的 function,我需要將其轉換為 Python

int from_any_to_10(string number, int system)
{
    int x;
    int p = 1;
    int result = 0;
    for (int k = number.length() - 1; k >= 0; k--)
    {
        if (number[k] <= '9' && number[k] >= '0')
        {
            x = number[k] - 48;
        }
        else 
        {
            x = number[k] - 55;
        }
        
        result = result + p * x;
        p = p * system;
    }
    return result;
}

我在 Python 中的代碼如下所示:

def from_any_to_10(number, system):
    x = int()
    p = 1
    result = 0

    for k in range(len(number) - 1, len(number) >= 0, -1):
        if number[k] <= '9' and number[k] >= '0':
            x = number[k] - 48
        else:
            x = number[k] - 55

        result = result + p * x
        p = p * system
    return result

但是,當我在 Python 中編譯代碼時,會出現以下錯誤:

File "C:\Users\LAPTOP\Desktop\Python\_INFA\main.py", line 17, in from_any_to_10
    x = number[k] - 48
TypeError: unsupported operand type(s) for -: 'str' and 'int'

我不知道如何重寫它以在 Python 中工作。 對不起,我的英語不好。

為了將數字轉換為十進制數系統。 我們可以從末尾迭代數字並取每個數字,然后將數字與數字系統相乘到最后一個數字的位置的冪,然后將它們全部相加。

轉換數制

C++

#include <iostream>
#include <algorithm>
#include <string>
#include <functional>
#include <cctype>

using namespace std;
//converts any number system to decimal number system
int from_any_to_10(string number, int system){
    //converting string to uppercase
    std::transform(number.begin(), number.end(), number.begin(), std::ptr_fun<int, int>(std::toupper));
    int x;
    int p=1;//stores power value
    int result=0;
    //iterating over numbers in reverse order
    for(int k=number.length()-1; k>=0; k--){
        if(number[k]<='9' && number[k] >='0')
            x=number[k]-48;
        else x=number[k]-55;
        result+=p*x;
        p*=system;
    }
    return result;
}
int main(){
    cout<<from_any_to_10("125a",16)<<endl;
    cout<<from_any_to_10("1011",2)<<endl;
    cout<<from_any_to_10("11111",2)<<endl;
    cout<<from_any_to_10("A1",16)<<endl;
    cout<<from_any_to_10("a1",16)<<endl;
    return 0;
}

Output:

$ g++ number_system.cpp && ./a.out 
4698
11
31
161
161

Python3

#converts from any number system to decimal number system
def from_any_to_10(number,system:int):
    number=number[::-1].upper()#reversing the string and converting to uppercase
    p=1
    result=0;
    for c in number:#iterating over the numbers
        if c <='9' and c>='0':
            #ord() converts a character to an integer
            x=ord(c)-48
        else:
            x=ord(c)-55
        result+=p*x
        p*=system
    return result;

if __name__=='__main__':
    print(from_any_to_10("125a",16))
    print(from_any_to_10("1011",2))
    print(from_any_to_10("11111",2))
    print(from_any_to_10("A1",16))
    print(from_any_to_10("a1",16))

OUTPUT:

$ python3 number_system.py 
4698
11
31
161
161

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM