C++ 返回C+中的双精度值+;
我想做一个程序,给我系列1的总和+2/2!+3/3!...........不! 这是我的密码C++ 返回C+中的双精度值+;,c++,visual-c++,C++,Visual C++,我想做一个程序,给我系列1的总和+2/2!+3/3!...........不! 这是我的密码 #include "stdafx.h" #include<conio.h> #include <stdio.h> #include <stdlib.h> int n; double sumseries(double sum, double count) { double fac = 1, i; for (i = 1; i <= count;
#include "stdafx.h"
#include<conio.h>
#include <stdio.h>
#include <stdlib.h>
int n;
double sumseries(double sum, double count)
{
double fac = 1, i;
for (i = 1; i <= count; i++)
fac = fac*i;
sum += (count++ / fac);
if (count <= n)
sumseries(sum, count);
else {
printf("sum of series is:%lf", sum);
return sum;
}
}
int main()
{
printf("enter the limit");
scanf_s("%d", &n);
double z = sumseries(0,1);
printf("\n%lf",z);
system("pause");
}
#包括“stdafx.h”
#包括
#包括
#包括
int n;
双和系列(双和、双计数)
{
双fac=1,i;
对于sumseries
函数中的(i=1;i),并非所有流控制路径都返回值
你至少应该写信
if (count <= n)
return sumseries(sum, count);
if(count你的编译器应该给你一个警告,甚至是一个错误。如果不是,请把警告级别调高
您有一个递归函数sumseries,但并没有从所有通向终点的路径返回值。因此,返回值是不确定的
递归函数不只是返回一次,您必须从每一步返回。在这种情况下,您返回最后一个值,但上一次调用该函数会将其丢弃。您需要继续返回它。因此,您需要使用
return sumseries(sum, count);
继续将值返回到上一步。SUMSSeries()中的控制流可能会结束并返回未定义的值。您需要在代码中避免这种情况。可能需要使用以下语句:
if (count <= n)
return sumseries(sum, count);
if(count)将编译器的警告转到更高的级别。它会立即告诉您。您有递归,但不会在整个链中执行return
s。或者在调试器中逐行运行代码,您将看到发生了什么。我不知道在double上有后缀增量。谁使用它?根据,使用double++
不断地累积错误。
if (count <= n)
return sumseries(sum, count);
if (count <= n)
sumseries(sum, count);