其他分享
首页 > 其他分享> > 求解二次函数

求解二次函数

作者:互联网

代码如下:

 1 //二次函数求解
 2 # include<stdio.h>
 3 # include<math.h>
 4 float a,b,c;
 5 struct d{
 6     double x1;
 7     double x2;
 8 };
 9 d R;
10 d func1(float,float,float);
11 double func2(float,float);
12 d func3(float,float,float);
13 int main()
14 {
15     scanf("%f%f%f",&a,&b,&c);
16     if(fabs(a)<1e-6)
17     {
18         printf("非二次函数\n");
19         return 0;
20     }
21     if(pow(b,2)-4*a*c>0)
22     {
23         printf("x1=%7.2f\nx2=%7.2f\n",func1(a,b,c).x1,func1(a,b,c).x2);
24     }
25     else if(fabs(pow(b,2)-4*a*c)<1e-6)
26     {
27         printf("x1=x2=%7.2f\n",func2(a,b));
28     }
29     else
30     {
31         printf("x1=%7.2f+%7.2fi\nx2=%7.2f-%7.2fi\n",func3(a,b,c).x1,func3(a,b,c).x2,func3(a,b,c).x1,func3(a,b,c).x2);
32     }
33 
34     return 0;
35 }
36 
37 d func1(float a,float b,float c)
38 {
39     d r;
40     double p1=-b/(2.0*a);
41     double p2=sqrt(pow(b,2)-4*a*c)/(2.0*a);
42     r.x1=p1+p2;
43     r.x2=p1-p2;
44     return r;
45 }
46 
47 double func2(float a,float b)
48 {
49     return -b/(2.0*a);
50 }
51 
52 d func3(float a,float b,float c)
53 {
54     d r;
55     double p1=-b/(2.0*a);
56     double p2=sqrt(-(pow(b,2)-4*a*c))/(2.0*a);
57     r.x1=p1;
58     r.x2=p2;
59     return r;
60 }

运行结果:

 

标签:func1,函数,二次,double,float,7.2,求解,x1,f%
来源: https://www.cnblogs.com/bboykaku/p/12448149.html