From fc2e46a6543a6d77c220aaaa6f1eb25e41833ef6 Mon Sep 17 00:00:00 2001 From: serr Date: Mon, 12 May 2025 20:56:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BA=D1=80=D0=B0=D1=81=D0=B8=D0=B2=D1=83=D1=8E=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=87=D0=B0=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BB=D0=B8=D0=BD=D0=BE?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- analyzers/polynomials/poly_calc/poly_calc.c | 32 +++++++++++++++------ poly.txt | 2 +- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/analyzers/polynomials/poly_calc/poly_calc.c b/analyzers/polynomials/poly_calc/poly_calc.c index a0263a9..d1c54ca 100644 --- a/analyzers/polynomials/poly_calc/poly_calc.c +++ b/analyzers/polynomials/poly_calc/poly_calc.c @@ -144,18 +144,32 @@ void free_polynomial(Polynomial *p) { // Печать полинома void print_polynomial(Polynomial *p) { if (p->size == 0) { - printf("0\n"); return; + printf("0\n"); + return; } for (int i = 0; i < p->size; i++) { - // Печать знака - if (p->terms[i].coefficient > 0) { printf("+"); - } else { printf("-"); } - // Печать коэффициента - printf("%d", abs(p->terms[i].coefficient)); - // Печать переменной и степени - if (p->terms[i].exponent > 0) { - printf("x^%d", p->terms[i].exponent); + Term term = p->terms[i]; + + // Печать знака (не печатаем '+' перед первым слагаемым) + if (i != 0 || term.coefficient < 0) { + printf("%c", term.coefficient > 0 ? '+' : '-'); + } + + // Печать коэффициента (если не 1/-1 или если степень 0) + if (abs(term.coefficient) != 1 || term.exponent == 0) { + printf("%d", abs(term.coefficient)); + } else if (term.coefficient == -1 && term.exponent != 0) { + printf("-"); // Специальный случай для -x^n + } + + // Печать переменной + if (term.exponent > 0) { + printf("x"); + // Печать степени только если она больше 1 + if (term.exponent > 1) { + printf("^%d", term.exponent); + } } } printf("\n"); diff --git a/poly.txt b/poly.txt index 89e26e6..5f4c8b1 100644 --- a/poly.txt +++ b/poly.txt @@ -1 +1 @@ -2+2*(10-3)*(x-8) \ No newline at end of file +((3*x^2 - 2*x + 1) * (x^3 - 4*x) + (5*x^4 - x^2)^2) - (x + 1)^5 \ No newline at end of file