Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c4d8417

Browse files
committed
Exercise 25.1
1 parent 45de28b commit c4d8417

File tree

5 files changed

+359
-0
lines changed

5 files changed

+359
-0
lines changed

‎CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ add_subdirectory ("Chapter_20")
3030
add_subdirectory ("Chapter_21")
3131
add_subdirectory ("Chapter_23")
3232
add_subdirectory ("Chapter_24")
33+
add_subdirectory ("Chapter_25")

‎Chapter_25/C25_Exercise_25.1.cpp

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
/* Exercise 25.1 */
2+
3+
#include<iostream>
4+
#include<sstream>
5+
#include<fstream>
6+
#include<iomanip>
7+
#include<vector>
8+
#include<bitset>
9+
#include"C25_Exercise_25.1.h"
10+
11+
using namespace std;
12+
13+
inline void error(string s) { throw runtime_error(s); }
14+
inline void error(const string& s, const string& s2) { error(s + s2); }
15+
inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); }
16+
inline void keep_window_open() { char ch; cin >> ch; }
17+
inline int randint(int max) { return rand() % max; }
18+
inline int randint(int min, int max) { return randint(max - min) + min; }
19+
20+
int main()
21+
{
22+
enum Action {
23+
EXIT = -1, PRINTACTIONLIST,
24+
CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, CASE9, CASE10
25+
};
26+
const string actionList = "\tList of actions:\n"
27+
" (1) Memory fragmentation, (2) Bits, (3) Infinite loop, (4) si = 128\n"
28+
" (5) Encrypt(), (6) Decrypt()\n"
29+
" (-1) Exit, (0) Print the list of actions\n";
30+
cout << actionList;
31+
int action;
32+
bool cond{ true };
33+
while (cond) try {
34+
cout << "\nPlease enter the action: ";
35+
if (!(cin >> action)) { ClearInput(cin); error("Error. Incorrect input"); }
36+
char ch;
37+
cin.get(ch);
38+
switch (action) {
39+
case CASE1: {
40+
cout << endl;
41+
cout << sp_2 << "sizeof(Message) = " << sizeof(Message) << " bytes" << endl;
42+
cout << sp_2 << "sizeof(Node) = " << sizeof(Node) << " bytes" << endl;
43+
cout << endl;
44+
45+
constexpr int n = 10;
46+
vector<int> randoms;
47+
for (int i = 0; i < n * 2; ++i) {
48+
randoms.push_back(randint(n));
49+
}
50+
int i = 0;
51+
while (i < n) {
52+
cout << sp_4 << right << setw(4) << i + 1 << ") ";
53+
Message* p1 = new Message{ randoms[i], randoms[i], randoms[i] };
54+
p1->id += randoms[i * 2];
55+
cout << "p1->" << p1 << ", ";
56+
Node* n1 = new Node(p1->id, p1->id);
57+
n1->id += p1->id;
58+
cout << "n1->" << n1 << ", ";
59+
delete p1;
60+
Node* n2 = new Node(n1->id, n1->id);
61+
n2->id += n1->id;
62+
cout << "n2->" << n2 << endl;
63+
i++;
64+
}
65+
cout << vsp_4;
66+
67+
i = 0;
68+
while (i < n) {
69+
cout << sp_4 << right << setw(4) << i + 1 << ") ";
70+
Node* n1 = new Node(randoms[i], randoms[i]);
71+
n1->id += n1->id;
72+
cout << "n1->" << n1 << ", ";
73+
Node* n2 = new Node(n1->id, n1->id);
74+
n2->id += n1->id;
75+
cout << "n2->" << n2 << ", ";
76+
Message* p1 = new Message{ n2->id, n2->id, n2->id };
77+
p1->id += n2->id;
78+
cout << "p1->" << p1 << endl;
79+
delete p1;
80+
i++;
81+
}
82+
83+
cout << vsp_2 << "This example does not work correctly. "
84+
"This is probably caused by compiler optimization." << endl;
85+
86+
cout << vsp_2;
87+
break;
88+
}
89+
case CASE2: {
90+
cout << endl;
91+
for (unsigned int i; cin >> i; ) {
92+
cout << dec << i << " == "
93+
<< hex << "0x" << i << " == "
94+
<< bitset<8 * sizeof(int)>{i} << '\n';
95+
}
96+
97+
98+
cout << vsp_2;
99+
break;
100+
}
101+
case CASE3: {
102+
cout << endl;
103+
unsigned char max = 160; // very large
104+
for (signed char i = 0; i < max; ++i) cout << int(i) << '\n';
105+
106+
cout << vsp_2;
107+
break;
108+
}
109+
case CASE4: {
110+
cout << endl;
111+
int si = 128;
112+
unsigned int ui = si;
113+
cout << dec << si << " == "
114+
<< hex << "0x" << si << " == "
115+
<< bitset<8 * sizeof(int)>{ui} << '\n';
116+
117+
cout << vsp_2;
118+
break;
119+
}
120+
case CASE5: {
121+
cout << endl;
122+
Encrypt();
123+
cout << vsp_2;
124+
break;
125+
}
126+
case CASE6: {
127+
cout << endl;
128+
Decrypt();
129+
cout << vsp_2;
130+
break;
131+
}
132+
case PRINTACTIONLIST:
133+
cout << actionList;
134+
break;
135+
case EXIT:
136+
cond = false;
137+
break;
138+
default:
139+
error("Error. Incorrect action number");
140+
break;
141+
}
142+
}
143+
catch (runtime_error& e) {
144+
cerr << e.what() << endl;
145+
}
146+
catch (...) {
147+
cerr << "Error. Exception\n";
148+
return 1;
149+
}
150+
return 0;
151+
}
152+
153+
void ClearInput(istream& is)
154+
{
155+
is.clear();
156+
is.ignore(numeric_limits<streamsize>::max(), '\n');
157+
}
158+
159+
void Encrypt()
160+
{
161+
const int nchar = 2 * sizeof(long); // 64 bits
162+
const int kchar = 2 * nchar; // 128 bits
163+
string op;
164+
string key;
165+
string infile;
166+
string outfile;
167+
cout << "please enter input file name, output file name, and key:\n";
168+
cin >> infile >> outfile >> key;
169+
while (key.size() < kchar) key += '0'; // pad key
170+
ifstream inf(infile);
171+
ofstream outf(outfile);
172+
if (!inf || !outf) error("bad file name");
173+
const unsigned long* k = reinterpret_cast<const unsigned long*>(key.data());
174+
unsigned long outptr[2];
175+
char inbuf[nchar];
176+
unsigned long* inptr = reinterpret_cast<unsigned long*>(inbuf);
177+
int count = 0;
178+
while (inf.get(inbuf[count])) {
179+
outf << hex; // use hexadecimal output
180+
if (++count == nchar) {
181+
encipher(inptr, outptr, k);
182+
// pad with leading zeros:
183+
outf << setw(8) << setfill('0') << outptr[0] << ' '
184+
<< setw(8) << setfill('0') << outptr[1] << ' ';
185+
count = 0;
186+
}
187+
}
188+
if (count) { // pad
189+
while (count != nchar) inbuf[count++] = '0';
190+
encipher(inptr, outptr, k);
191+
outf << outptr[0] << ' ' << outptr[1] << ' ';
192+
}
193+
}
194+
195+
void Decrypt()
196+
{
197+
const int nchar = 2 * sizeof(long); // 64 bits
198+
const int kchar = 2 * nchar; // 128 bits
199+
string op;
200+
string key;
201+
string infile;
202+
string outfile;
203+
cout << "please enter input file name, output file name, and key:\n";
204+
cin >> infile >> outfile >> key;
205+
while (key.size() < kchar) key += '0'; // pad key
206+
ifstream inf(infile);
207+
ofstream outf(outfile);
208+
if (!inf || !outf) error("bad file name");
209+
const unsigned long* k = reinterpret_cast<const unsigned long*>(key.data());
210+
unsigned long inptr[2];
211+
char outbuf[nchar + 1];
212+
outbuf[nchar] = 0; // terminator
213+
unsigned long* outptr = reinterpret_cast<unsigned long*>(outbuf);
214+
inf.setf(ios_base::hex, ios_base::basefield); // use hexadecimal input
215+
while (inf >> inptr[0] >> inptr[1]) {
216+
decipher(inptr, outptr, k);
217+
outf << outbuf;
218+
}
219+
}
220+
221+
void encipher(
222+
const unsigned long* const v,
223+
unsigned long* const w,
224+
const unsigned long* const k)
225+
{
226+
static_assert(sizeof(long) == 4, "size of long wrong for TEA");
227+
unsigned long y = v[0];
228+
unsigned long z = v[1];
229+
unsigned long sum = 0;
230+
const unsigned long delta = 0x9E3779B9;
231+
for (unsigned long n = 32; n-- > 0; ) {
232+
y += (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3];
233+
sum += delta;
234+
z += (y << 4 ^ y >> 5) + y ^ sum + k[sum >> 11 & 3];
235+
}
236+
w[0] = y;
237+
w[1] = z;
238+
}
239+
240+
void decipher(
241+
const unsigned long* const v,
242+
unsigned long* const w,
243+
const unsigned long* const k)
244+
{
245+
static_assert(sizeof(long) == 4, "size of long wrong for TEA");
246+
unsigned long y = v[0];
247+
unsigned long z = v[1];
248+
unsigned long sum = 0xC6EF3720;
249+
const unsigned long delta = 0x9E3779B9;
250+
// sum = delta<<5, in general sum = delta * n
251+
for (unsigned long n = 32; n-- > 0; ) {
252+
z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum >> 11 & 3];
253+
sum -= delta;
254+
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3];
255+
}
256+
w[0] = y;
257+
w[1] = z;
258+
}

