【问题标题】:Adding large integer values in C [duplicate]在C中添加大整数值[重复]
【发布时间】:2020-07-22 01:11:34
【问题描述】:

这是一个相当简单的问题:

如何获取 2 个无符号整数值并将它们添加到 C 中以用于以下输入约束。

0

我知道我可以将输入视为字符串,但我仍然有这个问题。

我已经写了下面的代码,但它不能取这么大的值 10^98

#include <stdio.h>

int main() {
    unsigned long long int a, b;
    int x = scanf("%llu%llu", &a, &b);
    while (x > 0) {
        printf("%llu\n",(a + b));
        x = scanf("%llu%llu" ,&a, &b);
    }
    return 0;
}

【问题讨论】:

    标签: c long-integer biginteger unsigned-integer


    【解决方案1】:

    您不能为此使用常规算术类型,即使 unsigned long long 只有 64 位,这比您的目的所需的要少得多。

    还要注意你的循环测试不正确:它应该是while (x == 2)

    这是一个可以处理99位十进制数字的小程序:

    #include <stdio.h>
    #include <string.h>
    
    char *bigadd(char *c, const char *a, const char *b) {
        size_t alen = strlen(a);
        size_t blen = strlen(b);
        size_t clen = (alen > blen) ? alen : blen;
        size_t i = clen;
        int digit, carry = 0;
        c[i] = '\0';
        while (i > 0) {
            digit = ((alen ? a[--alen] - '0' : 0) +
                     (blen ? b[--blen] - '0' : 0) +
                     carry);
            carry = digit >= 10;
            c[--i] = (char)('0' + (digit - carry * 10));
        }
        if (carry) {
            memmove(c + 1, c, clen + 1);
            c[0] = '1';
        }
        return c;
    }
    
    int main(int argc, char *argv[]) {
        char a[100], b[100], c[101];
    
        while (scanf("%99s%99s", a, b) == 2) {
            printf("%s\n", bigadd(c, a, b));
        }
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用 bignum 库,例如 GMP。这是一个例子。

      #include <gmp.h>
      #include <stdio.h>
      #include <assert.h>
      
      int main(){
      
        char inputStr[1024];
        /*
           mpz_t is the type defined for GMP integers.
           It is a pointer to the internals of the GMP integer data structure
         */
        mpz_t n;
        int flag;
      
        printf ("Enter your number: ");
        scanf("%1023s" , inputStr); /* NOTE: never every write a call scanf ("%s", inputStr);
                                        You are leaving a security hole in your code. */
      
        /* 1. Initialize the number */
        mpz_init(n);
        mpz_set_ui(n,0);
      
        /* 2. Parse the input string as a base 10 number */
        flag = mpz_set_str(n,inputStr, 10);
        assert (flag == 0); /* If flag is not 0 then the operation failed */
      
        /* Print n */
        printf ("n = ");
        mpz_out_str(stdout,10,n);
        printf ("\n");
      
        /* 3. Add one to the number */
      
        mpz_add_ui(n,n,1); /* n = n + 1 */
      
        /* 4. Print the result */
      
        printf (" n +1 = ");
        mpz_out_str(stdout,10,n);
        printf ("\n");
      
      
        /* 5. Square n+1 */
      
        mpz_mul(n,n,n); /* n = n * n */
      
      
        printf (" (n +1)^2 = ");
        mpz_out_str(stdout,10,n);
        printf ("\n");
      
      
        /* 6. Clean up the mpz_t handles or else we will leak memory */
        mpz_clear(n);
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-07-14
        • 1970-01-01
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        • 2021-08-21
        • 2011-11-03
        • 2012-09-01
        相关资源
        最近更新 更多