#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Bigint{
char *strnum;
unsigned int length;
}* Bigint;
void strrev(char *str, int len)
{
char cpy[len];
int i, j;
for(i = 0, j = len - 1; j >= 0; i++, j--){
cpy[i] = str[j];
}
cpy[i] = '\0';
}
Bigint bigint_new(const char *strnum)
{
Bigint bigint
= malloc(sizeof(Bigint
));
bigint->length = len;
bigint
->strnum
= malloc(len
+1);
strcpy(bigint
->strnum
, strnum
);
return bigint;
}
void bigint_del(Bigint bigint)
{
}
void bigint_show(Bigint bigint)
{
}
Bigint bigint_add(Bigint big1, Bigint big2)
{
int length = (big1->length > big2->length ? big1->length : big2->length);
char strnum[length + 1];
memset(strnum
, 0, length
+ 1);
int n, i1, i2, j = 0;
for (i1 = big1->length, i2 = big2->length, j = 0;
i1 >= 0 && i2 >= 0;
--i1, --i2, ++j) {
n = strnum[j] + (big1->strnum[i1] + big2->strnum[i2] - '0' * 2);
if (n > 9) {
strnum[j] = n % 10;
strnum[j+1] = 1;
}else{
strnum[j] = n;
}
}
if(i1 > 0 && i2 <= 0){
while(i1 >= 0){
strnum[j++] += big1->strnum[i1--] - '0';
}
}else if(i1 <= 0 && i2 > 0){
while(i2 >= 0){
strnum[j++] += big2->strnum[i2--] - '0';
}
}
if(strnum[j+1] != 0){
j++;
}
int i;
for(i = 0; i < j; i++){
strnum[i] += '0';
}
strnum[j] = '\0';
strrev(strnum, j);
return bigint_new(strnum);
}
int main(void)
{
Bigint num1 = bigint_new("990");
Bigint num2 = bigint_new("900");
Bigint num3 = bigint_add(num1, num2);
bigint_show(num3);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKdHlwZWRlZiBzdHJ1Y3QgQmlnaW50ewogICAgY2hhciAqc3RybnVtOwoJdW5zaWduZWQgaW50IGxlbmd0aDsKfSogQmlnaW50OwoKdm9pZCBzdHJyZXYoY2hhciAqc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