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