‎Chapter_25/C25_Exercise_25.1.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Exercise 25.1 */
2+
3+
using namespace std;
4+
5+
const char* sp_2 = " ";
6+
const char* sp_4 = " ";
7+
const char* sp_6 = " ";
8+
const char* sp_8 = " ";
9+
const char* vsp_2 = "\n\n";
10+
const char* vsp_3 = "\n\n\n";
11+
const char* vsp_4 = "\n\n\n\n";
12+
13+
void ClearInput(istream& is);
14+
15+
struct Node {
16+
int id{ 0 };
17+
int num{ 0 };
18+
Node(int id, int number) : id{ id }, num{ number } {}
19+
};
20+
21+
struct Message {
22+
int id{ 0 };
23+
int num{ 0 };
24+
int mark{ 0 };
25+
Message(int id, int number, int mark) : id{ id }, num{ number }, mark{ mark } {}
26+
};
27+
28+
29+
void Encrypt();
30+
void Decrypt();
31+
void encipher(
32+
const unsigned long* const v,
33+
unsigned long* const w,
34+
const unsigned long* const k);
35+
void decipher(
36+
const unsigned long* const v,
37+
unsigned long* const w,
38+
const unsigned long* const k);

‎Chapter_25/C25_Exercise_25.1_input.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
5b8fb57c 806fbcce 2db72335 23989d1d 991206bc 0363a308
2+
8f8111ac 38f3f2f3 9110a4bb c5e1389f 64d7efe8 ba133559
3+
4cc00fa0 6f77e537 bde7925f f87045f0 472bad6e dd228bc3
4+
a5686903 51cc9a61 fc19144e d3bcde62 4fdb7dc8 43d565e5
5+
f1d3f026 b2887412 97580690 d2ea4f8b 2d8fb3b7 936cfa6d
6+
6a13ef90 fd036721 b80035e1 7467d8d8 d32bb67e 29923fde
7+
197d4cd6 76874951 418e8a43 e9644c2a eb10e848 ba67dcd8
8+
7115211f dbe32069 e4e92f87 8bf3e33e b18f942c c965b87a
9+
44489114 18d4f2bc 256da1bf c57b1788 9113c372 12662c23
10+
eeb63c45 82499657 a8265f44 7c866aae 7c80a631 e91475e1
11+
5991ab8b 6aedbb73 71b642c4 8d78f68b d602bfe4 d1eadde7
12+
55f20835 1a6d3a4b 202c36b8 66a1e0f2 771993f3 11d1d0ab
13+
74a8cfd4 4ce54f5a e5fda09d acbdf110 259a1a19 b964a3a9
14+
456fd8a3 1e78591b 07c8f5a2 101641ec d0c9d7e1 60dbeb11
15+
b9ad8e72 ad30b839 201fc553 a34a79c4 217ca84d 30f666c6
16+
d018e61c d1c94ea6 6ca73314 cd60def1 6e16870e 45b94dc0
17+
d7b44fcd 96e0425a 72839f71 d5b6427c 214340f9 8745882f
18+
0602c1a2 b437c759 ca0e3903 bd4d8460 edd0551e 31d34dd3
19+
c3f943ed d2cae477 4d9d0b61 f647c377 0d9d303a ce1de974
20+
f9449784 df460350 5d42b06c d4dedb54 17811b5f 4f723692
21+
14d67edb 11da5447 67bc059a 4600f047 63e439e3 2e9d15f7
22+
4f21bbbe 3d7c5e9b 433564f5 c3ff2597 3a1ea1df 305e2713
23+
9421d209 2b52384f f78fbae7 d03c1f58 6832680a 207609f3
24+
9f2c5a59 ee31f147 2ebc3651 e017d9d6 d6d60ce2 2be1f2f9
25+
eb9de5a8 95657e30 cad37fda 7bce06f4 457daf44 eb257206
26+
418c24a5 de687477 5c1b3155 f744fbff 26800820 92224e9d
27+
43c03a51 d168f2d1 624c54fe 73c99473 1bce8fbb 62452495
28+
5de382c1 1a789445 aa00178a 3e583446 dcbd64c5 ddda1e73
29+
fa168da2 60bc109e 7102ce40 9fed3a0b 44245e5d f612ed4c
30+
b5c161f8 97ff2fc0 1dbf5674 45965600 b04c0afa b537a770
31+
9ab9bee7 1624516c 0d3e556b 6de6eda7 d159b10e 71d5c1a6
32+
b8bb87de 316a0fc9 62c01a3d 0a24a51f 86365842 52dabf4d
33+
372ac18b 9a5df281 35c9f8d7 07c8f9b4 36b6d9a5 a08ae934
34+
239efba5 5fe3fa6f 659df805 faf4c378 4c2048d6 e8bf4939
35+
31167a93 43d17818 998ba244 55dba8ee 799e07e7 43d26aef
36+
d5682864 05e641dc b5948ec8 03457e3f 80c934fe cc5ad4f9
37+
0dc16bb2 a50aa1ef d62ef1cd f8fbbf67 30c17f12 718f4d9a
38+
43295fed 561de2a0

‎Chapter_25/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# CMakeList.txt : CMake project for Template, include source and define
2+
# project specific logic here.
3+
#
4+
cmake_minimum_required (VERSION 3.8)
5+
6+
project ("Chapter_25")
7+
8+
set (CMAKE_CXX_STANDARD 14)
9+
set (CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/..)
10+
11+
12+
set (FILE_NAME C25_Exercise_25.1)
13+
set (SOURCE_CXX_LIST ${FILE_NAME}.cpp)
14+
15+
16+
# Add source to this project's executable.
17+
add_executable (${FILE_NAME} ${SOURCE_CXX_LIST})
18+
19+
20+
# TODO: Add tests and install targets if needed.
21+
install (TARGETS ${FILE_NAME} CONFIGURATIONS Debug DESTINATION Build/Debug)
22+
install (TARGETS ${FILE_NAME} CONFIGURATIONS Release DESTINATION Build)
23+
install (FILES C25_Exercise_25.1_input.txt CONFIGURATIONS Debug DESTINATION Build/Debug)
24+
install (FILES C25_Exercise_25.1_input.txt CONFIGURATIONS Release DESTINATION Build)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /