打印余弦函数图象//用数学cos函数怎么改?下面是反余弦函数编写的C/C++ code#includestdio.h#includemat
打印余弦函数图象//用数学cos函数怎么改?下面是反余弦函数编写的
- C/C++ code
#include<stdio.h>#include<math.h>int main(){ double y; int x,m; for(y=1;y>=-1;y-=0.1) /*y为列方向,值从1到-1,步长为0.1*/ { m=acos(y)*10; /*计算出y对应的弧度m,乘以10为图形放大倍数*/ for(x=1;x<m;x++) printf(" "); printf("*"); /*控制打印左侧的 * 号*/ for(;x<62-m;x++)printf(" "); printf("*\n"); /*控制打印同一行中对称的右侧*号*/ } return 0; }[解决办法]
- C/C++ code
#include <stdio.h>#include <math.h>#ifndef M_PI#define M_PI 3.14159265358979323846#endif#define W 80#define H 20char s[W][H];double x,y;int i,j;void main() { for (j=0;j<H;j++) for (i=0;i<W;i++) s[i][j]=' '; for (x=0.0;x<=359.0;x+=1.0) { i=(int)(x*W/360.0); if (i<0) i=0; if (i>W-1) i=W-1; y=sin(x*M_PI/180.0); j=(int)(H/2-(H/2-1)*y); if (j<0) j=0; if (j>H-1) j=H-1; s[i][j]='*'; } printf("sin\n"); for (j=0;j<H;j++) { for (i=0;i<W;i++) { printf("%c",s[i][j]); } printf("\n"); } for (j=0;j<H;j++) for (i=0;i<W;i++) s[i][j]=' '; for (x=0.0;x<=359.0;x+=1.0) { i=(int)(x*W/360.0); if (i<0) i=0; if (i>W-1) i=W-1; y=cos(x*M_PI/180.0); j=(int)(H/2-(H/2-1)*y); if (j<0) j=0; if (j>H-1) j=H-1; s[i][j]='*'; } printf("cos\n"); for (j=0;j<H;j++) { for (i=0;i<W;i++) { printf("%c",s[i][j]); } printf("\n"); }}//sin//// *************// *** ***// *** ***// *** ***// ** ***// ** **// *** ***// ** **//** ***//* ** **// ** **// *** ***// ** **// ** ***// *** ***// *** ***// *** ***// *************// *//cos////******* ******// *** ***// *** ***// *** ***// *** **// ** **// *** ***// ** **// ** **// ** ***// ** **// *** ***// ** **// ** ***// *** ***// *** ***// *** ***// *************// *
