當前位置:編程學習大全網 - 編程軟體 - c語言求方程ax方+bx+c=0的根,用3個函數分別求當b方-4ac大於0,等於0,小於0的根,主函數輸入a,b,c

c語言求方程ax方+bx+c=0的根,用3個函數分別求當b方-4ac大於0,等於0,小於0的根,主函數輸入a,b,c

#include <stdio.h>

#include <math.h>

float x1,x2,disc,p,q;

void 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("equation: %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);

}

}

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);

}

  • 上一篇:報培訓班需要註意什麽?
  • 下一篇:怎麽在linux shell 裏自動輸入壹個數字,比如2,然後自動執行壹個回車?
  • copyright 2024編程學習大全網