问题是使用函数评估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/