为什么这个 C++基于 Luhn 算法检查信用卡的代码行不通?
我编写此代码是为了根据 Luhn 算法检查信用卡号是否有效:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool ccc(string cc) {
vector<string> digits;
int aux;
for (int n = 0; n < cc.length(); ++n) {
digits.push_back(to_string(cc.at(n)));
cout << digits[n];
}
for (int s = 1; s < digits.size(); s += 2) {
aux = stoi(digits[s]);
aux *= 2;
digits[s] = to_string(aux);
aux = 0;
for (int f = 0; f < digits[s].length(); ++f) {
aux += stoi(to_string(digits[s].at(f)));
}
digits[s] = to_string(aux);
aux = 0;
}
for (int b = 0; b < digits.size(); ++b) {
aux += stoi(digits[b]);
}
aux *= 9;
if (aux % 10 == 0) {
return true;
} else {
return false;
}
}
int main(int argc, char* argv[]) {
string crecar;
cout << "CC:\n";
cin >> crecar;
if (ccc(crecar)) {
cout << "Valid\n";
} else {
cout << "Invalid\n";
}
}
我编译时没有错误或警告,并得到以下输出:
CC:
4895045418823857
52565753485253524956565051565355Invalid
它应该首先打印每个数字,我用它来检查什么是程序内部执行的操作,但我得到的数字太大了。为什么这个程序失败了?我应该做什么来修复它?
I wrote this code in to check if a credit card number is valid according to the Luhn algorithm:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool ccc(string cc) {
vector<string> digits;
int aux;
for (int n = 0; n < cc.length(); ++n) {
digits.push_back(to_string(cc.at(n)));
cout << digits[n];
}
for (int s = 1; s < digits.size(); s += 2) {
aux = stoi(digits[s]);
aux *= 2;
digits[s] = to_string(aux);
aux = 0;
for (int f = 0; f < digits[s].length(); ++f) {
aux += stoi(to_string(digits[s].at(f)));
}
digits[s] = to_string(aux);
aux = 0;
}
for (int b = 0; b < digits.size(); ++b) {
aux += stoi(digits[b]);
}
aux *= 9;
if (aux % 10 == 0) {
return true;
} else {
return false;
}
}
int main(int argc, char* argv[]) {
string crecar;
cout << "CC:\n";
cin >> crecar;
if (ccc(crecar)) {
cout << "Valid\n";
} else {
cout << "Invalid\n";
}
}
And I compile with no errors or warnings and I get the following output:
CC:
4895045418823857
52565753485253524956565051565355Invalid
It should print first every digit of digits, I used that to check what is the program doing internally, but I get that number that is too big. Why is this program failing and what should I do to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请查阅
to_string
文档(示例)您会发现char
没有重载,但是char
很容易转换为int
,因此int<将使用 /code> 重载。不幸的是你没有得到这个数字。您可以获得字符的编码值。因此,假设 ascii,
to_string('4')
不会返回包含 4 的string
,它将返回包含 ascii 的string
值为 4: 52。如果您将这个奇怪的数字视为 2 位数字的流,您将看到原始数字的 ASCII 表示形式。
此问题的破解方法是更改
为
构造一个包含给定字符的 1 个副本的字符串,并将其放置在向量中
立即提高可读性是
获得子 -我们正在查看的当前字符的字符串并将其放入
digits
中。但是从
string
到int
再到string
的所有这些都是make-work。只需存储int
并对其进行操作即可。如果您有数字,可以使用digit -'0'
将数字转换为int
,然后使用digit + '0'
再次转换回来。这将为您和计算机节省很多精力。Consult the
to_string
documentation (example) and you'll see that there is no overload forchar
, butchar
is easily convertible to anint
, so theint
overload will be used. Unfortunately you don't get the digit. You get the character's encoded value. So, assuming ascii,to_string('4')
won't return astring
containing 4, it'll return astring
containing the ascii value of 4: 52.If you look at the strange number as a stream of 2 digit numbers you'll see you have an ascii representation of your original number.
The hack fix for this problem is to change
to
construct a
string
that contains 1 copy of the given character and place it in thevector
An immediate readability improvement is
get a sub-string of the current character we're looking at and put it in
digits
.But all this to
string
toint
back tostring
is make-work. Just storeint
s and operate onint
s . If you have digits you can convert digit into anint
withdigit -'0'
and back again withdigit + '0'
. This will save you and the the computer a lot of effort.我不知道为什么你的代码这么长。根据有关算法的维基百科页面:
我不知道你从什么开始,但你的看起来真的很奇怪。
I'm not sure why your code is so long. According to the wikipedia page on the algorithm in question:
I don't know what you started with, but yours just seems really weird.