summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Readme.adoc93
-rw-r--r--tips/formatting.adoc104
2 files changed, 108 insertions, 89 deletions
diff --git a/Readme.adoc b/Readme.adoc
index 469b93b..8152ee6 100644
--- a/Readme.adoc
+++ b/Readme.adoc
@@ -22,96 +22,11 @@ image::img/ss_20220916_200135.png[Debugging is essential]
=== Formatting
-In my opinion, using
+Using
https://cplusplus.com/reference/library/manipulators/[
stream manipulators] and
https://cplusplus.com/reference/ios/ios_base/fmtflags/[
-stream format flags] is cumbersome, but the course _requires_ it’s use (using
-`printf` is not allowed). So maybe the following macro eases it’s use.
+stream format flags] can be cumbersome, but the course _requires_ them since
+using `printf` is not allowed. So maybe macros can be helpful here.
-[source,c++]
-----
-// [aux.h]
-
-/* Stream manipulator macros
- * =========================
- *
- * Requires <iostream> and <iomanip>
- */
-
-// The parenthesis in C/C++ macros are best practice
-// https://stackoverflow.com/q/7186504/7498073
-
-// Width-Left (use with strings)
-#define WL(w) " " << setw((w)-1) << left << setprecision(2) << fixed
-
-// Width-Right (use with numbers)
-#define WR(w) setw(w) << right << setprecision(2) << fixed
-
-// Width-Right-0-padding
-#define WR0(w, w0, x) setw((w) - (w0)) << "" \
- << setw(w0) << right << setfill('0') \
- << setprecision(2) << fixed << (x) \
- << setfill(' ')
-
-// Width-Date (dd/mm/yyyy format)
-#define WD(w, x) setw((w) - 10) << "" << WR0(2, 2, (x) % 100) \
- << '/' << WR0(2, 2, (x) / 100 % 100) \
- << '/' << WR0(4, 4, (x) / 10000)
-----
-
-Example usage:
-
-[source, c++]
-----
-//[example.cpp]
-#include <iostream>
-#include <iomanip>
-#include "aux.h" // easier to import a single .h to any .cpp
-#define MAXLEN 100
-using namespace std;
-typedef struct {
- int id;
- const char name[MAXLEN];
- double luckynumber;
- int dob; // date of birth
-} student_t;
-// dd/mm/yyyy
-void printdate(ostream &os, int date) {
- int d = date % 100;
- int mo = date / 100 % 100;
- int y = date / 10000;
- os << " " << WR0(2, 2, d)
- << '/' << WR0(2, 2, mo)
- << '/' << WR0(4, 4, y);
-}
-int main(int argc, char *argv[])
-{
- student_t students[] = {
- {1, "Ana Ambooken", 69.420, 19990606},
- {2, "Bob Banana", 666.66, 19760411},
- {3, "Carlos Clear", 123.456, 20000101}
- };
- int n = 3;
-
- for (int i = 0; i < n; i++) {
- cout << WR0(8, 4, students[i].id)
- << WL(40) << students[i].name
- << WR(8) << students[i].luckynumber;
- cout << WD(12, students[i].dob);
- cout << '\n';
- }
- return 0;
-}
-
-----
-
-Output:
-
-----
- 0001 Ana Ambooken 69.42 06/06/1999
- 0002 Bob Banana 666.66 11/04/1976
- 0003 Carlos Clear 123.46 01/01/2000
-----
-
-NOTE: Macros are useful, but don't abuse them. They are hard to debug.
+See link:tips/formatting.adoc[] for an example implementation and usage.
diff --git a/tips/formatting.adoc b/tips/formatting.adoc
new file mode 100644
index 0000000..3180aeb
--- /dev/null
+++ b/tips/formatting.adoc
@@ -0,0 +1,104 @@
+:source-highlighter: highlight.js
+
+To print tabulated fields in plain text I first divide the desired ouput by the
+number of columns each field should take. For example:
+
+----
+ dni name birdthdate
+ 12345678 Ana Allen 12/10/1998
+<- 12 col -><------ 24 col --------><- 12 col ->
+----
+
+I like to use multiples of 4, but multiples of 8 or 10 are good options too.
+The nice thing about 4 is that it aligns with my text editor's default tabstop
+setting. It helps when writting report headers.
+
+NOTE: Left-aligned fields after a right-aligned field need a space in
+ between. I like to just add the space to all left-aligned fields.
+
+WARNING: Macros are useful, but don't abuse them. They are hard to debug.
+
+Example implementation:
+
+[source,c++]
+----
+//[aux.h]
+
+/* Stream manipulator macros
+ * =========================
+ *
+ * Requires <iostream> and <iomanip>
+ */
+
+// The parenthesis in C/C++ macros are best practice
+// https://stackoverflow.com/q/7186504/7498073
+
+// Width-Left (use with strings)
+#define WL(w) " " << setw((w) - 1) << left << setprecision(2) << fixed
+
+// Width-Right (use with numbers)
+#define WR(w) setw(w) << right << setprecision(2) << fixed
+
+// Width-Right-0-padding
+#define WR0(w, w0, x) setw((w) - (w0)) << "" \
+ << setw(w0) << right << setfill('0') \
+ << setprecision(2) << fixed << (x) \
+ << setfill(' ')
+
+// Width-Left-Date (dd/mm/yyyy format)
+#define WLD(w, x) setw((w) - 10) << "" << WR0(2, 2, (x) % 100) \
+ << '/' << WR0(2, 2, (x) / 100 % 100) \
+ << '/' << WR0(4, 4, (x) / 10000)
+
+#define MAXLEN 100
+----
+
+Example usage:
+
+[source, c++]
+----
+//[example.cpp]
+
+/* Example usage
+ * ============= */
+
+#include <iostream>
+#include <iomanip>
+#include "aux.h" // It's easier to import a single .h to many .cpp files
+
+using namespace std;
+
+typedef struct {
+ int id;
+ const char name[MAXLEN];
+ double luckynumber;
+ int dob; // date of birth
+} student_t;
+
+int main(int argc, char *argv[])
+{
+ student_t students[] = {
+ {1, "Ana Ambooken", 69.420, 19990606},
+ {2, "Bob Banana", 666.66, 19760411},
+ {3, "Carlos Clear", 123.456, 20000101}
+ };
+ int n = 3;
+
+ for (int i = 0; i < n; i++) {
+ cout << WR0(8, 4, students[i].id)
+ << WL(40) << students[i].name
+ << WR(8) << students[i].luckynumber;
+ cout << WLD(12, students[i].dob);
+ cout << '\n';
+ }
+ return 0;
+}
+----
+
+Output:
+
+----
+ 0001 Ana Ambooken 69.42 06/06/1999
+ 0002 Bob Banana 666.66 11/04/1976
+ 0003 Carlos Clear 123.46 01/01/2000
+----