-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprueba-tecnica-code-2.cpp
More file actions
118 lines (96 loc) · 2.76 KB
/
prueba-tecnica-code-2.cpp
File metadata and controls
118 lines (96 loc) · 2.76 KB
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
#include <thread>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sstream>
/**
* IMPORTANT NOTE: when compiling, we need to introduce the following flag: -pthread.
*/
using namespace std;
class SharedData {
private:
struct shared_data {
string next_date;
int rand_number{};
string name = "Marc Guzman Albiol";
bool is_ready{};
};
public:
shared_data data;
/**
* Function that fills the data of the class using the 4 threads.
*/
void fillData() {
thread dateT(&SharedData::dateThread, this);
thread nameT(&SharedData::nameThread, this);
thread randT(&SharedData::randomThread, this);
dateT.join();
nameT.join();
randT.join();
thread readyT(&SharedData::isReadyThread, this);
readyT.join();
}
/**
* Function that retrieves the date of today and adds 123 days to it. The strategy is to use UNIX timestamp and
* add 60*60*24*123.
*/
void dateThread() {
time_t rawtime;
struct tm *timeinfo;
char buffer[80];
time(&rawtime);
rawtime += 60 * 60 * 24 * 123;
timeinfo = localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%d", timeinfo);
string str(buffer);
data.next_date = buffer;
}
/**
* Function that reverse the order of the name attribute. Uses the reverse function.
*/
void nameThread() {
reverse(data.name.begin(), data.name.end());
}
/**
* Function that gets a random integer number from 1 to 100. Firt, we 'randomly' initialize a seed in order to
* invoke the random behaviour of the rand function. Then, take the modulo 100 and sum one to obtain a random in
* the desired integer interval.
*/
void randomThread() {
srand(time(nullptr));
data.rand_number = rand() % 100 + 1;
}
void isReadyThread() {
data.is_ready = true;
}
};
/**
* Function to correctly print a bool.
* @param b bool.
* @return true if 1, false if 0
*/
string boolstring(bool b) {
return b ? "true" : "false";
}
/**
* Transforms the shared_data into a JSON.
* @param shared_data
* @return JSON string.
*/
string getJSONData(const SharedData& shared_data) {
stringstream ss;
string data;
ss << "{" << endl;
ss << R"( "next_date": )" + shared_data.data.next_date << endl;
ss << R"( "rand_number": )" + std::to_string(shared_data.data.rand_number) << endl;
ss << R"( "name": )" + shared_data.data.name << endl;
ss << R"( "is_ready": )" + boolstring(shared_data.data.is_ready) << endl;
ss << "}" << endl;
return ss.str();
}
int main() {
SharedData shared_data;
shared_data.fillData();
cout << getJSONData(shared_data) << endl;
return 0;
}