如何读取两行并从输入中读取随机数计数到两个数组中

问题描述 投票:0回答:4

例如输入为:

12 23 32 41 45
22 11 43

行以

'\n'
,

结尾

我想将数字保存到

a[]
b[]
;

a[] = {12, 23, 32, 41, 45}
b[] = {22, 11, 43}

重点是,我不知道每行有多少个数字。

如果我知道 line1 有

n
数字,而 line2 有
m
数字,我将使用
for
循环:

for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);
c scanf
4个回答
1
投票

如果您想继续使用 scanf 方法,我建议使用否定的 scanset

%[^]
格式说明符。

scanf("%[^\n]", pointerToCharArray)

这应该读取任意数量的字符,最多为但不包括指定的字符(在我们的例子中,是换行符)。如果您想放弃换行符,请按如下方式读取:

scanf("%[^\n]\n", pointerToCharArray)

下面可以找到参考页面的链接。否定的扫描集说明符包含在列表中:

http://www.cplusplus.com/reference/cstdio/scanf/

从这一点来看,使用 strtok() 将 scanf 的输出标记为数字数组是一件简单的事情。如果您不熟悉 strtok,下面提供了另一个参考链接:

http://www.cplusplus.com/reference/cstring/strtok/

我希望这有帮助!


0
投票

让我们使用非标准

getline
,一个常见的库扩展。 @Abbott整个行读入分配的内存中。

如果需要数组并且其大小在运行时确定,请使用C99中可用的可变长度数组,也可以选择在C11中使用。

创建一个函数来计数并可能保存数字。使用

strto...()
函数进行稳健的解析。

size_t count_longs(char *line, long *dest) {
  size_t count = 0;
  char *s = line;
  for (;;) {
    char *endptr;
    long num = strtol(s, &endptr, 10);
    if (s == endptr) {
      break;  /// no conversion
    }
    s = endptr;
    if (dest) {
      dest[count] = num;
    }
    count++;
  }
  return count;
} 

示例代码片段

  char *line = NULL;
  size_t len = 0;
  ssize_t nread = getline(&line, &len, stdin);  // read the whole line
  if (nread != -1) {
    // successful read

    long a[count_longs(line, NULL)];  // determine array size and use a 
    count_longs(line, a);             // 2nd pass: assign array elements. 

    nread = getline(&line, &len, stdin);
    if (nread != -1) {
      // successful read

      long b[count_longs(line, NULL)];
      count_longs(line, b);

      // Do something with a and b
    }
  }
  free(line);
  len = 0;

0
投票

下面的代码可能符合OP的要求。 该解决方案读取未指定数量的整数值并将它们存储在

arr
中。要中断循环/结束输入,只需输入一个非整数值。可以进一步指定它来搜索特定的输入等。

int c = 0;
int *arr = NULL;
int *extArr = NULL;
int i = 0;
for (i = 0; scanf("%d", &c) == 1; i++)
{
    extArr = (int*)realloc(arr, (i+1) * sizeof(int)); 
    arr = extArr;
    arr[i] = c; 

}
free(arr);

但是,我个人会混合使用我提供的解决方案和armitus答案(如果我不能使用

getline
或类似的),因为为每个int值重新分配内存对我来说似乎是多余的。

编辑由于对我提供的代码以及如何最好地使用它来满足OP的问题存在一些疑问。 codenippet 应该展示如何创建自己的 scanf 式函数,用于一次读取一个完整数组的未指定数量的整数输入(在这种情况下,必须省略

free()
直到完成数组(显然) ).


0
投票

下面的代码会有点复杂,但我认为这是最容易理解的方法,只需对

char
指针有一点了解即可。

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define ll long long

char* scan_str()
{
    char c, *res = (char*)malloc(1);
    int i = 0;
    while ((c = getc(stdin)) != '\n')
    {
        res[i++] = c;
        res = (char*)realloc(res, i+1);
    }
    res[i] = '\0';
    return res;
}

int main()
{
    //Input for the 1st array - A
    FILE* f1 = fopen("2lines.dat", "w");
    char* s = scan_str();
    fprintf(f1, "%s", s);
    fclose(f1);
    FILE* f2 = fopen("2lines.dat", "r");
    int c = 1;
    for (int i = 0; i < strlen(s); i++)
        if (s[i] == ' ') c++;
    ll a[c];
    for (int i = 0; i < c; i++)
        fscanf(f2, "%lld", a+i);
    fclose(f2);
    //Input for the 2nd array - B
    f1 = fopen("2lines.dat", "w");
    *s = scan_str();
    fprintf(f1, "%s", s);
    fclose(f1);
    f2 = fopen("2lines.dat", "r");
    c = 1;
    for (int i = 0; i < strlen(s); i++)
        if (s[i] == ' ') c++;
    ll b[c];
    for (int i = 0; i < c; i++)
        fscanf(f2, "%lld", b+i);
    fclose(f2);
    return 0;
}

但我不知道在

main
函数中定义数组是否会限制内存存储,因此非常感谢更多建议。

© www.soinside.com 2019 - 2024. All rights reserved.