Monday, August 11, 2014

Câu 1.11: Viết hàm giải và biện luận phương trình bậc hai ax2 + bx + c = 0. Viết chương trình nhập vào từ bàn phím ba số thực a, b, c, áp dụng hàm trên cho biết kết quả giải phương trình bậc hai với ba hệ số a, b, c vừa nhập

// cbai11.cpp : Defines the entry point for the console application.
//

#include"stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<math.h>

voidgaiptb2(float a,floatb,float c)
{
                floatdenta;
                printf("\nGiai phuong trinh he so: a= %5.2f, b=%5.2f, c=%5.2f",a,b,c);
                if(a==0&&b==0&&c==0) printf("\nPhuong trinh vo so nghiem");
                else
                                if(a==0&&b==0&&c!=0) printf("\nPhuong trinh vo nghiem");
                                else
                                                if(a==0&&b!=0) printf("\nPhuong trinh co ngiem %f",(-c)/b);
                                                else
                if(a!=0)
                {
                                denta=b*b-4*a*c;
                                if (denta<0) printf("\nPhuong trinh vo nghiem");
                                if (denta==0) printf("\nPhuong trinh co nghiem kep x1=x2=%f",(-b)/(2*a));
                                if (denta>0) printf("\nPhuong trinh co 2 nghiem phan biet: x1 = %f, x2 = %f",(-b-sqrt(denta))/(2*a),(-b+sqrt(denta))/(2*a));
                }
}

int_tmain(int argc, _TCHAR* argv[])
{
                floata,b,c;
                printf("\nNhap he so a,b,c:   ");
                scanf("%f%f%f",&a,&b,&c);
                gaiptb2(a,b,c);
                getch();
                return0;

}

No comments:

Post a Comment