【问题标题】:how to use scanf to scan double and char at the same time into a double array in C如何使用scanf将double和char同时扫描成C中的double数组
【发布时间】:2016-07-08 13:51:17
【问题描述】:

我有一个项目要做,要求我根据用户的输入将两个向量的地址记录到一个双精度数组中。然而。例如,如果用户写了

3  
1 2 3  
3 4 5

这意味着向量是3维的,两个向量是(1,2,3)(3,4,5)。如果用户写,

2  
1 2  
2 3  

这意味着向量是二维的,两个向量是(1,2)(2,3)。我需要将这两个向量的坐标记录到两个双精度数组 x 和 y 中。如何使用 scanf 将坐标读入这两个数组? (我不知道用户是否以正确的格式书写,他们可以在他们应该只写数字的地方写字母或其他符号。如果他们写数字以外的字符,我需要返回 - 1.)

到目前为止我的代码是

double x[100];  
char c;   
c = getchar();  
do {  
scanf("%lf",x)}  
while (c!= '\n');  

【问题讨论】:

  • 您无法使用scanf 检测并从错误的用户输入中恢复。考虑改用fgets
  • 正如我之前建议的那样,您应该使用strtok() 标记将整行拆分为标记的参数,并将它们转换为浮动槽atof()。正如 melpomene 所建议的那样,使用来自stdingets()
  • 但是我需要使用 scanf 来解决这个问题。是否可以先将所有内容(double 和 char)存储到数组中,然后检查数组中的元素是否为数字?
  • 你不能将 double 和 char 保存到一个 double 数组中......也许如果你定义一个字符串数组......
  • 为什么要使用scanf?是作业吗?

标签: c arrays vector


【解决方案1】:

据我了解您的问题,这里有一段代码应该可以解决它。

#include <stdio.h>
#include <stdlib.h>
#define NB_VECTORS 2 //Increase if you have more than 2 vectors

int* readVector(int size) {
    // Allocation fits the size
    int* vector = malloc(size*sizeof(int));
   //While the vector are the same size it works
    for (int i = 0; i < size; i++)
        if (scanf("%d", vector+i) != 1)
             return null; //bad input
    return vector;
}

int main(int argc, char** argv) {
    int size;
    scanf("%d", &size);

    //Each line is vectorized inside vectors[i]
    int* vectors[NB_VECTORS];#
    for (int i = 0; i < NB_VECTORS; i++)
        vectors[i] = readVector(size);

    return 0;
}

[编辑] 返回它已填充的项目数 -> cf http://www.cplusplus.com/reference/cstdio/scanf/

【讨论】:

  • 只是一个简单的问题,“int* readVector(int size)”中的“int size”是什么?
  • 是数组的大小吗?
  • 对,就是数组的大小
【解决方案2】:

也许您只需要这样的东西(简单、最小、无错误检查示例):

int main()
{
  int x[3], y[3];

  int dimension;
  scanf("%d", &dimension);

  if (dimension == 3)
  {
    scanf("%d %d %d", &x[0], &x[1], &x[2]);
    scanf("%d %d %d", &y[0], &y[1], &y[2]);
  }
  else if (dimension == 2)
  {
    scanf("%d %d", &x[0], &x[1]);
    scanf("%d %d", &y[0], &y[1]);
  }

  ...
  return 0;
}

【讨论】:

    【解决方案3】:

    scanf 不是解析用户输入的好函数。它接受2.1sdsa2 作为具有值2.1 的浮点数,它接受2.1sdsa2 作为具有值2 的int。 scanf 仅应在您知道输入有效时使用。

    如果你需要使用scanf,你可以扫描成一个字符串,然后编写你自己的解析来检查输入是否有效。

    一个简单的例子:

    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char s[10];
      while (1 == scanf("%s", s))
      {
         printf("%s\n", s);
         if (strcmp(s, "s") == 0) break;
      }
      return(0);
    }
    

    程序继续执行,直到输入为s

    输出示例:

    1 2.0 2.6
    1
    2.0
    2.6
    2a 4567 2.a23
    2a
    4567
    2.a23
    s
    s
    

    请注意,scanf 在看到空格时会返回。所以输入 1 2 3 将是 3 个循环(也就是返回 3 个子字符串)。

    因此,您可以将解析器放在 while 中,而不只是打印:

      while (1 == scanf("%s", s))
      {
          // Parse the string s and add value to array
      }
    

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 2019-04-07
      相关资源
      最近更新 更多