-1

我最近开始使用 C++ 并试图理解语法,但我遇到了放置问题。我必须做一个信用卡验证功能,要求提供一张有效的信用卡,然后我必须打印出信用卡的类型。

我唯一的问题是用于确定信用卡类型的 if 语句。我不知道它们的确切放置位置以及我是否缺少任何括号等。

对我来说有问题的确切块是:

        if(c.substr(0, 2) == "65" || c.substr(0, 5) == "6011" || c.substr(0, 7) >= "622126" && c.substr(0, 7) <= "622925" || c.substr(0, 4) >= "644" && c.substr(0, 4) <= "649")
            s = "You have a Discover card";
        if(c.substr(0, 2) >= "51" && c.substr(0, 2) <= "55")
            s = "You have a MasterCard card";
        if(c.substr(0, 1) == "4")
            s = "You have a Visa card";
        if(c.substr(0, 2) == "34" || c.substr(0, 2) == "37")
            s = "You have an American Express card"; 

这是我的代码。

#include <iostream>
#include <string>
using namespace std;

void validateCC();
string checkCC(string, int, bool&);
bool validateCCNum(string);

void main() {
    char again;
    cout << "Validate a credit card number (Y/N)? ";
    cin >> again;
    while (toupper(again) == 'Y') {
        validateCC();
        cout << "Validate a credit card number (Y/N)? ";
        cin >> again;
    }
}

void validateCC() {
    string ccn, msg;
    bool OK;
    int ccLen;
    cout << "Please enter a 15 or 16 digit credit card number."
        << "\n(No spaces or hyphens): ";
    cin >> ccn;
    ccLen = ccn.length();
    msg = checkCC(ccn, ccLen, OK);
    if(!OK)
        cout << msg;
    else
        if(validateCCNum(ccn))
            cout << "Valid credit card number\n";
        else
            cout << "Invalid credit card number\n";
    cout << "\n" << endl;
}

string checkCC(string c, int cLen, bool& ccOK) {
    string s = "";
    ccOK = true;
    for(int i=0;i<cLen && ccOK;++i)
        ccOK = isdigit(c[i]);

        if(c.substr(0, 2) == "65" || c.substr(0, 5) == "6011" || c.substr(0, 7) >= "622126" && c.substr(0, 7) <= "622925" || c.substr(0, 4) >= "644" && c.substr(0, 4) <= "649")
            s = "You have a Discover card";
        if(c.substr(0, 2) >= "51" && c.substr(0, 2) <= "55")
            s = "You have a MasterCard card";
        if(c.substr(0, 1) == "4")
            s = "You have a Visa card";
        if(c.substr(0, 2) == "34" || c.substr(0, 2) == "37")
            s = "You have an American Express card"; 

    if(ccOK == false) {
        s = "Invalid credit card number digits";
    } else if(cLen == 15) {
        if(c.substr(0, 2) != "34" && c.substr(0, 2) != "37") {
            ccOK = false;
            s = "Invalid American Express credit card number";
        }
    } else if(cLen != 16) {
        ccOK = false;
        s = "Invalid credit card number length";
    }
    return s;
}

bool validateCCNum(string cc) {
    bool flip = true;
    int tmp, num = 0;
    int ccLen = cc.length()-1;
    for(int ndx=ccLen;ndx>=0;ndx--) {
            if(flip)
                num += cc[ndx] - '0';
            else {
                tmp = (cc[ndx] - '0') * 2;
                if(tmp <= 9)
                    num += tmp;
                else
                    num += (1 + (tmp - 10)); // max of 18
            }
            flip = !flip;
    }
    return num % 10 == 0;
}

我不知道有问题的块是否属于它当前所在的位置,或者我是否将它放在 validateCC() 部分中,然后为它添加一个参数。

谢谢你。

4

4 回答 4

1

尝试将子字符串更改为 int (stringVar >> IntVar;) 以便您可以评估 <= >= 等...

    #include <iostream>
#include <string>
using namespace std;

void validateCC();
string checkCC(string, int, bool&);
bool validateCCNum(string);

void main() {
    char again;
    cout << "Validate a credit card number (Y/N)? ";
    cin >> again;
    while (toupper(again) == 'Y') {
        validateCC();
        cout << "Validate a credit card number (Y/N)? ";
        cin >> again;
    }
}
string checkCardType(string c){
if(atoi(c.substr(0, 2).c_str()) == 65 || atoi(c.substr(0, 5).c_str()) == 6011 || atoi(c.substr(0, 7).c_str()) >= 622126 && atoi(c.substr(0, 7).c_str()) <= 622925 || atoi(c.substr(0, 4).c_str()) >= 644 && atoi(c.substr(0, 4).c_str()) <= 649)
            return "You have a Discover card";
        if(atoi(c.substr(0, 2).c_str()) >= 51 && atoi(c.substr(0, 2).c_str()) <= 55)
            return "You have a MasterCard card";
        if(atoi(c.substr(0, 1).c_str()) == 4)
            return "You have a Visa card";
        if(atoi(c.substr(0, 2).c_str()) == 34 || atoi(c.substr(0, 2).c_str()) == 37)
            return "You have an American Express card"; 
        return "Card not recognized";
}
void validateCC() {
    string ccn, msg;
    bool OK;
    int ccLen;
    cout << "Please enter a 15 or 16 digit credit card number."
        << "\n(No spaces or hyphens): ";
    cin >> ccn;
    ccLen = ccn.length();
    msg = checkCC(ccn, ccLen, OK);
    if(!OK)
        cout << msg;
    else
        if(validateCCNum(ccn)){
            cout << "Valid credit card number\n";
            cout << checkCardType(ccn);}
        else
            cout << "Invalid credit card number\n";
    cout << "\n" << endl;
}

string checkCC(string c, int cLen, bool& ccOK) {
    string s = "";
    ccOK = true;
    for(int i=0;i<cLen && ccOK;++i)
        ccOK = isdigit(c[i]);

    if(ccOK == false) {
        s = "Invalid credit card number digits";
    } else if(cLen == 15) {
        if(c.substr(0, 2) != "34" && c.substr(0, 2) != "37") {
            ccOK = false;
            s = "Invalid American Express credit card number";
        }
    } else if(cLen != 16) {
        ccOK = false;
        s = "Invalid credit card number length";
    }
    return s;
}

bool validateCCNum(string cc) {
    bool flip = true;
    int tmp, num = 0;
    int ccLen = cc.length()-1;
    for(int ndx=ccLen;ndx>=0;ndx--) {
            if(flip)
                num += cc[ndx] - '0';
            else {
                tmp = (cc[ndx] - '0') * 2;
                if(tmp <= 9)
                    num += tmp;
                else
                    num += (1 + (tmp - 10)); // max of 18
            }
            flip = !flip;
    }
    return num % 10 == 0;
}
于 2013-01-13T22:42:40.197 回答
0

虽然不是最有效的方法,但我在您的代码中看到的唯一问题是您在某些情况下使用了太多字符:c.substr(0, 5) == "6011"只有在c.size() == 4.

对于它的价值,我会将前缀声明为static std::string const并使用std::equal它们(在首先检查数字是否足够长之后)。或者,我会在第一个字符上使用开关:

switch (c[0])
{
case '6':
    //  Additional tests for Discover...
    break;
case '5':
    //  Additional tests for MasterCard...
    break;

case '4':
    //  Additional tests for Visa...
    break;

case '3':
    //  Additional tests for American express..
    break;
}

或者更有可能的是,我会使用从配置文件中读取的模式的正则表达式;这不是您要硬编码到程序中的信息类型。

于 2013-01-13T23:04:06.833 回答
0

std::string::substr方法返回一个字符串,可以将其另一个字符串进行比较。到目前为止,您的代码看起来不错。

要使用括号,请查看C++ Operator Precedence。据我了解代码,您的 if 条件中不需要括号。但是,如果您有疑问,请使用括号来明确您的意图。

您不需要传递信用卡号的长度,因为checkCC可以自行确定。还将信用卡号传递为

string checkCC(const string &c, ...)

因为这避免了字符串的副本。

于 2013-01-13T23:05:05.070 回答
0

我不确定这是否有效:

c.substr(0, 2) == "34"

为什么不将字符串解析为整数 (atoi),然后将其与 '==' 进行比较。还将字符串与 "string".compare("string2") 进行比较(如果字符串相同,则返回 0)。

于 2013-01-13T22:55:17.683 回答