一元二次方程求解(C語言),包含複數情況都是

plaet發表於2020-12-31

一元二次方程 ax²+bx+c=0 的根包含3種情況(這裡僅討論a!=0的情況)

#include<stdio.h>
#include<math.h>
void ax(int a,int b,int c); //δ>0void bx(int a,int b,int c); //δ=0void cx(int a,int b,int c); //δ<0int main(){    int a,b,c;    scanf("%d%d%d",&a,&b,&c);    if(a<0) {        a=-a;        b=-b;        c=-c;    }    if(b*b-4*a*c>0)        ax(a,b,c);    if(b*b-4*a*c==0)        bx(a,b,c);    if(b*b-4*a*c<0)        cx(a,b,c);    return 0;}void ax(int a,int b,int c) {    double x0,x1,x2;    x0 = sqrt(b*b-4*a*c);    x1=(-b+x0)/(2.0*a);    x2=(-b-x0)/(2.0*a);    printf("x1=%.3f x2=%.3f\n",x1,x2);}void bx(int a,int b,int c) {    double x = -b/(2.0*a);    printf("x1=%.3f x2=%.3f\n",x,x);}void cx(int a,int b,int c) {    double m = -b/(2.0*a);    double n=sqrt(-(b*b-4*a*c))/(2.0*a);    printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi\n",m,n,m,n);}

相關文章