From eda2bffe27415d28813badc5db572d1e4b20c3de Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 16 Apr 2013 14:53:02 -0600 Subject: Added formatter for Number. I'm a little embarassed that I didn't do this from the start, made the code so much easier to read. --- src/main.cpp | 130 ++++++++++++++------------------------------------------- src/number.cpp | 5 +++ src/number.h | 2 + 3 files changed, 38 insertions(+), 99 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6674075..4454c08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,164 +38,101 @@ void numbertest1() Number a("1000902491523000321"); Number b("3004392012498000700"); - println("%1 + %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a + b).toString() ); + println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); a = "-872"; b = "123"; - println("%1 + %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a + b).toString() ); + println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); a = "728"; b = "-51"; - println("%1 + %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a + b).toString() ); + println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); a = "44"; b = "-55"; - println("%1 + %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a + b).toString() ); + println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); a = "44"; b = "-66"; - println("%1 + %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a + b).toString() ); + println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); a = "44"; b = "-66"; - println("%1 - %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a - b).toString() ); + println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); a = "44"; b = "66"; - println("%1 - %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a - b).toString() ); + println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); a = "7814"; b = "24"; - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); a = "12345"; b = "678"; - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); a = "3592846"; b = "944634757"; - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); a = "3592846"; b = ""; - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); a = "123"; b = "-45"; - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); a = "-123"; b = "45"; - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); a = "-123"; b = "-45"; - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); a = "123"; b = "45"; - println("%1 / %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a / b).toString() ); + println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); a = "12345"; b = "45"; - println("%1 / %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a / b).toString() ); + println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); a = "3007103450821050020096034077958224700"; b = "898239467"; - println("%1 / %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a / b).toString() ); - println("%1 %% %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a % b).toString() ); + println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); + println("%1 %% %2 = %3").arg( a ).arg( b ).arg( a % b ); a = "983429807324875233421784598754987439873472349875329853298732"; b = "18446744073709551615"; - println("%1 / %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a / b).toString() ); - println("%1 %% %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a % b).toString() ); + println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); + println("%1 %% %2 = %3").arg( a ).arg( b ).arg( a % b ); } #define compcheck( anum, op, bnum ) \ a = #anum; b = #bnum; \ - println("%4: %1 " #op " %2 = %3").arg( a.toString() ).arg( b.toString() ). \ + println("%4: %1 " #op " %2 = %3").arg( a ).arg( b ). \ arg( a op b ).arg( ((a op b) == (anum op bnum)) ? "pass" : "fail" ) void numbertestcomp() @@ -327,21 +264,16 @@ void radixtest() a = "f8a72bce3"; b = "9ea8cb3"; println("%1 + %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a + b).toString() ); + arg( a ). + arg( b ). + arg( (a + b) ); println("%1 - %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a - b).toString() ); - println("%1 / %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a / b).toString() ); - println("%1 * %2 = %3"). - arg( a.toString() ). - arg( b.toString() ). - arg( (a * b).toString() ); + arg( a ). + arg( b ). + arg( (a - b) ); + println("%1 / %2 = %3").arg( a ).arg( b ). + arg( (a / b) ); + println("%1 * %2 = %3").arg( a ).arg( b ).arg( (a * b) ); } int main( int , char *[] ) diff --git a/src/number.cpp b/src/number.cpp index 12a86e0..b045e7c 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -431,3 +431,8 @@ void Number::divide( const Number &rhs, Number &q, Number &r ) const // Bu::println("Complete: q = %1, r = %2").arg( q.toString() ).arg( r.toString() ); } +Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n ) +{ + return f << n.toString(); +} + diff --git a/src/number.h b/src/number.h index 8d98a1b..cdae1df 100644 --- a/src/number.h +++ b/src/number.h @@ -52,4 +52,6 @@ private: PackedIntArray aInt; }; +Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n ); + #endif -- cgit v1.2.3