Advertisement
Guest User

Giai PT bac 2

a guest
Dec 1st, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. bool ptb2(float a, float b, float c, float *x1, float *x2);
  8. void gpt();
  9.  
  10. int main() {
  11.  int tieptuc = 1;
  12.  while (tieptuc) {
  13.     gpt();  
  14.    
  15.     std::cout<< "Tiep tuc? (1 - Tiep tuc, 0 - Stop)";
  16.     std::cin >> tieptuc;
  17.  }
  18.  
  19.  return 0;  
  20. }
  21.  
  22. void gpt()
  23. {
  24.   float x1, x2;
  25.   float a, b, c;
  26.   std::cout<< "Nhap a: "; std::cin>>a;
  27.   std::cout<< "Nhap b: "; std::cin>>b;
  28.   std::cout<< "Nhap c: "; std::cin>>c;
  29.   if (ptb2(a, b, c, &x1, &x2)) {
  30.       printf("x1 = %f, x2 = %f\n", x1, x2);
  31.   } else {
  32.      printf("Vo nghiem\n");  
  33.   }
  34. }
  35.  
  36. // Ham ptb2 giai phuong trinh bac 2 co 3 he so a, b, c. Ham tra lai true neu co nghiem, false neu vo nghiem
  37. // Trong truong hop co nghiem thi nghiem duoc luu vao x1, x2
  38. bool ptb2(float a, float b, float c, float *x1, float *x2) {
  39.     float delta = b * b - 4 * a * c;
  40.     if (delta < 0)
  41.         return false;
  42.     *x1 = (-b + sqrt(delta))/(2*a);
  43.     *x2 = (-b - sqrt(delta))/(2*a);
  44.     return true;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement