int ptbhfloat a,float b, float c//Khai bao khac voi protype la no problem 74.. ptbnb,c;//Di giai phuong trinh bac nhat voi he so b va c.[r]
Trang 1Giai 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( < )
7. printf("Vo nghiem");
8 else if(D == )
9. printf("%8.2f", - /(2 a));
10 else
11 {
12. printf("%8.2f", (-b + sqrt( ))/(2 a));
13. printf("%8.2f", (-b - sqrt( ))/(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();
Trang 221 Giai_Ptb2();
22 getch();
23 return ;
24
25 }
26 void Nhap_ptb_nhat(float heso_a,float heso_b)
27 {
28. printf( \nNhap vao he so a: ");
29. scanf("%f",&*heso_a);
30. printf( \nNhap vao he so b: ");
31. scanf("%f",&*heso_b);
32 }
33
34 void Nhap_ptb_hai(float heso_a,float heso_b,float* heso_c)
35 {
36. printf( \nNhap vao he so a: ");
37. scanf("%f",&*heso_a);
38. printf( \nNhap vao he so b: ");
39. scanf("%f",&*heso_b);
40. printf( \nNhap vao he so c: ");
41. 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 ;//Vo so nghiem
49 else
50 return ;//Vo nghiem
51 }
52 else
53 return ;
54 }
55 void Xuat(float heso_a, float heso_b)
56 {
57 int songhiem=ptbn(heso_a,heso_b);
58 if(!songhiem)
59. printf( \nPhuong trinh vo nghiem.");
60 else if(songhiem==2
61. printf( \nPhuong trinh co vo so nghiem\n");
62 else
63. printf( \nNghiem la: %.2f",-heso_b/heso_a);
64 }
65 void Giai_Ptbn()
Trang 366 {
67 float a, ;
68 Nhap_ptb_nhat(&a,&b);
69 Xuat( , );
70
71
72 }
73 int ptbh(float a,float b, float c)//Khai bao khac voi protype la no problem
74 {
75 if( ==0
76 {
77 ptbn( , );//Di giai phuong trinh bac nhat voi he so b va c
78 }
79 else
80 {
81 float delta= * - * * ;
82 if(delta< )
83 return ;//Vo nghiem
84 else if(delta==0
85 return ;//Phuong trinh co nghiem kep
86 else
87 return ;//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( ==0
94 Xuat( , );
95 else
96 {
97 float delta= * - * * ;
98 int songhiem=ptbh( , , );
99 if(!songhiem)//songhiem==0
100 {
101. printf( \ndelta=%.2f<0",delta);
102. printf( \nPhuong trinh bac hai da cho Vo Nghiem.");
103 }
104 else if(songhiem==1
105 {
106. printf( \ndelta=%.2f=0",delta);
107. printf( \nPhuong trinh bac hai co nghiem kep: x1=x2=%.2f",
108 - /(2 a));
109 }
110 else//Truong hop co 2 nghiem
Trang 4111 {
112
113. printf( \nDelta=%.2f>0",delta);
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
119. printf("x1= %.2f\n",(-b sqrt(delta))/(2 a));
120. printf("x2= %.2f",(-b sqrt(delta))/(2 a));
121 }
122
123
124 }
125 }
126.void Giai_Ptb2()
127 {
128 float a, , ;
129 Nhap_ptb_hai(&a,&b,&c);
130 Xuat( , , );
131 }