-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch21.ixx
119 lines (112 loc) · 3.24 KB
/
ch21.ixx
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
118
119
export module ch21;
import std;
export namespace ch21 {
namespace exercises {
namespace ex1 {
using namespace std;
void test() {
{
wcout << "Your decimal separator is "
<< use_facet<numpunct<wchar_t>>(locale{ "" }).
decimal_point() << '\n';
wcout << "Your separator between digit groups is "
<< use_facet<numpunct<wchar_t>>(locale{ "" }).
thousands_sep() << '\n';
}
{
locale userLocale{ "" };
auto& facet{ use_facet<numpunct<char>>(userLocale) };
println("Decimal separator: {}", facet.decimal_point());
}
}
}
namespace ex2 {
using namespace std;
void test() {
{
regex reg{ "(\\d{3})-(\\d{3})-(\\d{4})" };
const string replacement{ "$1\n$2\n$3" };
while (true) {
print("Enter a phone number (xxx-xxx-xxxx) (q=quit):");
string str;
if (!getline(cin, str) || str == "q") { break; }
if (regex_match(str, reg)) {
println("Valid phone number.");
cout << regex_replace(str, reg, replacement,
regex_constants::format_no_copy) << endl;
}
else { println(" Invalid phone number!"); }
}
}
{
regex r{ "(\\d{3})-(\\d{3})-(\\d{4})" };
while (true) {
print("Enter a US phone number (xxx-xxx-xxxx) (q=quit): ");
string str;
if (!getline(cin, str) || str == "q") {
break;
}
if (smatch m; regex_match(str, m, r)) {
println(" Valid phone number:");
println("{}\n{}\n{}", m[1].str(), m[2].str(), m[3].str());
}
else {
println(" Invalid phone number!");
}
}
}
}
}
namespace ex3 {
using namespace std;
void clearInputBuffer() {
cin.clear(); // Clear any error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore remaining input
}
void test() {
regex r{ "//\\s*(.+)$" };
const string replacement{ "" };
while (true) {
print("Enter a multiple line code snippet that contains single line comment with @ as end(type \"q@\" to quit):\n");
string str;
clearInputBuffer(); // Clear the input buffer before reading
if (!getline(cin, str, '@') || str=="q") {
break;
}
string result{ regex_replace(str,r,replacement) };
println("Original string: '{}'", str);
println("New string : '{}'", result);
}
}
void test_answer() {
regex r{ "^(.*?)//.*$" };
const string replacement{ "$1" };
while (true) {
println("Enter a source code snippet, terminated with @:");
string str;
if (!getline(cin, str, '@')) {
break;
}
println("{}", regex_replace(str, r, replacement));
}
}
}
namespace ex4 {
using namespace std;
void test() {
regex reg{ "(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]])(?=(.*[[:digit:]]){2,}).{8,}" };
while (true) {
print("Enter a password, that consists of at least one lowercase letter,\n"
"at least one uppercase letter, at least one punctuation character,\n"
"at least two digits, and is at least eight characters long(q = quit) :\n");
string str;
if (!getline(cin, str) || str == "q") { break; }
if (regex_match(str, reg)) {
println("Valid password.");
}
else { println(" Invalid password!"); }
}
}
}
}
}