C 我如何创建一个程序来读取用逗号分隔的无限用户输入字符串?
我有一段代码,需要计算多项式的有理根C 我如何创建一个程序来读取用逗号分隔的无限用户输入字符串?,c,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
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
在某些分隔符处分隔字符串。你可以结合使用这些
接下来,您应该确定如何存储系数。使用诸如coef1
、coef2
等单个实体不是很有用。使用数组
将其付诸实践(尽管是针对浮点数系数),您可以得到:
#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;
}
#包括
#包括
#包括
#定义n10
整数系数(字符*行,双系数[],整数最大值)
{
int n=0;
char*token=strtok(行“,”);
while(令牌){
字符*尾部;
双x=strtod(令牌和尾部);
if(tail==token)返回-1;
如果(n0){
如果(ncoeff>N)ncoeff=N;
coeff_print(coeff,ncoeff);
}
}
返回0;
}
注意事项:
- 数组具有固定数量的条目,
。其中只有N
有效。这是C语言中的一种常用方法,但它意味着您输入的系数最多为ncoeff
N
- 如果你真的想要“无限”的输入,你必须迎合大数组。动态分配可以提供大小可变的数组。这是一个高级话题。(函数
返回系数的实际数目,该数目可能大于数组大小。这在本例中并不有用,但您可以使用此信息扫描coeff_read
的字符串,根据需要进行分配,然后使用分配的数据再次扫描。)max==0
将像sostrtok
这样的连续逗号视为一个单独的分隔符。例如,如果希望空条目表示零,则必须使用另一种方法、、
- 这里还有另一个限制:最大行长度。如果要进行无限或至少很长的输入,则无法确定行长度是否足够。你需要另一个解决方案
- 库函数
在其返回值、尾部指针和库的错误代码strod
中提供了更多信息。你可以利用它来找出这个数字是否超出范围或者后面是否有额外的数字。为了简单(和懒惰),我没有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;
}
#包括
#包括
int getLineOfDubles(文件*流,双*目的,大小\u t长度){
int i=0;
int-ch;
而(1){
while(isspace(ch=fgetc(stream))&&ch!='\n')
;
如果(ch='\n'| ch==EOF)中断;
如果(i>0){
if(ch!=',')返回0;//缺少逗号
while(isspace(ch=fgetc(stream))&&ch!='\n')
;
if(ch='\n'| | ch==EOF)返回0;//缺少数据
}
ungetc(ch、stdin);
双重数据;
if(scanf(“%lf”,&data)!=1)返回0;//错误数据
如果(i==length)返回0;//数据太多
dest[i++]=数据;
}
如果(i>0)返回i;
返回ch==EOF?EOF:0;
}
为什么不使用二维阵列?能否展示一些您需要的输入和输出示例?对于一个新手来说,很难做到这一点。你熟悉malloc
和realloc
吗?哦,我完全忘了二维阵列,谢谢你,阿米库!注意%10[^,],%10[^,],%s“
-->%9[^,],%9[^,],%9s”
(添加空格,从10到9)