c - 为什么我的程序一直返回输入提示?

标签 c linux clang prompt

这段代码可以完美编译,但是当我运行它时,在第二次“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/

相关文章:

c - 使用 C 与 Shell 交互

使用 gcc 编译 Linux 设备驱动模块

c - 如何仅使用 linux 系统调用将两个文件合并为第三个文件?

c - 如何从 AVX 内在函数中获得用于计算基本统计数据的性能提升?

c - 从函数返回数组地址

c - 以 "int x[][n]"作为参数的函数

c - 返回指针的函数

linux - 使用变量运行 GNU 并行命令会导致冲突吗?

c++ - clang libc++ 错误 : overload resolution selected implicitly-deleted copy assignment operator

c++ - 为什么 Clang 优化这段代码?