作业帮 > 综合 > 作业

下面这c语句哪错了?为什么将各个for循环拆开,计算值正确,合起来计算的值就完全错误呢

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/17 07:54:10
下面这c语句哪错了?为什么将各个for循环拆开,计算值正确,合起来计算的值就完全错误呢
#include
#include
main()
{
double y,x,e,i,d=3.1415926;
int a=50,b=10,c=60;
for (i=3;i
#include <math.h>
#include <stdio.h>
main()
{
    double  y,x,e,i,d=3.1415926;
    int a=50,b=10,c=60;
    for (i=3; i<=60; i+=0.5)
    {
        x=(a+b*i/c)*sin(i*d/180);
        y=(a+b*i/c)*cos(i*d/180);
        e=i;
        //printf("e=%f,x=%f,y=%f\n",e,x,y);    //Here
        printf("e=%lf,x=%lf,y=%lf\n",e,x,y);
    }
    for (i=3; i<=120; i+=0.5)
    {
        x=(a+b)*sin((i+60)*d/180);
        y=(a+b)*cos((i+60)*d/180);
        e=i+60;
        //printf("e=%d,x=%f,y=%f\n",e,x,y);    //Here
        printf("e=%lf,x=%lf,y=%lf\n",e,x,y);
    }
    for (i=3; i<=30; i+=0.5)
    {
        x=(a+b-2*b*i*i/(c*c))*sin((i+180)*d/180);
        y=(a+b-2*b*i*i/(c*c))*cos((i+180)*d/180);
        e=180+i;
        //printf("e=%d,x=%f,y=%f\n",e,x,y);    //Here
        printf("e=%lf,x=%lf,y=%lf\n",e,x,y);
    }
    for (i=31; i<=60; i+=0.5)
    {
        x=(a+2*b*(c-i)*(c-i)/(c*c))*sin((i+210)*d/180);
        y=(a+2*b*(c-i)*(c-i)/(c*c))*cos((i+210)*d/180);
        e=i+180;
        //printf("e=%d,x=%f,y=%f\n",e,x,y);    //Here
        printf("e=%lf,x=%lf,y=%lf\n",e,x,y);
    }
    for (i=3; i<=120; i+=0.5)
    {
        x=(a)*sin((i+240)*d/180);
        y=(a)*cos((i+240)*d/180);
        e=i+240;
        //printf("e=%d,x=%f,y=%f\n",e,x,y);    //Here
        printf("e=%lf,x=%lf,y=%lf\n",e,x,y);
    }
}有问题的地方如上标here的地方,主要是输出格式控制不对,double要用%lf
再问: 如你所说,改后输出的不是乱码了,但为什么e,x,,y是从208开始输出 的,而不是从3开始输出啊?
再答: 输出缓冲区没有那么大,后面的将前面的刷新了吧可以将后面的注释掉,就可以看到从3开始的了 或者在每个for循环后面加上一句getchar(); 这样每个for循环结束后,按下任意键才继续显示下一个循环的输出。
再问: 每个for循环后加个getchar();就成这样啦
再答: #include <math.h>
#include <stdio.h>
main()
{
    double  y,x,e,i,d=3.1415926;
    int a=50,b=10,c=60;
    for (i=3.0; i<=60; i+=0.5)
    {
        x=(a+b*i/c)*sin(i*d/180);
        y=(a+b*i/c)*cos(i*d/180);
        e=i;
        //printf("e=%f,x=%f,y=%f\n",e,x,y);    //Here
        printf("e=%lf,x=%lf,y=%lf\n",e,x,y);
    }
    getchar();   //---------------Here
    for (i=3; i<=120; i+=0.5)
    {
        x=(a+b)*sin((i+60)*d/180);
        y=(a+b)*cos((i+60)*d/180);
        e=i+60;
        //printf("e=%d,x=%f,y=%f\n",e,x,y);    //Here
        printf("e=%lf,x=%lf,y=%lf\n",e,x,y);
    }
    getchar();//---------------Here
    for (i=3; i<=30; i+=0.5)
    {
        x=(a+b-2*b*i*i/(c*c))*sin((i+180)*d/180);
        y=(a+b-2*b*i*i/(c*c))*cos((i+180)*d/180);
        e=180+i;
        //printf("e=%d,x=%f,y=%f\n",e,x,y);    //Here
        printf("e=%lf,x=%lf,y=%lf\n",e,x,y);
    }
    getchar();//---------------Here
  }要这样加