九度oj 1037 Powerful Calculator 高精度算法

http://ac.jobdu.com/problem.php?pid=1037

高精度运算加,减,乘。并且带有符号。

//高精度算法,400位以内的加,减,乘。
#include <stdio.h>
#include <string.h>

//add func
void add(int *a, int lena, int *b, int lenb, int *t, int &lent)
{
    int i, j, k = 0;
    for(i = 0, j = 0; i < lena && j < lenb; i ++, j ++){
        t[k ++] = a[i] + b[j];
    }
    while(i < lena){
        t[k ++] = a[i ++];
    }
    while(j < lenb){
        t[k ++] = b[j ++];
    }
    j = 0;
    for(i = 0; i < k; i ++){
        t[i] += j;
        j = t[i]/10;
        t[i] %= 10;
    }
    while(j != 0){
        t[k ++] = j % 10;
        j /= 10;
    }
    lent = k;
}

//sub func
void sub(int *a, int lena, int *b, int lenb, int *t, int &lent)
{
    int i, j = 0, k = 0;
    for(i = 0; i < lenb; i ++){
        t[i] = a[i] - b[i];
    }
    k = i;
    while(i < lena){
        t[k ++] = a[i ++];
    }
    for(i = 0; i < k; i ++){
        t[i] += j;
        if(t[i] < 0){
            j = -1;
            t[i] += 10;
        }
        else {
            j = 0;
        }
    }
    while(t[k - 1] == 0){
        k --;
    }
    lent = k;
}

//numti func
void multi(int *a, int lena, int *b, int lenb, int *t, int &lent)
{
    int i, j, k = 0;

    for(i = 0; i < lena; i ++){
        for(j = 0; j < lenb; j ++){
            t[i + j] += a[i] * b[j];
        }
    }
    k = i + j - 1;
/*    for(i = 0; i < k; i ++){
        printf("%d ", t[i]);
    }
    printf("\n");
*/
    j = 0;
    for(i = 0; i < k; i ++){
        t[i] += j;
        j = t[i] / 10;
        t[i] %= 10;
    }

    while(j != 0){
        t[k ++] = j % 10;
        j /= 10;
    }
    lent = k;
}

//chang char * to int * and return the lenth and signal
void getnum(char *str, int *a, int &lena, char &op)
{
    int len = strlen(str);
    int i, t = 0;
    if(str[0] == '-')op = '-';
    else op = '+';
    for(i = len - 1; i >= 1; i --){
        a[t ++] = (int)(str[i] - '0');
    }
    if(str[0] != '-' && str[0] != '+')a[t ++] = (int)(str[0] - '0');
    lena = t;
}

int main()
{
//    freopen("input.in", "r", stdin);
    char str[450], str2[450], op, op2;
    int num[450], num2[450];
    int ares[450], sres[450], mres[900];
    int lennum, lennum2, lena, lens, lenm;
    int i;
    while(gets(str)){
        gets(str2);
        lennum = strlen(str);
        lennum2 = strlen(str2);
        getnum(str, num, lennum, op);
        getnum(str2, num2, lennum2, op2);
        /*
        add(num, lennum, num2, lennum2, ares, lena);
        printf("%d\n", lena);
        for(i = lena - 1; i >= 0; i --){
            printf("%d", ares[i]);
        }
        printf("\n");
        if(lennum > lennum2){
            sub(num, lennum, num2, lennum2, sres, lens);
            printf("%d\n", lens);
            for(i = lens - 1; i >= 0; i --){
                printf("%d", sres[i]);
            }
            printf("\n");
        }*/
        if(op == op2){
            //add
            add(num, lennum, num2, lennum2, ares, lena);
            if(op == '-')printf("-");
            for(i = lena - 1; i >= 0; i --){
                printf("%d", ares[i]);
            }
            printf("\n");
            //sub
            if(lennum > lennum2){
                sub(num, lennum, num2, lennum2, sres, lens);
                if(op == '-')printf("-");
                for(i = lens - 1; i >= 0; i --){
                    printf("%d", sres[i]);
                }
                printf("\n");
            }//if
            else if(lennum < lennum2){
                sub(num2, lennum2, num, lennum, sres, lens);
                if(op == '+')printf("-");
                for(i = lens - 1; i >= 0; i --){
                    printf("%d", sres[i]);
                }
                printf("\n");
            }//else if
            else{
                for(i = lennum - 1; i >= 0; i --){
                    if(num[i] != num2[i])break;
                }
                if(i < 0)printf("0\n");
                else if(num[i] > num2[i]){
                    sub(num, lennum, num2, lennum2, sres, lens);
                    if(op == '-')printf("-");
                    for(i = lens - 1; i >= 0; i --){
                        printf("%d", sres[i]);
                    }
                    printf("\n");
                }
                else{
                    sub(num2, lennum2, num, lennum, sres, lens);
                    if(op == '+')printf("-");
                    for(i = lens - 1; i >= 0; i --){
                        printf("%d", sres[i]);
                    }
                    printf("\n");
                }//else
            }//else
        }//if
        else{
            //add
            if(lennum > lennum2){
                sub(num, lennum, num2, lennum2, ares, lena);
                if(op == '-')printf("-");
                for(i = lena - 1; i >= 0; i --){
                    printf("%d", ares[i]);
                }
                printf("\n");
            }//if
            else if(lennum < lennum2){
                sub(num2, lennum2, num, lennum, ares, lena);
                if(op == '+')printf("-");
                for(i = lena - 1; i >= 0; i --){
                    printf("%d", ares[i]);
                }
                printf("\n");
            }//else if
            else{
                for(i = lennum - 1; i >= 0; i --){
                    if(num[i] != num2[i])break;
                }
                if(i < 0)printf("0\n");
                else if(num[i] > num2[i]){
                    sub(num, lennum, num2, lennum2, ares, lena);
                    if(op == '-')printf("-");
                    for(i = lena - 1; i >= 0; i --){
                        printf("%d", ares[i]);
                    }
                    printf("\n");
                }
                else{
                    sub(num2, lennum2, num, lennum, ares, lena);
                    if(op == '+')printf("-");
                    for(i = lena - 1; i >= 0; i --){
                        printf("%d", ares[i]);
                    }
                    printf("\n");
                }//else
            }//else
            //sub
            add(num, lennum, num2, lennum2, sres, lens);
            if(op == '-')printf("-");
            for(i = lens - 1; i >= 0; i --){
                printf("%d", sres[i]);
            }
            printf("\n");
        }

        memset(mres, 0, sizeof(mres));
        multi(num, lennum, num2, lennum2, mres, lenm);
        if(op != op2)printf("-");
        for(i = lenm - 1; i >= 0; i --){
            printf("%d", mres[i]);
        }
        printf("\n");

    }
    return 0;
}


你可能感兴趣的:(算法)