大きな整数の乗算、加算、減算
乗算は古典的なアルゴリズムで解決されます.大きな整数乗算を解決するためにディジネーションもありますが、効率の向上は明らかではなく、コードがやや煩わしいです.古典的アルゴリズムとは、直接ビットを乗算してビットを変換するアルゴリズムであり、簡単である.足し算と乗算に使うキャリーアルゴリズムは同じで、減算は借り手を使います.注意結果の桁数は、乗算、加算、減算の結果の桁が異なります.以下はコードです
#include <iostream>
using namespace std;
void multiply(short *a, short *b, short *result, int len_a, int len_b, int len_result);
void my_plus(short *a, short *b, short *result, int len_a, int len_b, int len_result);
int my_sub(short *a, short *b, short *result, int len_a, int len_b, int len_result);
void carry(short *result, int len_result);
int main()
{
const int len_a = 31;
const int len_b = 31;
const int len_result = len_a + len_b; // m n , m+n m+n+1
short a[len_a] = {2, 9, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 2};
short b[len_b] = {2, 0, 1, 9, 1, 8, 1, 7, 1, 6, 1, 5, 1, 4, 1, 3, 1, 2, 1, 1, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1};
//
short result[len_result] = {0};
multiply(a, b, result, len_a, len_b, len_result);
carry(result, len_result);
printf("Multiply result is: ");
if(result[0] != 0)
printf("%d", result[0]);
for(int i=1; i<len_result; i++)
printf("%d", result[i]);
printf("
");
// ,plus , my
const int len_plus_result = (len_a>len_b)?len_a+1:len_b+1; // m n ,
short plus_result[len_plus_result] = {0};
my_plus(a, b, plus_result, len_a, len_b, len_plus_result);
carry(plus_result, len_plus_result);
printf("Plus result is: ");
if(plus_result[0] != 0)
printf("%d", plus_result[0]);
for(int i=1; i<len_plus_result; i++)
printf("%d", plus_result[i]);
printf("
");
//
const int len_sub_result = (len_a>len_b)?len_a:len_b;
short sub_result[len_sub_result] = {0};
int flag = my_sub(a, b, sub_result, len_a, len_b, len_sub_result);
printf("Sub result is: ");
if(flag == -1)
printf("-");
int zero_flag = 0;
for(int i=0; i<len_sub_result; i++)
{
if(sub_result[i] != 0)
zero_flag = 1;
if(zero_flag != 0)
printf("%d", sub_result[i]);
}
printf("
");
return 0;
}
void my_plus(short *a, short *b, short *result, int len_a, int len_b, int len_result)
{
// a b
int len = (len_a>len_b)?len_a:len_b;
short *temp_a = new short[len]();
short *temp_b = new short[len]();
int m=len-1;
for(int i=len_a-1; i>=0; i--)
temp_a[m--] = a[i];
m=len-1;
for(int i=len_b-1; i>=0; i--)
temp_b[m--] = b[i];
// , result[1]
int k=1;
for(int i=0; i<len; i++)
{
result[k] = temp_a[i] + temp_b[i];
k++;
}
delete temp_a;
delete temp_b;
}
int my_sub(short *a, short *b, short *result, int len_a, int len_b, int len_result)
{
// a b
int len = (len_a>len_b)?len_a:len_b;
short *temp_a = new short[len]();
short *temp_b = new short[len]();
int m=len-1;
for(int i=len_a-1; i>=0; i--)
temp_a[m--] = a[i];
m=len-1;
for(int i=len_b-1; i>=0; i--)
temp_b[m--] = b[i];
int flag = 0;// 0,a>b
for(int i=0; i<len; i++)
if(temp_a[i] > temp_b[i])
break;
else if(temp_a[i] < temp_b[i])
{
flag = -1;
break;
}
// , result[0]
int k=0;
for(int i=0; i<len; i++)
{
if(flag == 0)
result[k] = temp_a[i] - temp_b[i];
else
result[k] = temp_b[i] - temp_a[i];
k++;
}
// ,
for(int i=len_result-1; i>0; i--)
{
if(result[i]<0)
{
result[i] += 10;
result[i-1]--;
}
}
delete temp_a;
delete temp_b;
return flag;
}
void multiply(short *a, short *b, short *result, int len_a, int len_b, int len_result)
{
int k=1;// result result[1] ,result[0]
for(int i=0; i<len_a; i++)
{
for(int j=0; j<len_b; j++)
{
result[k] += a[i]*b[i];
k++;
}
k=2;
k+=i;
}
}
void carry(short *result, int len_result)
{
int carry;
int tens_place;
for(int i=len_result-1; i>0; i--)
{
int temp = result[i];
if(temp>9)
{
carry = temp/10;
tens_place = temp%10;
result[i] = tens_place;
result[i-1] += carry;
}
}
}