首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在C语言中,我应该使用哪种数据类型来存储变量10^200?

在C语言中,我应该使用哪种数据类型来存储变量10^200?
EN

Stack Overflow用户
提问于 2014-05-11 11:54:08
回答 3查看 363关注 0票数 2

如何处理10^200或大于C语言中的整数?即使我使用long long,它也不会起作用。那我该怎么做呢?我听说过大整数。但不知道如何使用它。据我所知,它是一个用于C#的库函数。但我正在使用C。除了大整数之外,还有其他方法可以处理这么大的整数吗?还有没有人能解释一下如何使用大整数?

为了澄清,我只是在寻找一个可以在C中工作的解决方案。

EN

回答 3

Stack Overflow用户

发布于 2014-05-11 12:06:09

参见similair question

简而言之,没有内置类型,但有一些开源库具有这种功能:用于C++的Boost.Multiprecision (Boost license)和用于C的GMP (LGPL v3 / v2双重许可证)

如果由于某些原因(例如许可证不兼容)你不能使用这些库,here如果你打算自己实现这样的功能,有一些技巧。

票数 2
EN

Stack Overflow用户

发布于 2014-05-11 12:57:39

这就是我们使用整数数组的方法(尽管使用字符数组更好).I只显示了加法,rest操作,如比较,乘法减法,你可以自己编写。

代码语言:javascript
运行
复制
#include<stdio.h>
#include<stdlib.h>
#define len 500 // max size of those numbers you are dealing

int findlength(int num[])
{
        int i=0;
        while(num[i]==0)
            ++i;
        return (len-i);


}


void equal(int num[] ,int a[])
{
        int i;

        for(i=0;i<len;++i)
            num[i]=a[i];

        free(a);

}


void print(int num[],int l)
{
        int i;

        for(i=len-l;i<len;++i)
            printf("%d",num[i]);

        printf("\n");

}


int *add(int num1[] , int num2[] )
{
        int i,carry=0;
        int *a = malloc(sizeof(int)*len); // an dynamic answer array has to be created because an local array will be deleted as soon as control leaves the function

        for(i=0;i<len;++i)
            a[i]=0;

        for(i=len-1;i>=0;--i)
        {
            a[i]=num1[i]+num2[i]+carry;
            carry=a[i]/10;
            a[i]=a[i]%10;
        }

        return a;

}


void input_number(int num[])
{
        int i=0,temp[len],j;
        char ch;

        for(i=0;i<len;++i) // fill whole array by zero. helps in finding length
            num[i]=0;

        i=0;

        printf("Enter number : ");

        while((ch=getchar())!='\n')
                temp[i++]= ch-'0'; //Saving number from left to right

        //shifting whole number to right side, now numbers are stored as 00000012 , 00000345 etc...

        for(j=0;j<=i;++j)
             num[len-1-j]=temp[i-j-1];


}

int main()
{
        int num1[len],num2[len],num3[len]; // to save space Use character array of size len.Char is also numeric type. It can hold 0- 9

        input_number(num1); // this way you can input those numbers
        input_number(num2);

        int len1=findlength(num1),len2=findlength(num2); // Might be used in Many operations.

        equal(num3,add(num1,num2));// This way define add , or subtract or any other operation you wan to do but return pointer to answer array.
        //Use equal function to equate "num3 = answer array" by some implementation.

        print(num3,findlength(num3)); // to print the number.
        // create an header file of all these function implementations and use them wherever you like

        return 0;
}
票数 2
EN

Stack Overflow用户

发布于 2014-05-11 12:12:18

Arbitrary-precision arithmetic的概念,有很多库可以满足您的需求,通常这些库使用整数、浮点数或Fixed-point arithmetic来处理任意精度的算术。

你可以找到许多针对不同平台、许可证和语言的解决方案,这取决于你想在什么样的环境中做什么,但一般来说,你会发现很多选择。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23588717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档