将数据存储到数组

问题描述

我是一名自学者,我正在参加免费的在线课程。我正在尝试将 everyOther 的值放入数组,以便稍后访问它。我环顾了互联网,但找不到任何足智多谋的东西。你能告诉我如何将 everyOther 的输出值存储到一个数组中。提前致谢。

#include <cs50.h>
#include <stdio.h>
#include <math.h>

long countDigit(long long n);

int main(void)
{
    long n;

   //This is asking for the input
    do
    {
        n = get_long("Number: ");
    }
    while(!(countDigit(n)>13));

    //Checksum math
    long everyOther = 0;

    while(n > 0)
    {
        long lastNumber = n/10;
        everyOther = lastNumber % 10;
        n = n / 100;
        printf("%li\n",everyOther);
    }

}

//This function helps us with the counting of the number
long countDigit(long long n) {
  return floor(log10(n) + 1);
}

解决方法

C++ 数组,一旦创建成一定长度,就不能扩展或收缩。在最大化空间效率方面,这是一个优势,但通常会限制您的代码。

在您的情况下,由于您正在接受长度不可预测 (>13) 的用户输入,您可能会创建一个没有足够空间的数组。

您有两个选择:

  1. 对用户的输入制定规则并相应地创建数组,例如,如果您确保最多有 20 位数字,则数组长度不能超过 20 位

    long lastNumber = n/10; everyOther = lastNumber % 10;代码。

everyother 放入数组可以做的是首先创建一个长度为例如 20 的数组,通过 long foo [20] 然后每次将所有其他人转移到数组中 foo[i] = everyother 记住将计数器保持在 i 上以迭代到下一个单元格。

  1. 使用vector代替数组,它基本上是动态数组,系统会为您处理空间限制。这不是最有效的,但却是万无一失的。要使用它,首先要使用 #include <vector>,然后简单地用 vector<long> foo; 声明一个向量 然后在 while 循环的每次迭代中用 foo.push_back(everyother) 相加。

要重用这两种方法,只需调用 foo[index_of_position] 来提取存储的所有其他信息。

请记住,您尝试存储在这些结构中的每个数据类型都必须与创建它们的类型相匹配(大多数情况下)。例如,vector<int> 不会接受 string 对象。

,

如果我是你,我会在顶部 #include <vector>,然后声明 std::vector<long> vectorArray;。然后,您可以将值放入向量中,该向量是一个动态大小的数组。应该说,虽然这是一个 C++ 特性,在 C 中不起作用,但由于这篇文章被标记为两者,我假设它会很好。

// #include <cs50.h>
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>

long countDigit(long long n);

int main(void)
{
    long n = 0; // initialise n

    //This is asking for the input
    do
    {
        std::cin >> n; // using this as input
    }
    while(!(countDigit(n)>13));

    //Checksum math
    long everyOther = 0;

    std::vector<long> vectorArray;
    
    while(n > 0)
    {
        long lastNumber = n/10;
        everyOther = lastNumber % 10;
        vectorArray.push_back( everyOther ); // this appends everyOther to the end of the array
        n = n / 100;
        printf("%li\n",everyOther);
    }

    for ( auto x : vectorArray )
        std::cout << x << ' ';      // sample printout of vector's contents
    
}

//This function helps us with the counting of the number
long countDigit(long long n) {
  return floor(log10(n) + 1);
}

,

使用 std::vector

#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <vector>

long countDigit(long long n);

int main(void) {
  long n;

  // This is asking for the input
  do {
    n = get_long("Number: ");
  } while (!(countDigit(n) > 13));

  // Checksum math
  long everyOther = 0;

  std::vector<long> destination;

  while (n > 0) {
    long lastNumber = n / 10;
    everyOther = lastNumber % 10;
    n = n / 100;
    printf("%li\n",everyOther);

    destination.push_back(everyOther);
  }
}

// This function helps us with the counting of the number
long countDigit(long long n) { return floor(log10(n) + 1); }

当您迭代随机数的数字时,很难有效地使用 std::array,因为它的大小在编译时设置。但是,您可以制作一个巨大的数组,并将其用作缓冲区。

#include <array>
#include <cs50.h>
#include <math.h>
#include <stdio.h>

#define BUFFERSIZE 65536

long countDigit(long long n);

int main(void) {
  long n;

  // This is asking for the input
  do {
    n = get_long("Number: ");
  } while (!(countDigit(n) > 13));

  // Checksum math
  long everyOther = 0;

  std::array<long,BUFFERSIZE> destination;

  size_t i = 0;
  while (n > 0) {
    long lastNumber = n / 10;
    everyOther = lastNumber % 10;
    n = n / 100;
    printf("%li\n",everyOther);

    destination[i++] = everyOther;
  }
}

// This function helps us with the counting of the number
long countDigit(long long n) { return floor(log10(n) + 1); }

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...