首页 > 试题广场 >

求方程ax2+bx+c=0的根,用3个函数分别求当:b2-4

[问答题]

求方程ax2+bx+c=0的根,用3个函数分别求当:b2-4ac

大于0、等于0和小于0

时的根并输出结果。从主函数输入abc的值。

推荐

#include<stdio.h>

#include<math.h>

float x1,x2,disc,p,q;

int main()

{void greater_than_zero(float,float);

void equal to_zero(float,float);

void smaller_than_zero(float,float);

float a,b,c;

printf("input a,b,c:");

scanf("%f,%f,%f",&a,&b,&c);

printf(“equatiom:%5.2f*x*x+%5.2f*x+%5.2f=0\n",a,b,c);

disc=b*b-4*a*c;

printf("root\n”);

if(disc>0)

{

greater_than_zero(a,b);

printf("x1=%f\t\tx2=%f\n",x1,x2);

}

else if(disc==0)

{equal_to_zero(a,b);

printf("x1=%f\t\tx2=%f\n",x1,x2);

}

else

{smaller_than_zero(a,b);

printf("x1=%f+%fi\tx2=%f-%fi\n",p,q,p,q);

}

return 0;

}


void greater_than_zero(float a,float b)

{x1=(-b+sqrt( disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

}


void equal_to_zero(float a,float b)

{

x1=x2=(-b)/(2*a);

}


void smaller_than_zero(float a.,float b)

{

p=-b/(2*a);

q=sqrt(-disc)/(2*a);

}


发表于 2018-03-25 10:39:51 回复(0)
#include <stdio.h>
#include <math.h>

float disc;//判别式    disc = b^2 - 4ac
float x1,x2;
float p,q; // 俩个虚根
void greater_than_zero(int a,int b);
void equal_to_zero(int a,int b);
void less_than_zero(int a,int b);
int main() {
    int a,b,c;
    printf("请输入a,b,c的值");
    scanf("%d %d %d",&a,&b,&c);
    disc = b*b - 4*a*c;//计算判别式
    if(disc >0){
        greater_than_zero(a,b);
        printf("disc>0的俩个根为:x1 = %2f,x2=%2f\n",x1,x2);
    }else if(disc == 0){
        equal_to_zero(a,b);
        printf("disc=0的俩个根为:x1 = %2f,x2=%2f\n",x1,x2);
    }else{ // disc<0
        less_than_zero(a,b);
        printf("disc<0的俩个根为:x1 = %2f,x2=%2f\n",p+q,p-q);
    };
    return 0;
}
//>0
void greater_than_zero(int a,int b){
    //x1 = (-b + sqrt(disc)) / 2a
    //x2 = (-b - sqrt(disc)) / 2a
    x1 = (-b + sqrt(disc)) / 2*a;
    x2 = (-b - sqrt(disc)) / 2*a;
}
//=0
void equal_to_zero(int a,int b){
    // x1 = x 2 = -b/2a
    x1 = x2 = -b/2*a;
}
//<0
void less_than_zero(int a,int b){
    //p = -b/2a
    //q = sqrt(-disc) / 2a
    p = -b/2*a;
    q = sqrt(-disc) / 2*a;
}

发表于 2022-10-23 10:56:04 回复(0)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double* result(double a, double b, double c){
 double* result = (double* )malloc(sizeof(double) * 2);
 double det = b * b - 4 * a * c;
 if(det < 0)
  return NULL;
 else if(det == 0){
  double res = -1 * b / (2 * a);
  * (result + 0) = res;
  * (result + 1) = res;
  return result;
 }
 else{
  * (result + 0)= (-1 * b + sqrt(det))/(2 * a);
  * (result + 1) = (-1 * b - sqrt(det))/(2 * a);
  return result;
 }
}
int main(){
 double a, b, c;
 scanf("%lf %lf %lf", &a, &b, &c);
 double* res = result(a,b,c);
 if(* (res + 0) != * (res + 1))
  printf("%.2lf %.2lf\n", *(res + 0), *(res + 1));
 else
  printf("%.2lf\n", *(res + 0));
}
发表于 2020-01-01 20:12:33 回复(0)