-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe7.cpp
96 lines (82 loc) · 2.25 KB
/
e7.cpp
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
//
// Created by martin on 10/22/22.
//
#include "../std_lib.h"
using namespace std;
//American Express 34 or 37
//Discover 65, 644, 6011
int is_prefix (const string &line, const string &prefix)
{
regex pattern { "^" + prefix };
smatch matches;
if (regex_search(line, matches, pattern))
{
return int(prefix.size());
}
return 0;
}
bool is_discovery (const string &card_string)
{
vector<string> prefixes { "65", "644", "6011" };
for (auto const &prefix : prefixes)
{
if (is_prefix(card_string, prefix))
{
return true;
}
}
return false;
}
bool is_american_express (const string &card_string)
{
vector<string> prefixes { "34", "37" };
return any_of(prefixes.begin(), prefixes.end(),
[&] (const string &prefix)
{
return is_prefix(card_string, prefix);
});
// for (auto const &prefix : prefixes)
// {
// if (is_prefix(card_string, prefix))
// {
// return true;
// }
// }
// return false;
}
int main (int argc, char *argv[])
{
ifstream in { "../c23/cc.txt" };
if (!in)
{
cerr << "no file" << endl;
}
regex pattern { R"((\d{4})-?(\d{4})-?(\d{4})-?(\d{2,4}))" };
auto line_no { 0 };
for (string line ; getline(in, line) ; /* */)
{
++line_no;
line.erase(std::remove_if(line.begin(), line.end(), ::isspace), line.end());
smatch matches;
if (regex_match(line, matches, pattern))
{
if (is_discovery(matches[1]))
{
cout << "Found a Discovery card: "
<< matches[1] << " "
<< matches[2] << " "
<< matches[3] << " "
<< matches[4] << endl;
}
if (is_american_express(matches[1]))
{
cout << "Found a American Express card: "
<< matches[1] << " "
<< matches[2] << " "
<< matches[3] << " "
<< matches[4] << endl;
}
}
}
return EXIT_SUCCESS;
}