这个流程图对吗?

Is this flowchart right?

我开发了一个 C 程序,可以使用泰勒级数展开计算 sin 函数的值。我还为该程序绘制了流程图。源代码如下:

#include<stdio.h>
#include<math.h>

int fact(int n)
{
 if(n==0)
  {
    return 1;
  }
 else
    return n*fact(n-1);
}

int main()
{
 int l,i,t=1;
 float deg,rad,val=0;

 printf("Enter degree of sin: ");
 scanf("%f",&deg);

 printf("Enter limit of Taylor series: ");  
 scanf("%d",&l);                            
 rad = (deg*3.142857)/180;                  

 for(i=1;i<=l;i+=2)
  {
   val = val + (t*pow(rad,i)/fact(i));     
   t = t*(-1);                         
  }

 printf("\nValue calculated by program, using Taylor Series:\n");
 printf("Sin(%f) = %f\n",deg,val);
 printf("\nValue calculated using library function:\n");
 printf("Sin(%f) = %f\n",deg,sin(rad));

 getch();
 return 0;
}

下面是程序的流程图:

那么这个流程图适合这个程序吗?有没有错误?我刚学编程,对画流程图不是很了解

不,两种情况下的流程图都是错误的,因为 1) for 循环在 main 函数体中而不是在 fact 函数体中。 2) 对于事实递归函数,正确的流程聊天将在这里:http://improvec.blogspot.in/2010/12/flow-chart-for-recursive-function-of.html

现在,我知道你知道for循环的基本流程图connect 主函数再试一次,把两个流程图连接起来。

使用阶乘函数的风险是它很快就会超出 int 范围。不需要幂函数或阶乘函数,因为泰勒级数的每一项都可以通过乘法和除法从前一项导出。

乘数不言自明,就是角度的平方。

除数是 i * (i - 1),阶乘的后两项。

您会看到我已经删除了您的符号更改因子 t,因为要将前一项的符号从负号更改为正号,或将正号更改为负号,您只需乘以 -1。但是我什至通过使用 (1 - i).

反转 (i - 1) 的符号来删除它

这个系列的第一个术语就是 rad 所以我从那个开始。

#include <stdio.h>
#include <math.h>

int main()
{
    int n, i;                                       // don't use `l` for a variable name 
    float deg, rad, radsq, val, term;

    printf("Enter degree of sin: ");
    if(scanf("%f", &deg) != 1) {
        return 1;                                   // or other error handling
    }

    printf("Enter limit of Taylor series: ");  
    if(scanf("%d", &n) != 1) {                           
        return 1;                                   // or other error handling
    }
    rad = deg * 3.14159265f / 180;                  // proper value for pi
    radsq = rad * rad;

    term = rad;                                     // first term is rad
    val = term;                                     // so is series sum
    for(i = 3; i <= n; i += 2)                      // we've done the first term
    {
        term *= radsq / (i * (1 - i));              // see explanation
        val += term;                                // sum the series
    }

    printf("\nValue calculated by program, using Taylor Series:\n");
    printf("Sin(%f) = %f\n", deg, val);
    printf("\nValue calculated using library function:\n");
    printf("Sin(%f) = %f\n", deg, sin(rad));

    return 0;
}