我应该使用哪种数据类型以C语言存储变量10 ^ 200? [英] which datatype should i use to store a variable 10^200 in C language?

查看:49
本文介绍了我应该使用哪种数据类型以C语言存储变量10 ^ 200?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理10 ^ 200或大于C语言中的整数?即使我使用了很长时间也无法正常工作.那我该怎么办?我听说过大整数.但不知道如何使用它.据我所知,这是C#的库函数.但是我正在使用C.除了大整数之外,还有其他方法可以处理这么大的整数吗?有人还可以向我解释如何使用大整数吗?

how to handle integers like 10^200 or larger than that in C language? it won't work even I use long long. then what should I do? I heard about big integer. but don't know how to work with it. as far i know it is a library function for C#. but I am working with C. is there any other way except big integer to work with such large integers? and can someone also explain me how to use big integer?

为了澄清,我只是在寻找一种适用于C语言的解决方案.

To clarify, I'm only looking for a solution that will work in C.

推荐答案

这是我们使用整数数组的方式(尽管最好使用字符数组).我仅显示了加法,余数运算(如比较,乘法减法)您可以自己写.

This is how we do by using integer arrays(though it is better to use character arrays).I have shown only addition , rest operation like comparison , multiplication subtraction you can write on your own.

#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;
}

这篇关于我应该使用哪种数据类型以C语言存储变量10 ^ 200?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