-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOcrAccount.cpp
94 lines (73 loc) · 1.83 KB
/
OcrAccount.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
#include <algorithm>
#include "OcrAccount.h"
using std::vector;
using std::string;
using std::equal;
OcrAccount::OcrAccount(const vector<string>& rhs)
{
for(int i = 0; i < 9; ++i)
value_.push_back(OcrDigit(slice(i, rhs)));
}
OcrAccount::operator string() const
{
string value;
for(int i = 0; i < 9; ++i)
value += value_[i];
return value;
}
bool OcrAccount::isValid()
{
string account = *this;
return hasValidChecksum(account);
}
bool OcrAccount::isLegible()
{
string account = *this;
return account.find('?') == string::npos;
}
bool OcrAccount::operator==(const OcrAccount& rhs) const
{
return equal(value_.begin(), value_.end(), rhs.value_.begin());
}
vector<string> OcrAccount::correctError() const
{
vector<string> value;
string account = *this;
size_t index = account.find("?");
if (index == string::npos)
{
for(int i = 0; i < 9; ++i)
computeCorrection(i, account, value);
}
else
computeCorrection(index, account, value);
return value;
}
vector<string> OcrAccount::slice(int index, const vector<string>& v)
{
vector<string> digit;
for(int i = 0; i < 3; ++i)
digit.push_back(v[i].substr(index*3, 3));
return digit;
}
bool OcrAccount::hasValidChecksum(const std::string& account)
{
if (account.find("?") != string::npos)
return false;
int checksum = 0;
int multiplier = 10;
while(--multiplier)
checksum += (account[9 - multiplier] - '0') * multiplier;
checksum %= 11;
return checksum == 0;
}
void OcrAccount::computeCorrection(int i, string account, vector<string>& value) const
{
vector<char> related = value_[i].related();
for(auto c: related)
{
account[i] = c;
if (hasValidChecksum(account))
value.push_back(account);
}
}