From 8892c7851f800cc28759c18750fcd80ac6eb703e Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 18 Apr 2020 10:25:05 +0200 Subject: [PATCH] Better output for << operator --- cnumber.hpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cnumber.hpp b/cnumber.hpp index 0b76a83..81b0e29 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -37,7 +37,23 @@ class cnumber { } friend ostream &operator<<( ostream &os, const cnumber &z ) { - os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i'; + if (z.r != 0) { + os << z.r; + if (z.signi && (z.i != 0)) { + os << '+'; + } + } + if (z.i != 0) { + if ((z.i != 1) && (z.i != -1)) { + os << z.i; + } + else { + if (! z.signi) { + os << '-'; + } + } + os << 'i'; + } return os; } void operator=(const cnumber &z ) {