|
注意: 要去掉前面的0再输出。
#include <stdio.h>
#include <string.h>

 struct node {
char arr[128];
int len;
};

__inline int max(int a, int b)
  {
return a > b ? a : b;
}

__inline void add(struct node *a, struct node *b)
  {
int i, val;

 for (val = i = 0; i < max(a->len, b->len); i++) {
if (i < a->len)
val += a->arr[i];
if (i < b->len)
val += b->arr[i];
a->arr[i] = val % 10;
val /= 10;
}
if (val)
a->arr[i++] = val;
a->len = i;
}

__inline void mul_single(struct node *a, int val, int pad, struct node *c)
  {
int i, r;

for (i = 0; i < pad; i++)
c->arr[i] = 0;
c->len = pad;
 for (r = i = 0; i < a->len; i++) {
r += a->arr[i] * val;
c->arr[c->len++] = r % 10;
r /= 10;
}
if (r)
c->arr[c->len++] = r;
}

__inline void mul(struct node *a, struct node *b, struct node *c)
  {
struct node t;
int i;

c->len = 0;
 for (i = 0; i < a->len; i++) {
mul_single(b, a->arr[i], i, &t);
add(c, &t);
}
}

__inline void input(struct node *t)
  {
char str[sizeof(t->arr)];
int i, len;

scanf("%s", str);
len = strlen(str);
t->len = 0;
for (i = len - 1; i >= 0; i--)
t->arr[t->len++] = str[i] - '0';
}

__inline void output(struct node *t)
  {
int i;

for (i = t->len - 1; i >= 0 && !t->arr[i]; i--);
 if (i < 0) {
printf("0\n");
return ;
}
for ( ; i >= 0; i--)
putc(t->arr[i] + '0', stdout);
putc('\n', stdout);
}

int main()
  {
struct node a, b, c;

freopen("e:\\test\\in.txt", "r", stdin);

input(&a);
input(&b);
mul(&a, &b, &c);
output(&c);

return 0;
}

|