【问题标题】:Int array to store large numbers (over 20 digits)用于存储大数(超过 20 位)的 int 数组
【发布时间】:2012-11-04 18:08:40
【问题描述】:

我有一个任务要求我首先设置整数数组来存储任意大的数字。通过使用 int 数组的每个元素,我可以为每个元素保留一个数字,因为 int 变量类型是 32 位,并且在失败之前最多只能达到大约 20 亿。我知道还有其他库使用 BigInt,但我想创建自己的数组。

我测试了我的代码,但它似乎不接受超过 9 位的值,直到它开始失败。

int * toArray(int, int);
int main()
{
    int n, *res, counter, i;
    printf("Input: ");
    scanf("%d", &n);
    while(n>0){
         n/=10;
         counter++;
    }
    res = toArray(n, counter);
    for(i=1;i<=counter;i++){
         printf("%d", *(res)+i);
    }
    printf("\n");
}   

int * toArray(int n, int counter)
{
    int i;
    int *numberArray = malloc(sizeof(int) * counter);
    for(i=0; i< counter; i++)
    {   
         numberArray[i] = n % 10;
    }
    return numberArray;
}

我希望至少能够接受接近 20 位数字。我知道这可以用 int 数组来完成,尽管 char 数组(或字符串)也是可能的。但我想改用 int 数组。任何有助于理解为什么它在 9 位数左右失败以及过去的建议将不胜感激。谢谢。

【问题讨论】:

  • 数组索引总是从 0 开始。为什么要使用i=1, i&lt;=counter 打印?
  • 旁注:你永远不会释放在toArray 中创建的整数数组。你应该解决这个问题。
  • 当你计算数字时,你做n /= 10;,而n &gt; 0,所以当你打电话给res = toArray(n, counter);时,你的号码n是0。如果你需要一个原始值的副本这样做。而在toArray 中,你永远不会修改n,所以所有数字都会得到相同的值n % 10

标签: c arrays


【解决方案1】:

问题是您正在从键盘读取 int

scanf("%d", &n);

因此,无论您输入多少位,您仍然只能得到 9 位。

为了能够输入任意数字,您必须改为将其读取为字符串,然后将其转换为您的 int 数组。

编辑:

这种方式(例如)将允许 20 位数字

  char ch;
  int digits[20];
  int i = 0;
  do
  {
    ch = fgetc(stdin);
    if ( isdigit(ch) )
    {
      digits[i++] = ch - 48; // convert ASCII to int
    }
  }
  while (isdigit(ch) && i < sizeof(digits)/sizeof(digits[0]));

【讨论】:

  • 我对这个问题的基本原理有点困惑。由于 char 小于 int,字符串不应该显示更少的数字吗?感觉因为我必须转换为 int 数组,所以首先绕过字符串会更方便。
  • 啊,我明白了我的第一个 scanf 函数是什么意思。这很有意义。但是是否还有一种方法可以只使用 int 数组?如果有一种方法可以在数组中的每个单元格中输入单个数字,那将解决 scanf("%d", &n); 的问题。对?虽然我确实想一次输入一个数字,而不是 digit ENTER digit ENTER 等。如果我使用带有gets()的字符串,那能解决整个输入问题吗?对不起,如果这到处都是。
  • 您可以使用另一个函数来更好地控制输入,例如fgetc 并一次读取一个字符,然后将其转换为 int。
  • 谢谢,昨天终于解决了
【解决方案2】:
#include<stdio.h>
#include<stdlib.h>
int *toArray(long long int n, int counter); int main() {
long long int n=0,copy_of_n=0;
int counter=0, i=0;
int *res=NULL;
printf("Input: ");
scanf("%lld", &n);//Refer note in the answer
copy_of_n=n;//see the while loop why use this.
while(n>0){
     n/=10;//Good, but you are not storing it. copy_of_n does that.
     counter++;//You didn't initialize this
}

res=toArray(copy_of_n, counter);
for(i=counter-1;i>=0;i--){//This ensures you get the numbers in a proper order and since index starts from 0 in the toArray function
     printf("%d", res[i]);
}
printf("\n");
free(res);//You forgot this
return 0; }   

int *toArray(long long int n, int counter) {
  int i=0;
  int *numberArray = malloc(sizeof(int) * counter);
  memset(numberArray,0x00,counter*sizeof(int));//Good to do this
  //Check for memory allocation
  for(i=0; i< counter; i++)
  {   
     numberArray[i] = n % 10;
     n=n/10LL;//You have to remove the digits too
  }
  return numberArray; }

注意:在 while 循环中分小部分读取整数,因为 long long 对它可以存储的位数有限制。另一种方法是存储在一个字符串中并将字符串拆分为最大值。不。可以存储在n 变量中的long long 中的位数并将其逐部分发送到函数。

long long 大小取决于实现,因此请确保检查最大值。不。它可以容纳的位数。(它当然会绕过问题中的 9 位数限制)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2017-10-02
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多