lab5a/ConsoleApplication1/ConsoleApplication1.cpp

45 lines
1017 B
C++
Raw Permalink Normal View History

2022-05-10 11:59:45 +00:00
#include <iostream>
#include <iomanip>
2022-05-10 11:42:22 +00:00
using namespace std;
2022-05-10 12:12:58 +00:00
class FunkcjaKwadratowa {
2022-05-10 11:59:45 +00:00
float a, b, c;
2022-05-10 12:12:58 +00:00
float delta;
public:
void podajDane(float a, float b, float c) {
this->a = a;
this->b = b;
this->c = c;
2022-05-10 11:59:45 +00:00
2022-05-10 12:12:58 +00:00
this->delta = (b * b) - (4 * a * c);
}
2022-05-10 11:59:45 +00:00
2022-05-10 12:12:58 +00:00
void rownanie() {
cout << setprecision(2) << fixed;
if (delta < 0) cout << "Brak pierwiastkow" << endl;
else if (delta == 0) {
float x = -b / (2 * a);
cout << "1 pierwiastek: x = " << x << endl;
}
else {
float x1, x2;
float sq = sqrt(delta);
x1 = (-b - sq) / (2 * a);
x2 = (-b + sq) / (2 * a);
cout << "2 pierwiastki: x1 = " << x1 << ", x2 = " << x2 << endl;
}
2022-05-10 11:59:45 +00:00
}
2022-05-10 12:12:58 +00:00
void wypisz() {
cout << "Rownanie: " << a << "x^2 + " << b << "x + " << c << " = 0" << endl;
2022-05-10 11:59:45 +00:00
}
2022-05-10 12:12:58 +00:00
};
int main()
{
FunkcjaKwadratowa q;
q.podajDane(4, 4, 1);
q.wypisz();
q.rownanie();
2022-05-10 11:59:45 +00:00
}