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 61fdc81

Browse files
committed
Exercise 23.6; Exercise 23.7; Exercise 23.8
1 parent 3539049 commit 61fdc81

9 files changed

+595
-0
lines changed

‎Chapter_23/C23_Exercise_23.6.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* Exercise 23.6 */
2+
3+
#include<iostream>
4+
#include<istream>
5+
#include<fstream>
6+
#include<sstream>
7+
#include<iomanip>
8+
#include<string>
9+
#include<vector>
10+
#include<regex>
11+
#include"C23_Exercise_23.6.h"
12+
13+
using namespace std;
14+
15+
inline void error(string s) { throw runtime_error(s); }
16+
inline void error(const string& s, const string& s2) { error(s + s2); }
17+
inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); }
18+
inline void keep_window_open() { char ch; cin >> ch; }
19+
20+
int main()
21+
{
22+
enum Action {
23+
EXIT = -1, PRINTACTIONLIST,
24+
CASE1, CASE2, CASE3, CASE4, CASE5
25+
};
26+
const string actionList = "\tList of actions:\n"
27+
" (1) IsDate()\n"
28+
" (-1) Exit, (0) Print the list of actions\n";
29+
cout << actionList;
30+
int action;
31+
bool cond{ true };
32+
while (cond) try {
33+
cout << "\nPlease enter the action: ";
34+
if (!(cin >> action)) { ClearInput(cin); error("Error. Incorrect input"); }
35+
char ch;
36+
cin.get(ch);
37+
switch (action) {
38+
case CASE1: {
39+
cout << endl;
40+
cout << sp_2 << "Please enter a file name: ";
41+
string fileName;
42+
getline(cin, fileName);
43+
if (!cin) error("Error. Incorrect input");
44+
ifstream ifs{ fileName };
45+
if(!ifs) error("Error. File opening error");
46+
47+
size_t lineNum = 0;
48+
cout << sp_4 << "[Format] Line number: line" << endl;
49+
while (true) {
50+
++lineNum;
51+
string s;
52+
getline(ifs, s);
53+
if (!ifs) {
54+
if (ifs.eof()) break;
55+
else error("Error. Read error");
56+
}
57+
if (IsDate(s)) {
58+
cout << sp_4 << lineNum << ": " << s << endl;
59+
}
60+
}
61+
62+
cout << vsp_2;
63+
break;
64+
}
65+
case PRINTACTIONLIST:
66+
cout << actionList;
67+
break;
68+
case EXIT:
69+
cond = false;
70+
break;
71+
default:
72+
error("Error. Incorrect action number");
73+
break;
74+
}
75+
}
76+
catch (runtime_error& e) {
77+
cerr << e.what() << endl;
78+
}
79+
catch (...) {
80+
cerr << "Error. Exception\n";
81+
return 1;
82+
}
83+
return 0;
84+
}
85+
86+
void ClearInput(istream& is)
87+
{
88+
is.clear();
89+
is.ignore(numeric_limits<streamsize>::max(), '\n');
90+
}
91+
92+
bool IsDate(const string& s)
93+
{
94+
/* date pattern:
95+
dd/mm/yy OR dd/mm/yyyy OR dd-mm-yy OR dd-mm-yyyy OR dd.mm.yy OR dd.mm.yyyy OR
96+
mm/dd/yy OR mm/dd/yyyy OR mm-dd-yy OR mm-dd-yyyy OR mm.dd.yy OR mm.dd.yyyy OR
97+
yy/mm/dd OR yyyy/mm/dd OR yy-mm-dd OR yyyy-mm-dd OR yy.mm.dd OR yyyy.mm.dd OR
98+
yy/dd/mm OR yyyy/dd/mm OR yy-dd-mm OR yyyy-dd-mm OR yy.dd.mm OR yyyy.dd.mm
99+
*/
100+
string db{ R"((^|\s|$))" }; // date boundary
101+
string sep{ R"([./-])" }; // separator list
102+
string d{ R"((0?[1-9]|[12]\d|3[01]))" }; // day: D or DD
103+
string m{ R"((0?[1-9]|1[0-2]))" }; // month: M or MM
104+
string y{ R"(\d\d(\d\d)?)" }; // year: YY or YYYY
105+
regex pattern{
106+
db +
107+
'(' +
108+
'(' + d + sep + m + '|' + m + sep + d + ')' + sep + y
109+
+ '|' +
110+
y + sep + '(' + d + sep + m + '|' + m + sep + d + ')'
111+
+ ')'
112+
+ db
113+
};
114+
if (regex_search(s, pattern)) return true;
115+
return false;
116+
}

‎Chapter_23/C23_Exercise_23.6.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Exercise 23.6 */
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+
bool IsDate(const string& s);

