Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- #include <stdio.h>
- #include <math.h>
- bool ptb2(float a, float b, float c, float *x1, float *x2);
- void gpt();
- int main() {
- int tieptuc = 1;
- while (tieptuc) {
- gpt();
- std::cout<< "Tiep tuc? (1 - Tiep tuc, 0 - Stop)";
- std::cin >> tieptuc;
- }
- return 0;
- }
- void gpt()
- {
- float x1, x2;
- float a, b, c;
- std::cout<< "Nhap a: "; std::cin>>a;
- std::cout<< "Nhap b: "; std::cin>>b;
- std::cout<< "Nhap c: "; std::cin>>c;
- if (ptb2(a, b, c, &x1, &x2)) {
- printf("x1 = %f, x2 = %f\n", x1, x2);
- } else {
- printf("Vo nghiem\n");
- }
- }
- // 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
- // Trong truong hop co nghiem thi nghiem duoc luu vao x1, x2
- bool ptb2(float a, float b, float c, float *x1, float *x2) {
- float delta = b * b - 4 * a * c;
- if (delta < 0)
- return false;
- *x1 = (-b + sqrt(delta))/(2*a);
- *x2 = (-b - sqrt(delta))/(2*a);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement