CS50信用问题万事达卡号为什么要无效
CS50 credit problem why should mastercard number be invalid
我正在尝试为 CS50 编写信用问题代码,它适用于大多数号码,但是当我 运行 支票时,我收到一条错误消息,提示我的代码正在返回 Mastercard 5555 5555 5555 4444,应该是无效的。手动弄清楚,我不明白为什么那张卡会无效。每第二个数字加倍:10 (1) + 10 (1) + 10 (1) + 10 (1) + 10 (1) + 10 (1) + 8 + 8 = 22。然后 22 + 5 + 5 + 5 + 5 + 5 + 5 + 4 + 4 = 50。50 是 10 的倍数,数字长 16 位,以 55 开头,那为什么不是万事达卡?
我对 post 我的代码感到尴尬,因为我知道它非常麻烦,而且肯定有更快、更优雅的方法来做同样的事情,但这是我第一次尝试编程,所以我有以我脑海中唯一合乎逻辑的方式去做——而且我只知道 class.
中显示的脚本
总之,就到这里了,大家不要笑,哈哈!
// To determine if a user's credit card number is valid and which company it belongs to
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// To get user input of credit card number
long card;
do
{
card = get_long("What is your credit card number: ");
}
while (card < 0);
// To divide number into digits
int a = card % 10;
int b = card % 100 - a;
int bb = b / 10;
int c = card % 1000 - b - a;
int cc = c / 100;
int d = card % 10000 - c - b - a;
int dd = d / 1000;
int e = card % 100000 - d - c - b - a;
int ee = e / 10000;
int f = card % 1000000 - e - d - c - b - a;
int ff = f / 100000;
int g = card % 10000000 - f - e - d - c - b - a;
int gg = g / 1000000;
int h = card % 100000000 - g - f - e - d - c - b - a;
int hh = h / 10000000;
int i = card % 1000000000 - h - g - f - e - d - c - b - a;
int ii = i / 100000000;
int j = card % 10000000000 - i - h - g - f - e - d - c - b - a;
int jj = j / 1000000000;
int k = card % 100000000000 - j - i - h - g - f - e - d - c - b - a;
int kk = k / 10000000000;
int l = card % 1000000000000 - k - j - i - h - g - f - e - d - c - b - a;
int ll = l / 100000000000;
int m = card % 10000000000000 - l - k - j - i - h - g - f - e - d - c - b - a;
int mm = m / 1000000000000;
int n = card % 100000000000000 - m - l - k - j - i - h - g - f - e - d - c - b - a;
int nn = n / 10000000000000;
int o = card % 1000000000000000 - n - m - l - k - j - i - h - g - f - e - d - c - b - a;
int oo = o / 100000000000000;
int p = card % 10000000000000000 - o - n - m - l - k - j - i - h - g - f - e - d - c - b - a;
int pp = p / 1000000000000000;
// To multiply each digit and reduce to one digit
int aaa = a * 2;
if (aaa > 9)
{
int ab = aaa % 10;
int ac = aaa % 100 - ab;
int ad = ac / 10;
aaa = ad + ab;
}
int bbb = bb * 2;
if (bbb > 9)
{
int ba = bbb % 10;
int bc = bbb % 100 - ba;
int bd = bc / 10;
bbb = bd + ba;
}
int ccc = cc * 2;
if (ccc > 9)
{
int cb = ccc % 10;
int ca = ccc % 100 - cb;
int cd = ca / 10;
ccc = cd + cb;
}
int ddd = dd * 2;
if (ddd > 9)
{
int db = ddd % 10;
int dc = ddd % 100 - db;
int da = dc / 10;
ddd = da + db;
}
int eee = ee * 2;
if (eee > 9)
{
int eb = eee % 10;
int ec = eee % 100 - eb;
int ed = ec / 10;
eee = ed + eb;
}
int fff = ff * 2;
if (fff > 9)
{
int fb = fff % 10;
int fc = fff % 100 - fb;
int fd = fc / 10;
fff = fd + fb;
}
int ggg = gg * 2;
if (ggg > 9)
{
int gb = ggg % 10;
int gc = ggg % 100 - gb;
int gd = gc / 10;
ggg = gd + gb;
}
int hhh = hh * 2;
if (hhh > 9)
{
int hb = hhh % 10;
int hc = hhh % 100 - hb;
int hd = hc / 10;
hhh = hd + hb;
}
int iii = ii * 2;
if (iii > 9)
{
int ib = iii % 10;
int ic = iii % 100 - ib;
int id = ic / 10;
iii = id + ib;
}
int jjj = jj * 2;
if (jjj > 9)
{
int jb = jjj % 10;
int jc = jjj % 100 - jb;
int jd = jc / 10;
jjj = jd + jb;
}
int kkk = kk * 2;
if (kkk > 9)
{
int kb = kkk % 10;
int kc = kkk % 100 - kb;
int kd = kc / 10;
kkk = kd + kb;
}
int lll = ll * 2;
if (lll > 9)
{
int lb = lll % 10;
int lc = lll % 100 - lb;
int ld = lc / 10;
lll = ld + lb;
}
int mmm = mm * 2;
if (mmm > 9)
{
int mb = mmm % 10;
int mc = mmm % 100 - mb;
int md = mc / 10;
mmm = md + mb;
}
int nnn = nn * 2;
if (nnn > 9)
{
int nb = nnn % 10;
int nc = nnn % 100 - nb;
int nd = nc / 10;
nnn = nd + nb;
}
int ooo = oo * 2;
if (ooo > 9)
{
int ob = ooo % 10;
int oc = ooo % 100 - ob;
int od = oc / 10;
ooo = od + ob;
}
int ppp = pp * 2;
if (ppp > 9)
{
int pb = ppp % 10;
int pc = ppp % 100 - pb;
int pd = pc / 10;
ppp = pd + pb;
}
// To determine if card is valid
int pra = bbb + ddd + fff + hhh + jjj + lll + nnn + ppp;
int prb = pra + aaa + ccc + eee + ggg + iii + kkk + mmm + ooo;
// To determine if card is AMEX
if (card >= 370000000000000 && card < 380000000000000)
{
printf("AMEX\n");
}
else if (card >= 340000000000000 && card < 350000000000000)
{
printf("AMEX\n");
}
// To determine if card is MASTERCARD
else if (card >= 5100000000000000 && card < 5500000000000000)
{
printf("MASTERCARD\n");
}
// To determine if card is VISA
else if (card >= 4000000000000 && card < 5000000000000)
{
printf("VISA\n");
}
else if (card >= 4000000000000000 && card < 5000000000000000)
{
printf("VISA\n");
}
// To determine if card is INVALID
else
{
printf("INVALID\n");
}
}
我看到您经常使用单个整数,代码变得超长且不可维护。这是一个非常糟糕的设计。
而不是整数,使用string
来存储信用卡号。要达到信用卡号中的个人号码,您可以使用
card = get_string("What is your credit card number: ");
card[i]
i
是信用卡号的索引。从零开始,循环遍历所有数字,然后进行计算。
我正在尝试为 CS50 编写信用问题代码,它适用于大多数号码,但是当我 运行 支票时,我收到一条错误消息,提示我的代码正在返回 Mastercard 5555 5555 5555 4444,应该是无效的。手动弄清楚,我不明白为什么那张卡会无效。每第二个数字加倍:10 (1) + 10 (1) + 10 (1) + 10 (1) + 10 (1) + 10 (1) + 8 + 8 = 22。然后 22 + 5 + 5 + 5 + 5 + 5 + 5 + 4 + 4 = 50。50 是 10 的倍数,数字长 16 位,以 55 开头,那为什么不是万事达卡?
我对 post 我的代码感到尴尬,因为我知道它非常麻烦,而且肯定有更快、更优雅的方法来做同样的事情,但这是我第一次尝试编程,所以我有以我脑海中唯一合乎逻辑的方式去做——而且我只知道 class.
中显示的脚本总之,就到这里了,大家不要笑,哈哈!
// To determine if a user's credit card number is valid and which company it belongs to
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// To get user input of credit card number
long card;
do
{
card = get_long("What is your credit card number: ");
}
while (card < 0);
// To divide number into digits
int a = card % 10;
int b = card % 100 - a;
int bb = b / 10;
int c = card % 1000 - b - a;
int cc = c / 100;
int d = card % 10000 - c - b - a;
int dd = d / 1000;
int e = card % 100000 - d - c - b - a;
int ee = e / 10000;
int f = card % 1000000 - e - d - c - b - a;
int ff = f / 100000;
int g = card % 10000000 - f - e - d - c - b - a;
int gg = g / 1000000;
int h = card % 100000000 - g - f - e - d - c - b - a;
int hh = h / 10000000;
int i = card % 1000000000 - h - g - f - e - d - c - b - a;
int ii = i / 100000000;
int j = card % 10000000000 - i - h - g - f - e - d - c - b - a;
int jj = j / 1000000000;
int k = card % 100000000000 - j - i - h - g - f - e - d - c - b - a;
int kk = k / 10000000000;
int l = card % 1000000000000 - k - j - i - h - g - f - e - d - c - b - a;
int ll = l / 100000000000;
int m = card % 10000000000000 - l - k - j - i - h - g - f - e - d - c - b - a;
int mm = m / 1000000000000;
int n = card % 100000000000000 - m - l - k - j - i - h - g - f - e - d - c - b - a;
int nn = n / 10000000000000;
int o = card % 1000000000000000 - n - m - l - k - j - i - h - g - f - e - d - c - b - a;
int oo = o / 100000000000000;
int p = card % 10000000000000000 - o - n - m - l - k - j - i - h - g - f - e - d - c - b - a;
int pp = p / 1000000000000000;
// To multiply each digit and reduce to one digit
int aaa = a * 2;
if (aaa > 9)
{
int ab = aaa % 10;
int ac = aaa % 100 - ab;
int ad = ac / 10;
aaa = ad + ab;
}
int bbb = bb * 2;
if (bbb > 9)
{
int ba = bbb % 10;
int bc = bbb % 100 - ba;
int bd = bc / 10;
bbb = bd + ba;
}
int ccc = cc * 2;
if (ccc > 9)
{
int cb = ccc % 10;
int ca = ccc % 100 - cb;
int cd = ca / 10;
ccc = cd + cb;
}
int ddd = dd * 2;
if (ddd > 9)
{
int db = ddd % 10;
int dc = ddd % 100 - db;
int da = dc / 10;
ddd = da + db;
}
int eee = ee * 2;
if (eee > 9)
{
int eb = eee % 10;
int ec = eee % 100 - eb;
int ed = ec / 10;
eee = ed + eb;
}
int fff = ff * 2;
if (fff > 9)
{
int fb = fff % 10;
int fc = fff % 100 - fb;
int fd = fc / 10;
fff = fd + fb;
}
int ggg = gg * 2;
if (ggg > 9)
{
int gb = ggg % 10;
int gc = ggg % 100 - gb;
int gd = gc / 10;
ggg = gd + gb;
}
int hhh = hh * 2;
if (hhh > 9)
{
int hb = hhh % 10;
int hc = hhh % 100 - hb;
int hd = hc / 10;
hhh = hd + hb;
}
int iii = ii * 2;
if (iii > 9)
{
int ib = iii % 10;
int ic = iii % 100 - ib;
int id = ic / 10;
iii = id + ib;
}
int jjj = jj * 2;
if (jjj > 9)
{
int jb = jjj % 10;
int jc = jjj % 100 - jb;
int jd = jc / 10;
jjj = jd + jb;
}
int kkk = kk * 2;
if (kkk > 9)
{
int kb = kkk % 10;
int kc = kkk % 100 - kb;
int kd = kc / 10;
kkk = kd + kb;
}
int lll = ll * 2;
if (lll > 9)
{
int lb = lll % 10;
int lc = lll % 100 - lb;
int ld = lc / 10;
lll = ld + lb;
}
int mmm = mm * 2;
if (mmm > 9)
{
int mb = mmm % 10;
int mc = mmm % 100 - mb;
int md = mc / 10;
mmm = md + mb;
}
int nnn = nn * 2;
if (nnn > 9)
{
int nb = nnn % 10;
int nc = nnn % 100 - nb;
int nd = nc / 10;
nnn = nd + nb;
}
int ooo = oo * 2;
if (ooo > 9)
{
int ob = ooo % 10;
int oc = ooo % 100 - ob;
int od = oc / 10;
ooo = od + ob;
}
int ppp = pp * 2;
if (ppp > 9)
{
int pb = ppp % 10;
int pc = ppp % 100 - pb;
int pd = pc / 10;
ppp = pd + pb;
}
// To determine if card is valid
int pra = bbb + ddd + fff + hhh + jjj + lll + nnn + ppp;
int prb = pra + aaa + ccc + eee + ggg + iii + kkk + mmm + ooo;
// To determine if card is AMEX
if (card >= 370000000000000 && card < 380000000000000)
{
printf("AMEX\n");
}
else if (card >= 340000000000000 && card < 350000000000000)
{
printf("AMEX\n");
}
// To determine if card is MASTERCARD
else if (card >= 5100000000000000 && card < 5500000000000000)
{
printf("MASTERCARD\n");
}
// To determine if card is VISA
else if (card >= 4000000000000 && card < 5000000000000)
{
printf("VISA\n");
}
else if (card >= 4000000000000000 && card < 5000000000000000)
{
printf("VISA\n");
}
// To determine if card is INVALID
else
{
printf("INVALID\n");
}
}
我看到您经常使用单个整数,代码变得超长且不可维护。这是一个非常糟糕的设计。
而不是整数,使用string
来存储信用卡号。要达到信用卡号中的个人号码,您可以使用
card = get_string("What is your credit card number: ");
card[i]
i
是信用卡号的索引。从零开始,循环遍历所有数字,然后进行计算。