‎Chapter_23/C23_Exercise_23.6_text.txt

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
According to Merriam-Webster's Medical Dictionary, 21/12/2001
2+
pneumonoultramicroscopicsilicovolcanoconiosis 21/12/2001 is a
3+
21/12/2001 pneumoconiosis
4+
caused by inhalation 21/12/2001
5+
21/12/2001 of very fine silicate or quartz dust.
6+
23/12/2000
7+
23/12/2000 gfdgfd
8+
gfdgdfg 23/12/2000
9+
23/12/2000
10+
23/12/2000 dfsdf
11+
sdfsdf 23/12/2000
12+
13+
01 true 2/22/90
14+
02 true 2/2/1990
15+
03 true 12/03/90
16+
04 true 12/23/2000
17+
05 true 23/12/2000
18+
06 true 03/12/2000
19+
07 true 3/12/20
20+
08 true 23/2/20
21+
09 true 23/02/2000
22+
10 true 23/12/2000
23+
24+
11 true 2-29-1990
25+
12 true 29-2-1990
26+
13 true 1-1-00
27+
14 true 01-01-00
28+
15 true 31-01-00
29+
16 true 1-31-00
30+
17 true 28-02-2020
31+
18 true 31-10-2020
32+
19 true 10-31-2020
33+
20 true 31-12-2020
34+
35+
21 true 02.29.00
36+
22 true 29.02.00
37+
23 true 1.1.00
38+
24 true 01.01.00
39+
25 true 31.01.2000
40+
26 true 1.31.2000
41+
27 true 28.02.2020
42+
28 true 31.10.2020
43+
29 true 10.31.2020
44+
30 true 31.12.2020
45+
46+
31 true 90/2/22
47+
32 true 1990年2月2日
48+
33 true 90/12/03
49+
34 true 2000年12月23日
50+
35 true 20002312
51+
36 true 2000年03月12日
52+
37 true 20/3/12
53+
38 true 20/23/2
54+
39 true 20002302
55+
40 true 20002312
56+
57+
41 true 1990-2-29
58+
42 true 1990-29-2
59+
43 true 00-1-1
60+
44 true 00-01-01
61+
45 true 00-31-01
62+
46 true 00-1-31
63+
47 true 2020-28-02
64+
48 true 2020-31-10
65+
49 true 2020年10月31日
66+
50 true 2020-31-12
67+
68+
51 true 00.02.29
69+
52 true 00.29.02
70+
53 true 00.1.1
71+
54 true 00.01.01
72+
55 true 20003101
73+
56 true 2000年1月31日
74+
57 true 20202802
75+
58 true 20203110
76+
59 true 2020年10月31日
77+
60 true 20203112
78+
79+
101 false 2/22/900
80+
102 false 22/22/1990
81+
103 false 13/13/90
82+
104 false 12/23/200
83+
105 false 23/12/200
84+
106 false 033/12/2000
85+
107 false 32/12/2000
86+
108 false 33/14/20
87+
109 false 00/2/2000
88+
110 false 23/0/20
89+
90+
111 false 29-29-1990
91+
112 false 29-2-199
92+
113 false 0-1-00
93+
114 false 01-0-00
94+
115 false 32-01-00
95+
116 false 13-31-00
96+
117 false 28-22-2000
97+
118 false 32-10-2000
98+
119 false 13-31-2020
99+
120 false 31-12-20204
100+
101+
121 false 02.29.000
102+
122 false 29.02.000
103+
123 false 13.13.00
104+
124 false 91.01.00
105+
125 false 31.21.2000
106+
126 false 1.32.2000
107+
127 false 28.22.2020
108+
128 false 31.10.202
109+
129 false 10.31.200
110+
130 false 111.12.2020
111+
112+
131 false 900/2/22
113+
132 false 19902222
114+
133 false 90/13/13
115+
134 false 200/12/23
116+
135 false 200/23/12
117+
136 false 2000/033/12
118+
137 false 20003212
119+
138 false 20/33/14
120+
139 false 2000002
121+
140 false 20/23/0
122+
123+
141 false 1990-29-29
124+
142 false 199-29-2
125+
143 false 00-0-1
126+
144 false 00-01-0
127+
145 false 00-32-01
128+
146 false 00-13-31
129+
147 false 2000-28-22-2000
130+
148 false 2000-32-10-2000
131+
149 false 202013-31-2020
132+
150 false 20204-31-12
133+
134+
151 false 000.02.29
135+
152 false 000.29.02.000
136+
153 false 00.13.13.00
137+
154 false 00.91.01
138+
155 false 20003121
139+
156 false 2000132
140+
157 false 20202822.2020
141+
158 false 20203110.202
142+
159 false 200.10.31
143+
160 false 2020.111.12
144+
145+

0 commit comments

Comments
(0)

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