大整数加减法的实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

typedef struct{
    char *digit;
    int len;
    int sgn;
} Int;

Int *Int_init_0(){
    Int *res = (Int*)malloc(sizeof(Int));
    res->digit = NULL;
    res->len = 0;
    res->sgn = 0;
    return res;
}

Int *Int_init_str(char *a){
    if(a[0] == '0')
        return Int_init_0();
    int state = 0;
    if(a[0] == '-')
        state = 1;
    Int *res = (Int*)malloc(sizeof(Int));
    int len = strlen(a);
    res->digit = (char*)malloc(len - state);
    res->len = len - state;
    res->sgn = state < 1 ? 1 : -1;
    for( int i = 0; i<=len-1-state; i++){
        res->digit[i] = a[len-i-1] - '0';
    }//将数字顺序颠倒(从后往前进位加) 
    return res;
}

void Int_print(Int *a){
    printf("[");
    if(a->sgn == 0)
        printf("0");
    if(a->sgn == -1 )
        printf("-");
    for(int i = a->len - 1; i >= 0; i--){
        printf("%d",a->digit[i]);
    }//注意输出时要从后向前输出 
    printf("]\n");
}

static Int *Int_add_abs(Int *a, Int *b){
    int n = (a->len > b->len ? a->len:b->len)+1;
    char *d = (char*)malloc(n);
    int i, carry=0, t;
    for(i=0; i<n; i++){
        int x1, x2;
        x1 = a->len > i ? a->digit[i]:0;
        x2 = b->len > i ? b->digit[i]:0;
        t = x1 + x2 + carry;
        d[i] = t % 10;
        carry = t / 10;
    }
    if(d[n-1] == 0){
        char *d1 = (char*)malloc(n-1);
        memcpy(d1, d, n-1);
        free(d);
        n--;
        d = d1;
    }
    Int *res = (Int*)malloc(sizeof(Int));
    res->digit = d;
    res->sgn = a->sgn;
    res->len = n;
    return res;
}

int Int_cmp(Int *x, Int *y){
    if(x->len > y->len)
        return 1;
    if(x->len < y->len)
        return -1;
    int i;
    for(i=x->len - 1; i>=0; i--){
        if(x->digit[i] > y->digit[i])
            return 1;
        if(y->digit[i] > x->digit[i])
            return -1;
    }
    return 0;
}

static Int *Int_sub_abs(Int *x, Int *y){
    int cmp_result = Int_cmp(x, y);
    if(cmp_result == 0)
        return Int_init_0();
    Int *x1 = x;
    Int *y1 = y;
    if(cmp_result == -1){
        x1 = y;
        y1 = x;
    }
    x = x1;
    y = y1;
    int n = x->len;
    int i;
    char xd, yd,t,carry = 0;
    char *d = (char*)calloc(n,sizeof(char));
    for(i=0; i<n; i++){
        yd = y->len > i?y->digit[i]:0;
        t = x->digit[i] - yd + carry;
        carry = 0;
        if(t<0){
            t += 10;
            carry = -1;
        }
        d[i] = t;
    }
    int real_size = 0;
    for(i=n-1; i>=0; i--){
        if(d[i] != 0){
            real_size = i+1;
            break;
        }
    }
    if(real_size != n){
        char *d1 = (char*)calloc(real_size,sizeof(char));
        memcpy(d1,d,real_size);
        free(d);
        d = d1;
    }
    Int *res = (Int*)malloc(sizeof(Int));
    res->digit = d;
    res->sgn = cmp_result > -1 ? x->sgn : (-y->sgn);
    res->len = real_size;
    return res; 
}

Int* Int_add(Int *x, Int *y){
    Int *res = (Int*)malloc(sizeof(Int));
    if(x->sgn != y->sgn){ 
        if(Int_cmp(x,y) == 0)
            return Int_init_0();
        res = Int_sub_abs(x,y);
        if(x->sgn > y->sgn){
            if(Int_cmp(x,y) == 1)
                res->sgn = 1;
            else res->sgn = -1; 
        }
    else {
        if(Int_cmp(x,y) == 1)
                res->sgn = -1;
            else res->sgn = 1; 
        }
    }
    else 
        res = Int_add_abs(x,y);
    return res;    
}

Int *Int_sub(Int *x, Int *y){
    Int *res = (Int *)malloc(sizeof(Int));
    if(x->sgn != y->sgn){
        res = Int_add_abs(x,y);
        res->sgn = x->sgn;
    }
    else res = Int_sub_abs(x,y);
    return res;
}

int main(){
    char *s1 = "123";
    char *s2 = "-123";
    Int *a = Int_init_str(s1);
    Int *b = Int_init_str(s2);
    Int *c = Int_add(a, b);
    Int_print(a);
    Int_print(b);
    Int_print(c);
    return 0;
}
        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值