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 6b2d4ba

Browse files
committed
Exercise 24.10; Exercise 24.11; Exercise 24.12
1 parent 44c945f commit 6b2d4ba

File tree

8 files changed

+468
-0
lines changed

8 files changed

+468
-0
lines changed

‎Chapter_24/C24_Exercise_24.10.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Exercise 24.10 */
2+
3+
#include<iostream>
4+
#include<sstream>
5+
#include<iomanip>
6+
#include<vector>
7+
#include<random>
8+
#include"C24_Exercise_24.10.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) randint()\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+
cout << "Please enter the n (range [0,n]): ";
38+
int n;
39+
if (!(cin >> n)) { ClearInput(cin); error("Error. Incorrect input"); }
40+
cout << "Please enter the d (number of times): ";
41+
int d;
42+
if (!(cin >> d)) { ClearInput(cin); error("Error. Incorrect input"); }
43+
vector<int> randoms;
44+
randoms.resize(n + 1, 0);
45+
for (int i = 0; i < d; ++i) {
46+
++randoms[randint(n)];
47+
}
48+
cout << endl;
49+
for (int i = 0; i < randoms.size(); ++i) {
50+
cout << setw(3) << right << i << ") ";
51+
for (int j = 0; j < randoms[i]; ++j) {
52+
cout << '*';
53+
}
54+
cout << endl;
55+
}
56+
57+
cout << vsp_2;
58+
break;
59+
}
60+
case PRINTACTIONLIST:
61+
cout << actionList;
62+
break;
63+
case EXIT:
64+
cond = false;
65+
break;
66+
default:
67+
error("Error. Incorrect action number");
68+
break;
69+
}
70+
}
71+
catch (runtime_error& e) {
72+
cerr << e.what() << endl;
73+
}
74+
catch (...) {
75+
cerr << "Error. Exception\n";
76+
return 1;
77+
}
78+
return 0;
79+
}
80+
81+
void ClearInput(istream& is)
82+
{
83+
is.clear();
84+
is.ignore(numeric_limits<streamsize>::max(), '\n');
85+
}
86+
87+
int randint(int min, int max)
88+
{
89+
static default_random_engine ran;
90+
return uniform_int_distribution<>{min, max}(ran);
91+
}
92+
93+
int randint(int max)
94+
{
95+
return randint(0, max);
96+
}

‎Chapter_24/C24_Exercise_24.10.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Exercise 24.10 */
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+
int randint(int min, int max);
15+
int randint(int max);

‎Chapter_24/C24_Exercise_24.11.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* Exercise 24.11 */
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.11.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) swap_columns()\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+
int a[3][3] =
38+
{
39+
{1,2,3},
40+
{4,5,6},
41+
{7,8,9}
42+
};
43+
44+
Numeric_lib::Matrix<int, 2> A(a);
45+
cout << "A =\n" << A << endl;
46+
A.swap_columns(1, 2);
47+
cout << endl << "A.swap_columns(2, 3);" << endl;
48+
cout << "A =\n" << A << endl;
49+
A.swap_columns(0, 2);
50+
cout << endl << "A.swap_columns(0, 2);" << endl;
51+
cout << "A =\n" << A << endl;
52+
53+
cout << endl << endl;
54+
int b[3][5] =
55+
{
56+
{1,2,3,4,5},
57+
{11,12,13,14,15},
58+
{55,66,77,88,99}
59+
};
60+
61+
Numeric_lib::Matrix<int, 2> B(b);
62+
cout << "B =\n" << B << endl;
63+
B.swap_columns(0, 4);
64+
cout << endl << "B.swap_columns(0, 4);" << endl;
65+
cout << "B =\n" << B << endl;
66+
B.swap_columns(2, 3);
67+
cout << endl << "B.swap_columns(2, 3);" << endl;
68+
cout << "B =\n" << B << endl;
69+
70+
71+
cout << vsp_2;
72+
break;
73+
}
74+
case PRINTACTIONLIST:
75+
cout << actionList;
76+
break;
77+
case EXIT:
78+
cond = false;
79+
break;
80+
default:
81+
error("Error. Incorrect action number");
82+
break;
83+
}
84+
}
85+
catch (runtime_error& e) {
86+
cerr << e.what() << endl;
87+
}
88+
catch (...) {
89+
cerr << "Error. Exception\n";
90+
return 1;
91+
}
92+
return 0;
93+
}
94+
95+
void ClearInput(istream& is)
96+
{
97+
is.clear();
98+
is.ignore(numeric_limits<streamsize>::max(), '\n');
99+
}

‎Chapter_24/C24_Exercise_24.11.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Exercise 24.11 */
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);

0 commit comments

Comments
(0)

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