我需要一种在C中读取两次浮动的方法。每次都以<;Ctrl>-D
我需要读入多项式的系数(作为浮点数),并用ctrl-d标记结束。 然后我需要读入x值并显示f(x)。也可以用ctrl-d结束 到目前为止,我已经用我需要一种在C中读取两次浮动的方法。每次都以<;Ctrl>-D,c,scanf,stdin,eof,C,Scanf,Stdin,Eof,我需要读入多项式的系数(作为浮点数),并用ctrl-d标记结束。 然后我需要读入x值并显示f(x)。也可以用ctrl-d结束 到目前为止,我已经用scanf函数进行了尝试。读取系数效果很好,但在第一次键入ctrl-d后,scanf将无法读取x值 #include <stdio.h> int main(){ int count = 0; float poly[33]; while(scanf("%f", &poly[count]) != EOF){
scanf
函数进行了尝试。读取系数效果很好,但在第一次键入ctrl-d后,scanf将无法读取x值
#include <stdio.h>
int main(){
int count = 0;
float poly[33];
while(scanf("%f", &poly[count]) != EOF){ // reading the coeffs
count++;
}
printf("Bitte Stellen zur Auswertung angeben\n");
float x;
float res;
while(scanf("%f", &x) != EOF){ //Here it Fails. Since scanf still sees the EOF from before
res = 0;
for(int i = 1; i < count; i++){
res += poly[i-1] * x + poly[i];
}
printf("Wert des Polynoms an der Stelle %f: %f\n", x, res);
}
}
#包括
int main(){
整数计数=0;
浮动多边形[33];
而(scanf(“%f”,&poly[count])!=EOF){//读取系数
计数++;
}
printf(“bite Stellen zur Auswertung angeben”);
浮动x;
浮动res;
而(scanf(“%f”,&x)!=EOF){//在这里它失败了。因为scanf仍然可以看到以前的EOF
res=0;
对于(int i=1;i
重新打开stdin
可以在第一次循环后工作
freopen(NULL, "rb", stdin);
或考虑<代码> CeleRr(STDIN)< /COD>
的思想。使用Enter而不是使用Ctrld来结束输入(这将关闭stdin),怎么样 创建一个函数来读取一行
浮点值
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
int read_rest_of_line(FILE *stream) {
int ch;
do {
ch = fgetc(stream);
} while (ch != '\n' && ch != EOF);
return ch;
}
// Read a line of input of float`s. Return count
int read_line_of_floats(float *x, int n) {
bool char_found = false;
int count;
for (count = 0; count < n; count++) {
// Consume leading white-space looking for \n - do not let "%f" do it
int ch;
while (isspace((ch = getchar()))) {
char_found = true;
if (ch == '\n') {
return count;
}
}
if (ch == EOF) {
return (count || char_found) ? count : EOF;
}
ungetc(ch, stdin);
if (scanf("%f", &x[count]) != 1) {
read_rest_of_line(stdin);
return count;
}
}
read_rest_of_line(stdin);
return count;
}
在第一次循环后,重新打开标准DIN可以工作
freopen(NULL, "rb", stdin);
或考虑<代码> CeleRr(STDIN)< /COD>
的思想。使用Enter而不是使用Ctrld来结束输入(这将关闭stdin),怎么样 创建一个函数来读取一行
浮点值
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
int read_rest_of_line(FILE *stream) {
int ch;
do {
ch = fgetc(stream);
} while (ch != '\n' && ch != EOF);
return ch;
}
// Read a line of input of float`s. Return count
int read_line_of_floats(float *x, int n) {
bool char_found = false;
int count;
for (count = 0; count < n; count++) {
// Consume leading white-space looking for \n - do not let "%f" do it
int ch;
while (isspace((ch = getchar()))) {
char_found = true;
if (ch == '\n') {
return count;
}
}
if (ch == EOF) {
return (count || char_found) ? count : EOF;
}
ungetc(ch, stdin);
if (scanf("%f", &x[count]) != 1) {
read_rest_of_line(stdin);
return count;
}
}
read_rest_of_line(stdin);
return count;
}
您应该使用
clearerr(stdin)
清除标准输入上的EOF和错误条件,这将允许您读取更多数据。输入的设计不是很合理。除此之外,您如何从文件中提供信息简短的回答:你不能!如果您总是必须从终端键入数据,那么编写测试会变得非常乏味。重新思考设计。要求多项式的所有系数都在一条线上。读取行(fgets()
,例如)并转换数字()。然后读取值。或者要求先计算系数,然后再计算那么多系数,然后输入的其余部分是要计算的x
值。或者别的什么!您应该使用clearerr(stdin)
清除标准输入上的EOF和错误条件,这将允许您读取更多数据。输入的设计不是很合理。除此之外,您如何从文件中提供信息简短的回答:你不能!如果您总是必须从终端键入数据,那么编写测试会变得非常乏味。重新思考设计。要求多项式的所有系数都在一条线上。读取行(fgets()
,例如)并转换数字()。然后读取值。或者要求先计算系数,然后再计算那么多系数,然后输入的其余部分是要计算的x
值。或者别的什么!使用freopen()
是一种非常重要的方法,如果输入是文件而不是终端,则无法正常工作。但是,如果输入方案是文件而不是终端,则通常无法正常工作。使用freopen()
是一种非常重要的方法,如果输入方案是文件而不是终端,则无法正常工作。但是,如果输入方案是一个文件而不是一个终端,那么它通常不会正常工作。