solve multiplication problem with the largest integers

The inputs:

string a = "10101100"  __> int 

string b = "10010011" 

the length of binary number is n = 8

 The below codes is to initialize  the inputs 

// input
#include <iostream>
#include <iomanip>
using namespace std;
const int len = 20;
void trans_string_to_array(string a , int n , int arr[]);
void display_array(int arr[], int len);
int main()
{
    // initialization of the array stuff
    string a = "10101100";
    string b = "10010011";
    int n = 8;
    int X[len],Y[len],Z[len]; //The first array is to receive the elements in string a and the others so on ,Z = a * b
    // transform the string type into array type(in reversed order)
    trans_string_to_array(a, n, X);
    trans_string_to_array(b, n, Y);
    // to display the array to see whether its correct or not
    display_array(X, len);
    display_array(Y, len);
    

    return 0;
}
void trans_string_to_array(string a , int n , int arr[])
{
    for(int i =0; i < n ; ++i){
        arr[i] = int(a[n-1-i] - '0');  // you have to transform the char type into int, otherwise it will show its ASCII value
    }
    for( int i = n; i < len; ++i){
        arr[i] = 0;
    }
}
void display_array(int arr[], int len)
{
    for(int i = 0 ; i < len; ++i){
        cout<< setw(3) << arr[i] ;
    }
    cout << endl;
}

The outpus of the initialization:

  0  0  1  1  0  1  0  1  0  0  0  0  0  0  0  0  0  0  0  0
  1  1  0  0  1  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0

Process returned 0 (0x0)   execution time : 0.080 s
Press any key to continue.

The wrong codes:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void transform_string_to_array(string a, int arr[], int n);
void dis_array(int arr[]);
void multiplication(int arr1[], int arr2[], int arr3[], int n);
void get_high(int arr[], int arr_high[], int n);
void get_low(int arr[], int arr_low[], int n);
int transform_2_to_10(int arr[], int n);
void transform_10_to_2(int x, int arr[]);
const int len = 20;
int main()
{
    int n = 8;
    string a = "10101100";
    string b = "10010011";
    int arr1[len], arr2[len], arr3[len];
    transform_string_to_array(a, arr1, n);
    transform_string_to_array(b, arr2, n);
    cout << "The content of arr1:"<< endl;
    dis_array(arr1);
    cout << transform_2_to_10(arr1, len) << endl;
    cout << "The content of arr2:" << endl;
    dis_array(arr2);
    cout << transform_2_to_10(arr2,len) << endl;
    multiplication(arr1, arr2, arr3, n);
    dis_array(arr3);
    cout << endl << transform_2_to_10(arr3,len);
    return 0;
}
void transform_string_to_array(string a, int arr[], int n)
{
    int i;
    for(i = 0; i<n; ++i){
        arr[i] = int(a[n-1-i] - '0');
    }
    for(int j = i; j < len; ++j){
        arr[j] = 0;
    }
}
void dis_array(int arr[])
{
    for( int i = len-1; i >=0; --i){
        cout << setw(3) << arr[i];
    }
    cout <<endl;
}
void multiplication(int arr1[], int arr2[], int arr3[], int n)
{
    if(n==1){
        arr3[0] = arr1[0] * arr2[0];
        return;
    }
    for(int i =0; i<len; ++i){
        arr3[i] = 0;
    }
    int arr1_high[len], arr1_low[len], arr2_high[len], arr2_low[len];
    get_high(arr1, arr1_high, n );
    get_low(arr1, arr1_low, n);
    get_high(arr2, arr2_high, n);
    get_low(arr2, arr2_low, n);
    int part1[len], part2[len], part3[len], part4[len];
    int middle = n/2;
    multiplication(arr1_high, arr2_high, part1, middle);
    multiplication(arr1_high, arr2_low, part2, middle);
    multiplication(arr1_low, arr2_high, part3, middle);
    multiplication(arr1_low, arr2_low, part4, middle);
    int d1, d2, d3, d4;
    d1 = transform_2_to_10(part1, len);
    d2 = transform_2_to_10(part2, len);
    d3 = transform_2_to_10(part3, len);
    d4 = transform_2_to_10(part4, len);
    int final_result;
    final_result = d1*(int)pow(2.0, n) + (d2 + d3)*(int)pow(2.0, n/2) + d4;
    transform_10_to_2(final_result, arr3);
}
void get_high(int arr[], int arr_high[], int n)
{
   int i;
   for(i =0; i<len; ++i){
        arr_high[i] = 0;
   }
   for(i = n/2; i<n; i++){
        arr_high[i] = arr[i];
   }
}
void get_low(int arr[], int arr_low[], int n)
{
    int middle = n/2;
    for( int i = 0; i< middle; ++i){
        arr_low[i] = arr[i];
    }
    for(int j = middle; j <len; ++j){
        arr_low[j] = 0;
    }
    arr_low[middle] = '\0';
}
int transform_2_to_10(int arr[], int n)
{
    int result = 0;
    for(int i =0; i<n; ++i){
        result += arr[i]*pow(2.0, i);
    }
    return result;
}
void transform_10_to_2(int x, int arr[])
{
    int i, j=0;
    while(x !=0){
        arr[j] = x%2;
        ++j;
        x = x/2;
    }
    for(i =j; i < len; ++i){
        arr[j] = 0;
    }
}

The wrong anwser, I wish I could find the reason behind it. 

The content of arr1:
  0  0  0  0  0  0  0  0  0  0  0  0  1  0  1  0  1  1  0  0
172
The content of arr2:
  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  1  0  0  1  1
147
 -1  0  0  0 -1 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0

-573440

猜你喜欢

转载自blog.csdn.net/weixin_38396940/article/details/120661192