- Code:
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<math.h>
using namespace std;
bool GPTB1(int b, int c,float* x)
{
if((b==0)&&(c!=0))
return false;
else {
*x=-(float)c/b;
return true;
}
}
bool GiaiPTBac2(int a, int b, int c, float *x1, float *x2 )
{
float delta=b*b-4*a*c;
if (delta<0 )
return false;
else if (delta>0)
{
*x1=(-b+sqrt(delta))/(2*a);
*x2=(-b-sqrt(delta))/(2*a);
}
else if (delta==0)
{
*x1 =-b/(2*a);
*x2=-b/(2*a);
}
return true;
}
int main () {
int a=0, b=0, c=0; float x1, x2, x;
int t=0;
cout<<"\n Giai phuong trinh bat 2\n A*X^2+B*X+C=0";
while(true)
{
cout<<"\n Nhap a:";
cin>>a;
if (a!=0) break;
else cout<<"Day la PT bac 1";
}
cout<<"\n Nhap b:";
cin>>b;
cout<<"\n Nhap c:";
cin>>c;
if (a==0){
cout<<" Day la pt bat 1";
bool Ketqua=GPTB1(b, c, &x );
if(Ketqua=false)
cout<<"PT vo nghiem";
else
cout<< "\n\n Nghiem cua X:"<<x;
/*cout<< "\n\n Nghiem cua X:"<<-c;
cout<<"/";
cout<<""<<b;*/
}
else
{
bool Ketqua=GiaiPTBac2(a,b,c,&x1, &x2);
//if (Ketqua=false)
if (Ketqua==false)
cout<<"PT vo nghiem";
else
//cout<< "Nghiem cua x1,x2 la:"<<x1,x2;
cout<< "Nghiem cua X1:"<<x1<<", X2:"<<x2;
}
getch();
}
Cảm ơn bạn đả tham gia Diển Đàn CNTT
Nếu bạn chưa có tài khoản hãy sử dụng tên tài khoản "ACTIVE" và mật khẩu "public" !!!