鱼C论坛

 找回密码
 立即注册
查看: 615|回复: 2

[已解决]求方程根问题

[复制链接]
发表于 2020-7-22 10:32:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是源码,运行时说else前没有if,麻烦大家看一下


#include <stdio.h>
#include <math.h>
int main()
{
        double a,b,c,disc,x1,x2,realpart,imagepart;
        scanf("%lf%lf%lf",&a,&b,&c);
        printf("This is a equation");
        if(fabs(a)<=1e-6)
        {
                printf("is not a quadratic");
        }
        else
        {
                disc=b*b-4*a*c;
                if(fabs(disc)<=1e-6)
                {
                        printf("has two equal roots:%8.4f\n",-b/(2*a));
                }else
                {
                        if(fabs(disc)==1e-6)
                        {
                                printf("have no roots:%8.4f\n",-b/(2*a));
                        }
                }else                                                ----------------------这里报错了
                {
                        if(disc>1e-6)
                        {
                                x1=(-b+sqrt(disc)/(2*a));
                                x1=(-b-sqrt(disc)/(2*a));
                                printf("has distinct real roots:%8.4f and %8.4f\n",x1,x2);
                        }
                }else                                                ---------------------这也报错了
                {
                        realpart=-b/(2*a);
                        imagepart=sqrt(-disc)/(2*a);
                        pritnf("has complex roots:\n");
                        printf("%8.4f+%8.4fi\n",realpart,imagepart);
                        printf("%8.4f-%8.4fi\n",realpart,imagepart);
                }
        }
        return 0;
}
最佳答案
2020-7-22 10:48:07
你的if...else... 用法是错的。
有if…else if…else if…else…的用法
没有if…else…else…else…的用法
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     double a,b,c,disc,x1,x2,realpart,imagepart;
  6.     scanf("%lf%lf%lf",&a,&b,&c);
  7.     printf("This is a equation ");
  8.     if(fabs(a)<=1e-6)
  9.     {
  10.         printf("is not a quadratic");
  11.     }
  12.     else
  13.     {
  14.         disc=b*b-4*a*c;
  15.         if(fabs(disc)<=1e-6)
  16.         {
  17.             printf("has two equal roots:%8.4f\n",-b/(2*a));
  18.         }else if(fabs(disc)==1e-6)
  19.         {
  20.             printf("have no roots:%8.4f\n",-b/(2*a));
  21.         }
  22.         else if(disc>1e-6)
  23.         {
  24.             x1=(-b+sqrt(disc)/(2*a));
  25.             x2=(-b-sqrt(disc)/(2*a));
  26.             printf("has distinct real roots:%8.4f and %8.4f\n",x1,x2);
  27.         }
  28.         else  //                                              ---------------------这也报错了
  29.         {
  30.             realpart=-b/(2*a);
  31.             imagepart=sqrt(-disc)/(2*a);
  32.             printf("has complex roots:\n");
  33.             printf("%8.4f+%8.4fi\n",realpart,imagepart);
  34.             printf("%8.4f-%8.4fi\n",realpart,imagepart);
  35.     }
  36.     }
  37.     return 0;
  38. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-7-22 10:48:07 | 显示全部楼层    本楼为最佳答案   
你的if...else... 用法是错的。
有if…else if…else if…else…的用法
没有if…else…else…else…的用法
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     double a,b,c,disc,x1,x2,realpart,imagepart;
  6.     scanf("%lf%lf%lf",&a,&b,&c);
  7.     printf("This is a equation ");
  8.     if(fabs(a)<=1e-6)
  9.     {
  10.         printf("is not a quadratic");
  11.     }
  12.     else
  13.     {
  14.         disc=b*b-4*a*c;
  15.         if(fabs(disc)<=1e-6)
  16.         {
  17.             printf("has two equal roots:%8.4f\n",-b/(2*a));
  18.         }else if(fabs(disc)==1e-6)
  19.         {
  20.             printf("have no roots:%8.4f\n",-b/(2*a));
  21.         }
  22.         else if(disc>1e-6)
  23.         {
  24.             x1=(-b+sqrt(disc)/(2*a));
  25.             x2=(-b-sqrt(disc)/(2*a));
  26.             printf("has distinct real roots:%8.4f and %8.4f\n",x1,x2);
  27.         }
  28.         else  //                                              ---------------------这也报错了
  29.         {
  30.             realpart=-b/(2*a);
  31.             imagepart=sqrt(-disc)/(2*a);
  32.             printf("has complex roots:\n");
  33.             printf("%8.4f+%8.4fi\n",realpart,imagepart);
  34.             printf("%8.4f-%8.4fi\n",realpart,imagepart);
  35.     }
  36.     }
  37.     return 0;
  38. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-22 11:14:13 | 显示全部楼层
谢谢您的回答,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 20:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表