繁体   English   中英

如何创建一个程序,该程序将读取c中用逗号分隔的无限用户输入字符串?

[英]How do I create a program that will read infinite user input strings separated by commas in c?

我有这段代码需要计算多项式的有理根

int n,co;
char coef1[10],coef2[10],coef3[10],coef4[10];

printf("Enter the highest degree of the input polynomial:");
scanf("%d",&n);

co=n+1;

printf("Enter %d integer coefficients starting from the 0th degree\n", co);

printf("Separate each input by a comma:");

此部分仅用作测试,当输入的程度为2时,是否可以正确读取用户输入

scanf("%10[^,],%10[^,],%s",coef1,coef2,coef3,coef4);

printf("%s %s %s",coef1,coef2,coef3);

我的问题是如何打印%10 [^,]的次数与用户输入的n相同(对于无限输入来说应该是可能的),并在末尾添加%s。 另外,即使我这样做,我也需要一种方法来声明coef1,coef2等与co相同的次数。

不清楚您想要什么。 我假设您想读取一组系数并跟踪输入了多少个系数。

scanf不适合这种方法,因为您已经必须指定要在格式文件中读取多少个参数。 scanf也不识别新行,因此如果没有巴洛克式格式语法,就无法读取基于行的格式。 fgets读取整行。 strtok在某些分隔符处分隔字符串。 您可以结合使用这些。

接下来,您应该确定如何存储系数。 使用像coef1coef2等单个实体不是很有用。 使用数组。

将其付诸实践(尽管有浮点系数),您将获得:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define N 10

int coeff_read(char *line, double  coeff[], int max)
{
    int n = 0;
    char *token = strtok(line, ",");

    while (token) {
        char *tail;
        double x = strtod(token, &tail);

        if (tail == token) return -1;

        if (n < max) coeff[n] = x;
        n++;

        token = strtok(NULL, ",");
    }

    return n;
}

void coeff_print(const double *coeff, int n)
{
    for (int i = 0; i < n; i++) {
        if (i) printf(", ");
        printf("%g", coeff[i]);
    }
    puts("");
}

int main()
{
    char line[200];
    double coeff[N];
    int ncoeff;

    while (fgets(line, sizeof(line), stdin)) {
        ncoeff = coeff_read(line, coeff, N);

        if (ncoeff < 0) {
            puts("Invalid input.");
            continue;
        }

        if (ncoeff > 0) {
            if (ncoeff > N) ncoeff = N;
            coeff_print(coeff, ncoeff);
        }
    }

    return 0;
}

注意事项:

  • 该数组具有固定数量的条目N 其中只有ncoeff有效。 这是C语言中的常用方法,但是这意味着您输入的内容最多具有N系数。
  • 如果您确实想要“无限”输入,则必须迎合大型数组。 动态分配可以提供可变大小的数组。 这是一个高级话题。 (函数coeff_read返回实际的系数数目,该数目可能大于数组的大小。在此示例中这没有用,但是您可以使用此信息来扫描max == 0的字符串,进行适当分配,然后再次进行扫描分配的数据。)
  • strtok认为像这样连续的逗号的运行,,,作为单个分离器。 例如,如果您希望空条目表示零,则必须使用另一种方法。
  • 这里还有另一个限制:最大行长。 如果要输入无限或至少非常长的输入,则不能确保行长足够。 您将需要其他解决方案。
  • 库函数strtod在其返回值,尾指针和库的错误代码errno提供了更多信息。 您可以利用它来确定数字是否超出范围或后面是否有多余的字符。 为了简单起见(和懒惰),我没有。
  • 您需要整数而不是浮点数。 库函数strtol是与strtod等效的整数。

不建议无限输入,因为这是一个安全漏洞。 而是允许许多。

要在一行中使用scanf("%lf", &data) ,首先需要在其他代码中查找'\\n'

#include <ctype.h>
#include <stdio.h>

int GetLineOfDoubles(FILE *stream, double *dest, size_t length) {
  int i = 0;
  int ch;

  while (1) {
    while (isspace(ch = fgetc(stream)) && ch != '\n')
      ;
    if (ch == '\n' || ch == EOF) break;

    if (i > 0) {
      if (ch != ',') return 0; // Missing comma

      while (isspace(ch = fgetc(stream)) && ch != '\n')
        ;
      if (ch == '\n' || ch == EOF) return 0;  // Missing data
    }

    ungetc(ch, stdin);

    double data;
    if (scanf("%lf", &data) != 1) return 0; // Bad data
    if (i == length) return 0; // Too much data
    dest[i++] = data;
  }

  if (i > 0) return i;
  return ch == EOF ? EOF : 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM