Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- struct Equation {
- double a;
- double b;
- double c;
- Equation(double aa, double bb, double cc) {
- this->a = aa;
- this->b = bb;
- this->c = cc;
- }
- Equation(double aa, double bb){
- this->a = aa;
- this->b = bb;
- this->c = 0;
- }
- void solveLinear() {
- if (b != 0) {
- printf("Уравнение (линейное) имеет один корень: %f\n", -a / b);
- } else if (a == 0) {
- printf("Уравнение (линейное) имеет множество корней\n");
- } else {
- printf("Нет корней\n");
- }
- }
- void solveQuadratic() {
- double discriminant = b * b - 4 * a * c;
- if (discriminant < 0) {
- printf("Нет корней\n");
- } else if (discriminant == 0) {
- printf("Уравнение (квадратное) имеет один корень: %f\n", -b / (2 * a));
- } else {
- printf("Уравнение (квадратное) имеет два корня: x1 = %f, x2 = %f\n", (-b + sqrt(discriminant)) / (2 * a), (-b - sqrt(discriminant)) / (2 * a));
- }
- }
- };
- int main() {
- double a, b, c;
- printf("Введите коэффициенты a, b, c для уравнения в формате ax^2 + bx + c = 0: ");
- if (scanf("%lf %lf", &a, &b) == 2) {
- if (getchar() == '\n') {
- Equation eqLinear(a, b);
- eqLinear.solveLinear();
- } else {
- if (scanf("%lf", &c) == 1) {
- Equation eqQuadratic(a, b, c);
- eqQuadratic.solveQuadratic();
- } else {
- printf("Неверный ввод.\n");
- }
- }
- } else {
- printf("Неверный ввод.\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement