バーラル @ ウィキ

Cでグラフ!

<stdio.h>
<math.h>

/* 任意の関数のグラフ */

double fun(double x)
{
  double y;
  y = sin(x);
  return y;
}
int main(void){
  double  x, y;
  double  xfrom, xto;
  int	   xsteps;
  double  yfrom, yto;
  int     xidx, yidx;
  printf("*********\n");
  printf("y=sin(x)\nx[rad]\n");
  printf("*********\n");
  printf("x: from to steps> ");
  scanf( "%lf %lf %d", &xfrom, &xto, &xsteps );
  printf("y: from to     > ");
  scanf( "%lf %lf", &yfrom, &yto );
  
  for(xidx=0;xidx<=xsteps;xidx++){
     
     x = xfrom + (xto-xfrom)/xsteps*xidx;
     y = fun(x);
     printf("%8.3lf: ",x);

     if ((y<yfrom)||(y>yto)){
  printf( "\n" );
     }else{
  yidx = (y-yfrom)*50/(yto-yfrom)+0.5;
  while(yidx--){printf( " " );}
  printf("*\n");
     }
  }
  return 0;
}





最終更新:2010年12月14日 22:07