我对Java一窍不通(。í _ ì。) 2023-12-04 23:44 采纳率: 50%
浏览 8
已结题

帮我看看这个怎那么做,困我俩小时了

哪里有问题,学习通总不通过


#include<stdio.h>
#include<math.h>

void condition1(int a,int b,int c,double* x1,double* x2){
    *x1=(-b+sqrt(b*b-4*a*c))/(2*a);
    *x2=(-b-sqrt(b*b-4*a*c))/(2*a);
}
    
void condition2(int a,int b,int c,double* x1,double* x2){
    *x1=(-b+sqrt(b*b-4*a*c))/(2*a);
    *x2=(-b-sqrt(b*b-4*a*c))/(2*a);
}
void condition3(int a,int b,int c,double* x1,double* x2){
    double real=-b/(2*a);
    double imaginary=sqrt((b*b-4*a*c))/(2*a);
    *x1=real;
    *x2=imaginary;
}
int main(){
    int a,b,c;
    double x1,x2,x;
    scanf("%d %d %d",&a,&b,&c);
    if((b*b-4*a*c)>0){
        condition1(a,b,c,&x1,&x2);
        printf("x1=%.4f,x2=%.4f\n",x1,x2);
    }
    else if((b*b-4*a*c)==0){
        condition2(a,b,c,&x1,&x2);
        printf("x1=x2=%.4f\n",x1);
    }
    else if((b*b-4*a*c)<0){
        condition3(a,b,c,&x1,&x2);
        printf("x1=%.4f+%.4fi,x2=%.4f-%.4fi",x1,x2,x1,x2);
    }
    else if(a==0&&b==0){
        printf("错误!a与b不能同时为0!");
    } 
    else if(a==0){
        x=-c/b;
        printf("x=%.4f",x);
    }
    return 0;
}

求方程ax^2+bx+c=0的根,用3个函数分别求当b^2-4ac>0、=0和<0的情况下方程的根,从主函数输入a、b、c的值并输出结果。

输入格式为:a,b,c(回车)

输出格式:1.两个相同的实数根:x1=x2=(实根1)(回车)

             2.两个不相同的实数根:x1=(实根1),x2=(实根2)(回车)

             3.两个共轭复数根:x1=(实部1)+(虚部1)i,x2=(实部2)-(虚部2)i(回车)

             4.a和b为0:错误!a与b不能同时为05.a为0:x=(根)(回车)

其中,只输出上面冒号后的部分,每个根的数字部分保留4位小数

  • 写回答

2条回答 默认 最新

  • 柯本 2023-12-05 09:02
    关注
    
    
    #include <math.h>
    #include <stdio.h>
    void condition1(double a, double b, double c, double *x1, double *x2)
    {
      *x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
      *x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
    }
    void condition2(double a, double b, double c, double *x1, double *x2)
    {
      *x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
      *x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
    }
    void condition3(double a, double b, double c, double *x1, double *x2)
    {
      double real = -b / (2 * a);
      double imaginary = sqrt((-b * b + 4 * a * c)) / (2 * a); //这个开方里面要取反(因为是负)
      *x1 = real;
      *x2 = imaginary;
    }
    int main()
    {
      double a, b, c; //要求没有说明a,b,c为整形
      double x1, x2, x, p;
      scanf("%lf,%lf,%lf", &a, &b, &c); //要求是以逗号分隔
      p = b * b - 4 * a * c; //为简化运算及程序,这个加个变量
      if (a == 0 && b == 0)  //这个要放最前面
        {
          printf("错误!a与b不能同时为0!\n");
        }
      else if (a == 0) //这个要放在前面
        {
          x = -c / b;
          printf("x=%.4f\n", x);
        }
      else if (p > 0)
        {
          condition1(a, b, c, &x1, &x2);
          printf("x1=%.4f,x2=%.4f\n", x1, x2);
        }
      else if (p == 0)
        {
          condition2(a, b, c, &x1, &x2);
          printf("x1=x2=%.4f\n", x1);
        }
      else if (p < 0)
        {
          condition3(a, b, c, &x1, &x2);
          printf("x1=%.4f%+.4fi,x2=%.4f%+.4fi\n", x1, x2, x1, x2); //符号位直接用格式化符,这样不会有1.0000+-2.0000i
        }
      return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月8日
  • 已采纳回答 12月5日
  • 创建了问题 12月4日

悬赏问题

  • ¥15 拓扑学,凸集,紧集。。
  • ¥15 如何扩大AIS数据容量
  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音