首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

打印函数图象//用数学cos函数不行?解决方案

2012-08-10 
打印函数图象//用数学cos函数不行?????C/C++ code#includestdio.h#includemath.hint main(){double x

打印函数图象//用数学cos函数不行?????

C/C++ code
#include<stdio.h>#include<math.h>int main(){     double x;     int y,m;     for(x=1;x>=-1;x-=0.1)     {        m=cos(x);        for(y=1;y<m;y++)            printf(" ");        printf("*");        for(;y<62-m;y++)            printf(" ");        printf("*\n");     }}


[解决办法]
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,i1;void main() {    for (j=0;j<H;j++)        for (i=0;i<W;i++)            s[i][j]=' ';    i1=0;    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;        if (i1!=i) {            s[i][j]='*';            i1=i;        }    }    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]=' ';    i1=0;    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;        if (i1!=i) {            s[i][j]='*';            i1=i;        }    }    printf("cos\n");    for (j=0;j<H;j++) {        for (i=0;i<W;i++) {            printf("%c",s[i][j]);        }        printf("\n");    }}//sin////              *************//            **             **//          **                 **//        **                     **//      **                         **//     *                             *//   **                               **//  *                                   *// *                                     **//                                         *                                     *//                                          *                                   *//                                           **                               **//                                             *                             *//                                              **                         **//                                                **                     **//                                                  **                 **//                                                    **             **//                                                      ****** ******//                                                            *//cos//// ******                                                                   ******//       **                                                               **//         **                                                           **//           **                                                       **//             **                                                   **//               *                                                 *//                **                                             **//                  *                                           *//                   *                                         *//                    **                                     **//                      *                                   *//                       **                               **//                         *                             *//                          **                         **//                            **                     **//                              **                 **//                                **             **//                                  ****** ******//                                        * 

热点排行