Advertisement
Centipede18

ptbac2

Mar 26th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstdio>
  4. using namespace std;
  5.  
  6. void nhap(float a, float b, float c){
  7.     cin>>a>>b>>c;
  8. }
  9.  
  10.  
  11. void nghiem(float a, float b, float c){
  12.     float x1, x2;
  13.     if(a==0){
  14.         if(b==0){
  15.             if(c==0) cout<<"Phuong trinh co vo so nghiem";
  16.             else cout<<"Phuong trinh vo nghiem";
  17.         }
  18.         if(b!=0){
  19.             x1 = -c/b;
  20.             printf("Nghiem cua phuong trinh la: %.2f", x1);
  21.         }
  22.     }
  23.     else {
  24.         float delta;
  25.         int x = 0;
  26.         delta = b*b-4*a*c;
  27.         if(delta > 0) x=1;
  28.         if(delta == 0) x=0;
  29.         if(delta < 0) x=-1;
  30.         switch(x){
  31.             case 1:{
  32.                 x1 = (-b+sqrt(delta))/(2*a);
  33.                 x2 = (-b-sqrt(delta))/(2*a);
  34.                 printf("Phuong trinh co 2 nghiem la: %.2f, %.2f", x1, x2);
  35.                 break;
  36.             }
  37.             case 0:{
  38.                 x1 = -b/(2*a);
  39.                 printf("Nghiem cua phuong trinh la: %.2f", x1);
  40.                 break;
  41.             }
  42.             case -1:{
  43.                 cout<<"Pt vo nghiem";
  44.                 break;
  45.             }
  46.             default: cout<<"khong the giai phuong trinh nay!";
  47.         }
  48.     }
  49. }
  50.    
  51. main(){
  52.     float a,b,c;
  53. //  nhap(a, b, c);
  54.     cin>>a>>b>>c;
  55.     nghiem(a, b, c);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement