📜  程序将BCD号码转换为十进制数

📅  最后修改于: 2021-05-06 22:46:33             🧑  作者: Mango

给定一个BCD(二进制编码的十进制)编号,任务是将BCD编号转换为等效的十进制编号
例子:

方法:

  1. 遍历给定BCD编号中的所有位。
  2. 将给定的BCD数分成4个块,然后开始计算其等效的十进制数
  3. 将此数字存储在名为sum的变量中。
  4. 从求和存储在变量num中的数字开始构造一个数字。
  5. 反转到目前为止形成的数字,然后返回该数字。

下面是上述方法的实现。

C++
// C++ code to convert BCD to its
// decimal number(base 10).
 
// Including Header Files
#include 
using namespace std;
 
// Function to convert BCD to Decimal
int bcdToDecimal(string s)
{
    int len = s.length(),
        check = 0, check0 = 0;
    int num = 0, sum = 0,
        mul = 1, rev = 0;
 
    // Iterating through the bits backwards
    for (int i = len - 1; i >= 0; i--) {
 
        // Forming the equivalent
        // digit(0 to 9)
        // from the group of 4.
        sum += (s[i] - '0') * mul;
        mul *= 2;
        check++;
 
        // Reinitialize all variables
        // and compute the number.
        if (check == 4 || i == 0) {
            if (sum == 0 && check0 == 0) {
                num = 1;
                check0 = 1;
            }
            else {
                // update the answer
                num = num * 10 + sum;
            }
 
            check = 0;
            sum = 0;
            mul = 1;
        }
    }
 
    // Reverse the number formed.
    while (num > 0) {
        rev = rev * 10 + (num % 10);
        num /= 10;
    }
 
    if (check0 == 1)
        return rev - 1;
 
    return rev;
}
 
// Driver Code
int main()
{
    string s = "100000101000";
 
    // Function Call
    cout << bcdToDecimal(s);
 
    return 0;
}


Java
// Java code to convert BCD to its
// decimal number(base 10).
// Including Header Files
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to convert BCD to Decimal
public static int bcdToDecimal(String s)
{
    int len = s.length();
    int check = 0, check0 = 0;
    int num = 0, sum = 0;
    int mul = 1, rev = 0;
 
    // Iterating through the bits backwards
    for(int i = len - 1; i >= 0; i--)
    {
 
       // Forming the equivalent
       // digit(0 to 9)
       // from the group of 4.
       sum += (s.charAt(i) - '0') * mul;
       mul *= 2;
       check++;
        
       // Reinitialize all variables
       // and compute the number.
       if (check == 4 || i == 0)
       {
           if (sum == 0 && check0 == 0)
           {
               num = 1;
               check0 = 1;
           }
           else
           {
 
               // Update the answer
               num = num * 10 + sum;
           }
            
           check = 0;
           sum = 0;
           mul = 1;
       }
    }
     
    // Reverse the number formed.
    while (num > 0)
    {
        rev = rev * 10 + (num % 10);
        num /= 10;
    }
 
    if (check0 == 1)
        return rev - 1;
 
    return rev;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "100000101000";
 
    // Function Call
    System.out.println(bcdToDecimal(s));
}
}
 
// This code is contributed by coder001


Python3
# Python3 code to convert BCD to its
# decimal number(base 10).
 
# Function to convert BCD to Decimal
def bcdToDecimal(s):
 
    length = len(s);
    check = 0;
    check0 = 0;
    num = 0;
    sum = 0;
    mul = 1;
    rev = 0;
     
    # Iterating through the bits backwards
    for i in range(length - 1, -1, -1):
         
        # Forming the equivalent
        # digit(0 to 9)
        # from the group of 4.
        sum += (ord(s[i]) - ord('0')) * mul;
        mul *= 2;
        check += 1;
         
        # Reinitialize all variables
        # and compute the number
        if (check == 4 or i == 0):
            if (sum == 0 and check0 == 0):
                num = 1;
                check0 = 1;
                 
            else:
                 
                # Update the answer
                num = num * 10 + sum;
                 
            check = 0;
            sum = 0;
            mul = 1;
             
    # Reverse the number formed.
    while (num > 0):
        rev = rev * 10 + (num % 10);
        num //= 10;
         
    if (check0 == 1):
        return rev - 1;
         
    return rev;
 
# Driver Code
if __name__ == "__main__":
 
    s = "100000101000";
     
    # Function Call
    print(bcdToDecimal(s));
     
# This code is contributed by AnkitRai01


C#
// C# code to convert BCD to its
// decimal number(base 10).
// Including Header Files
using System;
 
class GFG
{
     
// Function to convert BCD to Decimal
public static int bcdToDecimal(String s)
{
    int len = s.Length;
    int check = 0, check0 = 0;
    int num = 0, sum = 0;
    int mul = 1, rev = 0;
 
    // Iterating through the bits backwards
    for(int i = len - 1; i >= 0; i--)
    {
 
        // Forming the equivalent
        // digit(0 to 9)
        // from the group of 4.
        sum += (s[i] - '0') * mul;
        mul *= 2;
        check++;
             
        // Reinitialize all variables
        // and compute the number.
        if (check == 4 || i == 0)
        {
            if (sum == 0 && check0 == 0)
            {
                num = 1;
                check0 = 1;
            }
            else
            {
     
                // Update the answer
                num = num * 10 + sum;
            }
             
            check = 0;
            sum = 0;
            mul = 1;
        }
    }
     
    // Reverse the number formed.
    while (num > 0)
    {
        rev = rev * 10 + (num % 10);
        num /= 10;
    }
 
    if (check0 == 1)
        return rev - 1;
 
    return rev;
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "100000101000";
 
    // Function Call
    Console.WriteLine(bcdToDecimal(s));
}
}
 
// This code is contributed by 29AjayKumar


输出:
828

时间复杂度: O(N)
辅助空间复杂度: O(1)