注册 登录
编程论坛 C++教室

帮忙看看错误啊

寻竹而过 发布于 2012-10-25 18:59, 515 次点击
#include<stdio.h>
#include<math.h>
int main()
{
    double a,b,c,d,e,x1,x2;
    while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
    {   
    d=b*b-4*a*c;
    if(d>0)
    {
    e=sqrt(d);
    x1=(-b+e)/(2*a);
    x2=(-b-e)/(2*a);
    printf("x1=%.3f,x2=%.3f\n",x1,x2);
    }
    else if(d=0)
    {
    x1=(-b)/(2*a);
    printf("x1=x2=%.3f\n",x1);
    }
    else
    {
    e=sqrt(-d)
    x1=(-b+i*e)/(2*a);
    x2=(-b-i*e)/(2*a);
    printf("x1=%.3f,x2=%.3f\n",x1,x2);
    }
    }
return 0;
}
10 回复
#2
stuniversity2012-10-25 19:09
#include<stdio.h>
#include<math.h>
int main()
{
    double a,b,c,d,e,x1,x2;
    while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
    {   
    d=b*b-4*a*c;
    if(d>0)
    {
    e=sqrt(d);
    x1=(-b+e)/(2*a);
    x2=(-b-e)/(2*a);
    printf("x1=%.3f,x2=%.3f\n",x1,x2);
    }
    else if(d=0)
    {
    x1=(-b)/(2*a);
    printf("x1=x2=%.3f\n",x1);
    }
    else
    {
    e=sqrt(-d);    //这里要加分号
    x1=(-b+e)/(2*a);    //去掉i
    x2=(-b-e)/(2*a);    //去掉i
    printf("x1=%.3f,x2=%.3f\n",x1,x2);
    }
    }
return 0;
}
#3
寻竹而过2012-10-25 19:32
去掉i那不是不对了吗,求解释
#4
寻竹而过2012-10-25 19:37
试了,但结果是
编译错误信息:

--------------------------------------------------------------------------------

Main.cpp: In function 'int main()':
Main.cpp:16: warning: suggest parentheses around assignment used as truth value
Main.cpp:24: error: expected `:' before ';' token
Main.cpp:24: error: expected primary-expression before ';' token
#5
newdos2012-10-26 11:28
你的i变量在哪里声明的?

else if(d=0) ---》》》》 else if( d == 0 )
#6
liman1232012-10-26 13:02
#include<stdio.h>
 #include<math.h>
 int main()
 {
     double a,b,c,d,e,x1,x2;
     while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
     {   
     d=b*b-4*a*c;
     if(d>0)
     {
     e=sqrt(d);
     x1=(-b+e)/(2*a);
     x2=(-b-e)/(2*a);
     printf("x1=%.3f,x2=%.3f\n",x1,x2);
     }
     else if(d=0)
     {
     x1=(-b)/(2*a);
     printf("x1=x2=%.3f\n",x1);
     }
     else
     {
     e=sqrt(-d);//这儿差分号!
     x1=(-b+i*e)/(2*a);//i是干啥的?
     x2=(-b-i*e)/(2*a);
     printf("x1=%.3f,x2=%.3f\n",x1,x2);
     }
     }
 return 0;
 }
#7
未名湖的云2012-10-26 21:13
首先求解一元二次方程,这个i(虚数)不需要再函数里出现,你这里出现了没有定义肯定错误,楼主只需要在输出的时候把i加上去就可以了~~~~~
#include<stdio.h>
#include<math.h>
int main()
{
    double a,b,c,d,e,x1,x2,p,q;
    while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
    {   
    d=b*b-4*a*c;
    if(d>0)
    {
    e=sqrt(d);
    x1=(-b+e)/(2*a);
    x2=(-b-e)/(2*a);
    printf("x1=%.3f,x2=%.3f\n",x1,x2);
    }
    else if(d=0)
    {
    x1=(-b)/(2*a);
    printf("x1=x2=%.3f\n",x1);
    }
    else
    {
    e=sqrt(-d);    //这里要加分号,楼上说得对
    x1=(-b+e)/(2*a);    //去掉这部分
    x2=(-b-e)/(2*a);    //去掉这部分
    p=(-b)/(2*a);
    q=e/(2*a);      
    printf("x1=%.3f+%.3fi,x2=%.3f-%.3fi\n",p,q,p,q);//这样     我是这样做的~~~~~
    }
    }
return 0;
}
#8
寻竹而过2012-10-27 15:42
这样啊
#9
未名湖的云2012-10-27 15:55
回复 8楼 寻竹而过
你想想看啊~~~~~这就明白了~~~
#10
寻竹而过2012-10-27 18:02
#include<stdio.h>
#include<math.h>
int main()
{
    double a,b,c,d,e,x1,x2,m,n;
    while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
    {
        d=b*b-4*a*c;
        if(d>0)
        {
        e=sqrt(d);
        x1=(-b+e)/(2*a);
        x2=(-b-e)/(2*a);
        printf("x1=%.3f,x2=%.3f\n",x1,x2);
        }
        else if(d<0)
        {
            e=sqrt(-d);
            m=-b/(2*a);
            n=e/(2*a);
            printf("x1=%.3f+%.3fi,x2=%.3f-%.3fi\n",m,n,m,n);
        }
        else
        {
            x1=(-b)/(2*a);
            printf("x1=x2=%.3f\n",x1);
        }
        
    }
        return 0;
}
那先在呢?我还是通不过啊
#11
寻竹而过2012-10-27 18:05
额,是x1x2之间的空格没弄
谢了啊
1