From ece9b2e4c547c986fd9e2bb4b7d97287be5621fb Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 17 Jan 2025 19:35:54 +0100 Subject: Fix bigint print --- bigint.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/bigint.h b/bigint.h index a8199b9..9287fda 100644 --- a/bigint.h +++ b/bigint.h @@ -124,14 +124,22 @@ public: constexpr BigInt operator%=(const BigInt& z) { return *this = *this % z; } friend std::ostream& operator<<(std::ostream& os, const BigInt& z) { - bool fl = false; + if (z == 0) { + os << "0"; + return os; + } + if (!z.sign) os << "-"; - for (int i = z.D-1; i >= 0; i--) - if (fl = fl || z.digits[i] != 0; fl) - os << z.digits[i]; - if (z == 0) - os << "0"; + + int j; + for (j = z.D-1; z.digits[j] == 0; j--) ; + os << z.digits[j]; // Top digit is not padded + + for (int i = j-1; i >= 0; i--) { + std::string num = std::to_string(z.digits[i]); + os << std::string(E - num.length(), '0') << num; + } return os; } -- cgit v1.2.3