这段代码可以完美编译,但是当我运行它时,在第二次“scanf”时它总是会返回提示,就像它期待无限量的值一样。我在 Linux 上使用 Clang。我真的需要你的帮助,因为我这个星期五要考试。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int fatorial(int n){
int f=1, t;
for(t=n;t>1;t--){
f=f*t;
}
return f;
}
float sen(float x, float tol){
float res=0, aux=0;
int n=0;
for(n=1;res-aux!=tol||aux-res!=tol; n++){
res=aux;
res=res+(pow(-1,n+1))*((pow(x,2*n-1))/fatorial(2*n-1));
}
return res;
}
int main(){
float yo, tol, res;
printf("What's the value of x? ");
scanf(" %f", &yo);
printf("What's the tolerance? ");
scanf(" %f", &tol);
res=sen(yo, tol);
printf("The sin of %.2f is %f.\n", yo, res);
return 0;
}
最佳答案
根据您链接的屏幕截图,您的程序没有要求更多输入。它正在计算一个答案。在 Linux 中,您仍然可以在程序运行时向终端输入内容,这就是您正在做的事情。
您可以使用 top
或其他 CPU 监视器来查看您的进程正在使用 100% 的 CPU。问题是您在 sen()
中的算法正在运行一个无限循环,并且永远不会达到其目标容差值。
关于c - 为什么我的程序一直返回输入提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26980036/