Giai phuong trinh bac 2
1.
// Phương trình bậc 2 ax^2 + bx + c = 0
2.
#include <math.h> // Sử dụng sqrt
3.
void GiaiPTBacHai(int a, int b, int c)
4.
{
5.
int D = b*b - 4*a*c;
6.
if(D<0)
printf("Vo nghiem");
7.
8.
9.
else if(D == 0)
printf("%8.2f", -b/(2*a));
10.
else
11.
{
printf("%8.2f", (-b + sqrt(D))/(2*a));
12.
13.
printf("%8.2f", (-b - sqrt(D))/(2*a));
14.
}
15. }
Giai phuong trinh bac 1 va 2
1.
#include "stdio.h"
2.
#include "conio.h"
3.
#include<math.h>
4.
5.
void Nhap_ptb_nhat(float *heso_a,float *heso_b);
6.
void Nhap_ptb_hai(float *heso_a,float *heso_b, float* heso_c);
7.
8.
int ptbn(float heso_a,float heso_b);
9.
void Xuat(float heso_a, float heso_b);
10. void Giai_Ptbn();
11.
12.
13. int ptbh(float heso_a,float heso_b, float heso_b);
14. void Xuat(float heso_a,float heso_b,float heso_c);
15. void Giai_Ptb2();
16.
17. int main()
18. {
19.
20.
//Giai_Ptbn();
21.
Giai_Ptb2();
22.
getch();
23.
return 0;
24.
25. }
26. void Nhap_ptb_nhat(float *heso_a,float *heso_b)
27. {
28.
29.
30.
31.
printf("\nNhap vao he so a: ");
scanf("%f",&*heso_a);
printf("\nNhap vao he so b: ");
scanf("%f",&*heso_b);
32. }
33.
34. void Nhap_ptb_hai(float *heso_a,float *heso_b,float* heso_c)
35. {
36.
37.
38.
39.
40.
41.
printf("\nNhap vao he so a: ");
scanf("%f",&*heso_a);
printf("\nNhap vao he so b: ");
scanf("%f",&*heso_b);
printf("\nNhap vao he so c: ");
scanf("%f",&*heso_c);
42. }
43. int ptbn(float hso_a,float heso_b)
44. {
45.
if(hso_a==0)
46.
{
47.
if(heso_b==0)
48.
return 2;//Vo so nghiem
49.
else
50.
return 0;//Vo nghiem
51.
}
52.
else
53.
return 1;
54. }
55. void Xuat(float heso_a, float heso_b)
56. {
57.
int songhiem=ptbn(heso_a,heso_b);
58.
59.
if(!songhiem)
printf("\nPhuong trinh vo nghiem.");
60.
else
61.
if(songhiem==2)
printf("\nPhuong trinh co vo so nghiem\n");
62.
else
printf("\nNghiem la: %.2f",-heso_b/heso_a);
63.
64. }
65. void Giai_Ptbn()
66. {
67.
float a,b;
68.
Nhap_ptb_nhat(&a,&b);
69.
Xuat(a,b);
70.
71.
72. }
73. int ptbh(float a,float b, float c)//Khai bao khac voi protype la no problem
74. {
75.
if(a==0)
76.
{
77.
ptbn(b,c);//Di giai phuong trinh bac nhat voi he so b va c
78.
}
79.
else
80.
{
81.
float delta=b*b-4*a*c;
82.
if(delta<0)
83.
return 0;//Vo nghiem
84.
else if(delta==0)
85.
return 1;//Phuong trinh co nghiem kep
86.
else
87.
return 2;//Phuong trinh co 2 nghiem
88.
89.
}
90. }
91. void Xuat(float a, float b, float c)//Khai bao khac voi protype la no problem
92. {
93.
if(a==0)
94.
Xuat(b,c);
95.
else
96.
{
97.
float delta=b*b-4*a*c;
98.
int songhiem=ptbh(a,b,c);
99.
if(!songhiem)//songhiem==0
100.
{
printf("\ndelta=%.2f<0",delta);
101.
102.
printf("\nPhuong trinh bac hai da cho Vo Nghiem.");
103.
}
104.
else
105.
if(songhiem==1)
{
printf("\ndelta=%.2f=0",delta);
106.
107.
printf("\nPhuong trinh bac hai co nghiem kep: x1=x2=%.2f",
108.
-b/(2*a));
109.
}
110.
else//Truong hop co 2 nghiem
111.
{
112.
printf("\nDelta=%.2f>0",delta);
113.
114.
printf("\nPhuong trinh bac hai da cho co 2 nghiem: \n\n");
115.
116.
//printf("x1=(-b+sqrt(delta))/2*a=%.2f\n",(-
b+sqrt(delta))/(2*a));
117.
//printf("x2=(-b-sqrt(delta))/2*a=%.2f",(-b-sqrt(delta))/
(2*a));
118.
printf("x1= %.2f\n",(-b+sqrt(delta))/(2*a));
119.
120.
printf("x2= %.2f",(-b-sqrt(delta))/(2*a));
121.
}
122.
123.
124.
}
125. }
126. void Giai_Ptb2()
127. {
128.
float a,b,c;
129.
Nhap_ptb_hai(&a,&b,&c);
130.
Xuat(a,b,c);
131. }