题目大意:高精度数乘法
注意:每次输入的那两个结构体内的数组要初始化为零,数组不能开太小;
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int N = 16255;
class begin {
public:
int len , s[N];
begin() {
len = 0;
memset(s, 0, sizeof (s));
}
begin operator = (const char * s1);
begin operator += (const begin s1);
void multiply (const begin & n);
} b[2], sum;
begin begin::operator += (const begin s1) {
int i , a = 0;
int l = (len > s1.len) ? len: s1.len;
for (i = 0; a || i < l; i++) {
a = a + s1.s[i] + s[i] ;
s[i] = a % 10;
a = a / 10;
}
len = i;
return *this;
}
void begin::multiply (const begin& n) {
memset(sum.s,0,sizeof(sum.s));
sum.len = 0;
begin c;
c.len = 0;
int i , j , k, a = 0;
for(i = 0; i < len; i++) {
for ( k = i, j = 0; a || j < n.len; j++, k++) {
a = a + s[i] * n.s[j];
c.s[k] = a % 10;
a = a / 10;
}
c.len = j + i;
sum += c;
memset(c.s, 0 , sizeof(c.s)) ;
}
}
begin begin ::operator = (const char * s1) {
len = strlen(s1);
for (int i = 0 ; i < len ; i++) {
s[i] = s1[len - 1 - i] - '0';
}
return *this;
}
int main () {
char a[2][N];
while ( scanf("%s", &a[0]) != EOF && scanf("%s", &a[1]) != EOF) {
memset(b[0].s, 0, sizeof(b[0].s));
memset(b[1].s, 0, sizeof(b[1].s));
if(strcmp(a[0],"0") == 0 || strcmp(a[1],"0") == 0)
{
printf("0\n");
continue;
}
b[0] = a[0];
b[1] = a[1];
if(b[0].len < b[1].len)
b[0].multiply(b[1]);
else
b[1].multiply(b[0]);
for (int i = sum.len - 1; i >= 0; i--)
printf("%d", sum.s[i]);
printf("\n");
}
return 0;
}