1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
= Unofficial LP1 test repository
Mitsuo
2022-09-09
:source-highlighter: highlight.js
The shortest and most readable code wins!
Contributions are very welcomed and needed, if you want to contribute
please open a Pull Request following the directory structure:
semester/L#/submitter's name/
== Tips
*Debugging*
Prof. Guanira states it clearly in "Guia de Creacion Ejecucion y
Depuracion.pdf":
image::img/ss_20220916_200135.png[Debugging is essential]
*Formatting*
In my opinion, 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.
[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.
|