【高精度运算】加,减,乘,比较

void Susake_add(char *s1, char *s2) {
     int len1 = strlen(s1), len2 = strlen(s2), len, i;
     int a[N] = {0}, b[N] = {0};
     for(i = 0; i < len1; a[i] = s1[len1 - 1 - i++] - '0') ;
     for(i = 0; i < len2; b[i] = s2[len2 - 1 - i++] - '0') ;
     len = len1 > len2 ? len1 : len2;
     for(i = 0; i < len; i++) {
         a[i] += b[i];
         if(a[i] >= 10) {
             a[i] %= 10;
             a[i+1]++;
         }
     }
     if(a[len]) printf("%d", a[len]);
     for(i = len - 1; i >= 0; printf("%d", a[i--]));
     printf("\n");
}
void Susake_sub(char *s1, char *s2) {
    int len, len1, len2, i, j;
    int a[N] = {0}, b[N] = {0};
    char s3[N];
    memset(s3, 0, sizeof(s3));
    char *s = (char *)malloc(10 * sizeof(char));
    s = Susake_com(s1, s2);
    if(strcmp("a<b", s) == 0) {
        strcpy(s3, s1);
        strcpy(s1, s2);
        strcpy(s2, s3);
        printf("-");
    }
    free(s);
    len1 = strlen(s1); len2 = strlen(s2);
    for(i = 0; i < len1; a[i] = s1[len1 - 1 - i++] - '0') ;
    for(i = 0; i < len2; b[i] = s2[len2 - 1 - i++] - '0') ;
    len = len1 > len2 ? len1 : len2;
    for(int i = 0; i < len; i++) {
        if(a[i] - b[i] >= 0) a[i] -= b[i];
        else {
            a[i] = a[i + 1] == 0 && i + 1 == len1 ? b[i] - a[i] : a[i] + 10 - b[i];
            a[i + 1]--;
        }
    }
    if(a[len - 1] || len == 1) printf("%d", a[len - 1]);
    for(int i = len - 2; i >= 0; i--) printf("%d", a[i]);
    printf("\n");
}
void Susake_mul(char *s1, char *s2) {
     int len1 = strlen(s1), len2 = strlen(s2), len, i, j, k = 0, tip = 0, tag = 0;
     int a[N] = {0}, b[N] = {0}, c[N] = {0};
     for(i = 0; i < len1; a[i] = s1[len1 - 1 - i++] - '0') ;
     for(i = 0; i < len2; b[i] = s2[len2 - 1 - i++] - '0') ;
     for(i = 0; i < len1; i++) {
         for(j = 0; j < len2; j++) {
             if(a[i] * b[j] >= 10) {
                 c[k] += a[i] * b[j] % 10;
                 if(c[k] >= 10){
                     c[k] = c[k] % 10;
                     c[k + 1]++;
                 }
                 c[k + 1] += a[i] * b[j] / 10;
                 if(c[k + 1] >= 10){
                     c[k + 1] = c[k + 1] % 10;
                     c[k + 2]++;
                 }
             }
             else {
                 c[k] += a[i] * b[j];
                 if(c[k] >= 10) {
                     c[k] = c[k] % 10;
                     c[k + 1]++;
                     if(c[k + 1] >= 10){
                         c[k + 1] = c[k + 1] % 10;
                         c[k + 2]++;
                     }
                 }
             }
             k++;
             tag = k;
         }
         tip++;
         k = tip;
     }
     if(c[tag]) printf("%d", c[tag]);
     for(i = tag - 1; i >= 0; i--) printf("%d", c[i]);
     printf("\n");
 }
char *Susake_com(char *a, char *b) {
     char *res;
     res = (char *)malloc(10 * sizeof(char));
     int len1 = strlen(a), len2 = strlen(b), i, t = 0;
     if(a[0] != '-' && b[0] != '-') {
         if(len1 > len2) {
             strcpy(res, "a>b");
             return res;
         }
         else if(len1 == len2) {
             for(i = 0; i < len1; i++) {
                 t++;
                 if((a[i] - '0') == (b[i] - '0')) ;
                 else if((a[i] - '0') < (b[i] - '0')) {
                     strcpy(res, "a<b");
                     return res;
                 }
                 else {
                     strcpy(res, "a>b");
                     return res;
                 }
                 if(t == len1) {
                    strcpy(res, "a==b");
                    return res;
                 }
             }
         }
         else {
            strcpy(res, "a<b");
            return res;
         }
     }
     if(a[0] == '-' && b[0] == '-') {
         if(len1 > len2) {
            strcpy(res, "a<b");
            return res;
         }
         else if(len1 == len2) {
             for(i = 1; i < len1; i++) {
                 t++;
                 if((a[i] - '0') == (b[i] - '0')) ;
                 else if((a[i] - '0') > (b[i] - '0')) {
                    strcpy(res, "a<b");
                    return res;
                 }
                 else if((a[i] - '0') < (b[i] - '0')) {
                     strcpy(res, "a>b");
                     return res;
                 }
                 if(t == len1 - 1) {
                    strcpy(res, "a==b");
                    return res;
                 }
             }
         }
         else {
            strcpy(res, "a>b");
            return res;
         }
     }
     if(a[0] == '-' && b[0] != '-') {
        strcpy(res, "a<b");
        return res;
     }
     if(b[0] == '-' && a[0] != '-') {
        strcpy(res, "a>b");
        return res;
     }
}

 

posted on 2015-06-17 15:45  Susake  阅读(270)  评论(0编辑  收藏  举报

导航