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 2cae756

Browse files
committed
Exercise 24.1; Exercise 24.2
1 parent 295fe08 commit 2cae756

File tree

6 files changed

+266
-0
lines changed

6 files changed

+266
-0
lines changed

‎CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ add_subdirectory ("Chapter_19")
2929
add_subdirectory ("Chapter_20")
3030
add_subdirectory ("Chapter_21")
3131
add_subdirectory ("Chapter_23")
32+
add_subdirectory ("Chapter_24")

‎Chapter_24/C24_Exercise_24.1.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Exercise 24.1 */
2+
3+
#include<iostream>
4+
#include<sstream>
5+
#include<iomanip>
6+
#include"../Matrix/Matrix11.h"
7+
#include"../Matrix/MatrixIO11.h"
8+
#include"C24_Exercise_24.1.h"
9+
10+
using namespace std;
11+
12+
inline void error(string s) { throw runtime_error(s); }
13+
inline void error(const string& s, const string& s2) { error(s + s2); }
14+
inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); }
15+
inline void keep_window_open() { char ch; cin >> ch; }
16+
17+
int main()
18+
{
19+
enum Action {
20+
EXIT = -1, PRINTACTIONLIST,
21+
CASE1, CASE2, CASE3, CASE4, CASE5
22+
};
23+
const string actionList = "\tList of actions:\n"
24+
" (1) triple()\n"
25+
" (-1) Exit, (0) Print the list of actions\n";
26+
cout << actionList;
27+
int action;
28+
bool cond{ true };
29+
while (cond) try {
30+
cout << "\nPlease enter the action: ";
31+
if (!(cin >> action)) { ClearInput(cin); error("Error. Incorrect input"); }
32+
char ch;
33+
cin.get(ch);
34+
switch (action) {
35+
case CASE1: {
36+
cout << endl;
37+
Numeric_lib::Matrix<int> mat(5);
38+
for (int i = 0; i < 5; ++i) mat[i] = i + 1;
39+
40+
Numeric_lib::Matrix<int> mat1(5);
41+
for (int i = 0; i < 5; ++i) mat1[i] = i + 1;
42+
Numeric_lib::Matrix<int> mat2(5);
43+
mat1.apply(triple1);
44+
mat2 = Numeric_lib::apply(triple2, mat);
45+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
46+
cout << sp_2 << "Matrix<int> mat1.apply(triple1): " << mat1 << endl;
47+
cout << sp_2 << "Matrix<int> mat2 = Numeric_lib::apply(triple2, mat): " << mat2 << endl;
48+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
49+
50+
Numeric_lib::Matrix<int> mat3(5);
51+
for (int i = 0; i < 5; ++i) mat3[i] = i + 1;
52+
Numeric_lib::Matrix<int> mat4(5);
53+
mat3.apply(triple);
54+
mat4 = Numeric_lib::apply(triple, mat);
55+
cout << endl;
56+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
57+
cout << sp_2 << "Matrix<int> mat3.apply(triple): " << mat3 << endl;
58+
cout << sp_2 << "Matrix<int> mat4 = Numeric_lib::apply(triple, mat): " << mat4 << endl;
59+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
60+
61+
cout << vsp_2;
62+
break;
63+
}
64+
case PRINTACTIONLIST:
65+
cout << actionList;
66+
break;
67+
case EXIT:
68+
cond = false;
69+
break;
70+
default:
71+
error("Error. Incorrect action number");
72+
break;
73+
}
74+
}
75+
catch (runtime_error& e) {
76+
cerr << e.what() << endl;
77+
}
78+
catch (...) {
79+
cerr << "Error. Exception\n";
80+
return 1;
81+
}
82+
return 0;
83+
}
84+
85+
void ClearInput(istream& is)
86+
{
87+
is.clear();
88+
is.ignore(numeric_limits<streamsize>::max(), '\n');
89+
}

‎Chapter_24/C24_Exercise_24.1.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Exercise 24.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+
void triple1(int& elem) { elem *= 3; }
16+
int triple2(const int& elem) { return elem * 3; }
17+
int triple(int& elem) { return elem *= 3; }

