The second try of the multiplication huge intergers

The multiplication result is:
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

The error codes:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void transform_string_to_array(string s, int a[], int n);
void disp(int a[], int n);
void binary_multiplication(int a[], int b[], int c[], int n);
void get_high(int a[], int a_high[], int n);
void get_low(int a[], int a_low[], int n);
int trans_2_to_10(int a[], int n);
void trans_10_to_2(int x, int a[], int n);
int main()
{
    //the initialization conditions, give you two strings consisting with 0 or 1
    string s[2];
    s[0] = "10101100";
    s[1] = "10010011";
    //transform the string into array
    int n = 8;
    int a[2][8];                // generate 2 array containing 8 elements
    for(int i =0; i< 2; ++i){
        transform_string_to_array(s[i], a[i], n);
    }
   // disp(a[0], n); //This a[0] is not the value in the array a, but is a pointer in the type of pointer array a
   int mul_arr[20];
    binary_multiplication(a[0], a[1], mul_arr, n);
    cout << "The multiplication result is:" << endl;
    disp(mul_arr, 20);
    return 0;
}
void transform_string_to_array(string s, int a[], int n)
{
    for(int i=0; i < n; ++i){
        a[i] = int(s[n-1-i]- '0');
    }
}
void disp(int a[], int n)
{
    for(int i=0; i<n; ++i){
        cout << setw(4) << a[i];
    }
}
void binary_multiplication(int a[], int b[], int c[], int n)
{
    if(n==1){
        c[0] = a[0]*b[0];
        return;
    }
    int a_high_part[10], a_low_part[10];
    get_high(a, a_high_part, n);
    get_low(a, a_low_part, n);
    int b_high_part[10], b_low_part[10];
    get_high(b, b_high_part, n);
    get_low(b, b_low_part, n);
    int m_high_2[10], m_high_low[10], m_low_high[10], m_low_2[10];
    binary_multiplication(a_high_part, b_high_part, m_high_2, n/2);
    binary_multiplication(a_high_part, b_low_part, m_high_low, n/2);
    binary_multiplication(a_low_part, b_high_part, m_low_high, n/2);
    binary_multiplication(a_low_part, b_low_part, m_low_2, n/2);
    // transform the result from multiplication into decimal values
    int d_high_2, d_high_low, d_low_high, d_low_low;
    d_high_2 = trans_2_to_10(m_high_2, 10);
    d_high_low = trans_2_to_10(m_high_low, 10);
    d_low_high = trans_2_to_10(m_low_high, 10);
    d_low_low = trans_2_to_10(m_low_2, 10);
    int d_final;
    d_final = d_high_2*pow(2, n) + (d_high_low + d_low_high)*pow(2, n/2) + d_low_low;
    trans_10_to_2(d_final, c, 20);
}
void get_high(int a[], int a_high[], int n)
{
    for(int i =0; i<10; ++i){
        a_high[i] = 0;
    }
    for(int i = 0; i<n/2 ;++i){
        a_high[i] = a[n/2+i];
    }
}
void get_low(int a[], int a_low[], int n)
{
    for(int i =0; i<10; ++i){
        a_low[i] = 0;
    }
    for(int i=0; i<n/2; ++i){
        a_low[i] = a[i];
    }
}
int trans_2_to_10(int a[], int n)
{
    int result = 0;
    for(int i = 0; i<n; ++i){
        result += a[i]*pow(2, i);
    }
    return result;
}
void trans_10_to_2(int x, int a[], int n)
{
    int i =0;
    while(x > 0){
        a[i++] = x%2;
        x = x/2;
    }
    for(int j =i; j<n; ++j){
        a[j] = 0;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值