c++ - 代码可以编译,但是不起作用! cmd窗口正处于理想状态,但没有任何反应

标签 c++ c

问题是使用函数评估Sin(x)系列。该代码可以成功编译,但无法运行,甚至无法运行。

#include <stdio.h>
#include <math.h>
void sine(int, float *);
void factorial(int, int *);
int main()
{
    int x, fact;
    float sol;
    scanf("%d", &x);
    sine(x, &sol);
    printf("Solution of Sin(x) series is %f.", sol);
    return 0;
}
void sine(int x, float *sol)
{
    int i;
    int fact=1;
    factorial(x, &fact);
    for (i=3;i>0;i+=2)
        *sol=x+pow(-1, i)*pow(x, i)/fact;
}
void factorial(int x, int *fact)
{
    int j;
    for (j=1;j<=x;j++)
        *fact*=j;
}

最佳答案

for函数中的sine周期似乎不正确。您正在检查i > 0,但是将其初始化为3并以2的步长递增。该循环可能仅在i溢出时才中断,但这是未定义的行为(请参阅here)。

关于c++ - 代码可以编译,但是不起作用! cmd窗口正处于理想状态,但没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59382289/

相关文章:

c++ - 用十六进制值替换 std::string 中的字符

c++ - 本地化在静态控制中不起作用

c - 为什么结构体多了 3 个字节?可以去掉吗?

c - C 中的 ADT - 有或没有指针?

c++ - 将点作为参数传递给 C 宏

c++ - 运营商新实现可见性问题

在 C 中创建自定义 DNS 名称服务器

python - C 返回具有错误值的 numpy 数组

字符字符串*与字符字符串[]

c++ - 数组的大小是在编译时确定的吗?