‎Chapter_24/C24_Exercise_24.2.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Exercise 24.2 */
2+
3+
#include<iostream>
4+
#include<sstream>
5+
#include<iomanip>
6+
#include"../Matrix/Matrix11.h"
7+
#include"../Matrix/MatrixIO11.h"
8+
#include"C24_Exercise_24.2.h"
9+
10+
using namespace std;
11+
12+
inline void error(string s) { throw runtime_error(s); }
13+
inline void error(const string& s, const string& s2) { error(s + s2); }
14+
inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); }
15+
inline void keep_window_open() { char ch; cin >> ch; }
16+
17+
int main()
18+
{
19+
enum Action {
20+
EXIT = -1, PRINTACTIONLIST,
21+
CASE1, CASE2, CASE3, CASE4, CASE5
22+
};
23+
const string actionList = "\tList of actions:\n"
24+
" (1) triple{}\n"
25+
" (-1) Exit, (0) Print the list of actions\n";
26+
cout << actionList;
27+
int action;
28+
bool cond{ true };
29+
while (cond) try {
30+
cout << "\nPlease enter the action: ";
31+
if (!(cin >> action)) { ClearInput(cin); error("Error. Incorrect input"); }
32+
char ch;
33+
cin.get(ch);
34+
switch (action) {
35+
case CASE1: {
36+
cout << endl;
37+
Numeric_lib::Matrix<int> mat(5);
38+
for (int i = 0; i < 5; ++i) mat[i] = i + 1;
39+
40+
Numeric_lib::Matrix<int> mat1(5);
41+
for (int i = 0; i < 5; ++i) mat1[i] = i + 1;
42+
Numeric_lib::Matrix<int> mat2(5);
43+
mat1.apply(triple1{});
44+
mat2 = Numeric_lib::apply(triple2{}, mat);
45+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
46+
cout << sp_2 << "Matrix<int> mat1.apply(triple1{}): " << mat1 << endl;
47+
cout << sp_2 << "Matrix<int> mat2 = Numeric_lib::apply(triple2{}, mat): " << mat2 << endl;
48+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
49+
50+
Numeric_lib::Matrix<int> mat3(5);
51+
for (int i = 0; i < 5; ++i) mat3[i] = i + 1;
52+
Numeric_lib::Matrix<int> mat4(5);
53+
mat3.apply(triple{});
54+
mat4 = Numeric_lib::apply(triple{}, mat);
55+
cout << endl;
56+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
57+
cout << sp_2 << "Matrix<int> mat3.apply(triple{}): " << mat3 << endl;
58+
cout << sp_2 << "Matrix<int> mat4 = Numeric_lib::apply(triple{}, mat): " << mat4 << endl;
59+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
60+
61+
Numeric_lib::Matrix<int> mat5(5);
62+
for (int i = 0; i < 5; ++i) mat5[i] = i + 1;
63+
Numeric_lib::Matrix<int> mat6(5);
64+
mat5.apply(multiply{ 5 });
65+
mat6 = Numeric_lib::apply(multiply{ 10 }, mat);
66+
cout << endl;
67+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
68+
cout << sp_2 << "Matrix<int> mat5.apply(multiply{ 5 }): " << mat5 << endl;
69+
cout << sp_2 << "Matrix<int> mat6 = Numeric_lib::apply(multiply{ 10 }, mat): " << mat6 << endl;
70+
cout << sp_2 << "Source Matrix<int> mat: " << mat << endl;
71+
72+
cout << vsp_2;
73+
break;
74+
}
75+
case PRINTACTIONLIST:
76+
cout << actionList;
77+
break;
78+
case EXIT:
79+
cond = false;
80+
break;
81+
default:
82+
error("Error. Incorrect action number");
83+
break;
84+
}
85+
}
86+
catch (runtime_error& e) {
87+
cerr << e.what() << endl;
88+
}
89+
catch (...) {
90+
cerr << "Error. Exception\n";
91+
return 1;
92+
}
93+
return 0;
94+
}
95+
96+
void ClearInput(istream& is)
97+
{
98+
is.clear();
99+
is.ignore(numeric_limits<streamsize>::max(), '\n');
100+
}

‎Chapter_24/C24_Exercise_24.2.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Exercise 24.2 */
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 triple1 {
16+
void operator()(int& elem) { elem *= 3; }
17+
};
18+
19+
struct triple2 {
20+
int operator()(const int& elem) { return elem * 3; }
21+
};
22+
23+
struct triple {
24+
int operator()(int& elem) { return elem *= 3; }
25+
};
26+
27+
class multiply {
28+
int mul;
29+
public:
30+
multiply(int multiplier) : mul{ multiplier } {}
31+
int operator()(int& elem) { return elem *= mul; }
32+
};

‎Chapter_24/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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_24")
7+
8+
set (CMAKE_CXX_STANDARD 14)
9+
set (CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/..)
10+
11+
12+
set (FILE_NAME C24_Exercise_24.1)
13+
set (SOURCE_CXX_LIST ${FILE_NAME}.cpp)
14+
set (FILE_NAME2 C24_Exercise_24.2)
15+
set (SOURCE_CXX_LIST2 ${FILE_NAME2}.cpp)
16+
17+
18+
# Add source to this project's executable.
19+
add_executable (${FILE_NAME} ${SOURCE_CXX_LIST})
20+
add_executable (${FILE_NAME2} ${SOURCE_CXX_LIST2})
21+
22+
23+
# TODO: Add tests and install targets if needed.
24+
install (TARGETS ${FILE_NAME} CONFIGURATIONS Debug DESTINATION Build/Debug)
25+
install (TARGETS ${FILE_NAME} CONFIGURATIONS Release DESTINATION Build)
26+
install (TARGETS ${FILE_NAME2} CONFIGURATIONS Debug DESTINATION Build/Debug)
27+
install (TARGETS ${FILE_NAME2} CONFIGURATIONS Release DESTINATION Build)

0 commit comments

Comments
(0)

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