c ++

时间:2018-04-01 21:07:18

标签: c++ c++03

目标:有效的电话格式为:514 999-9999(加拿大国家格式) 我们必须有一个列表,空格和连字符的区域代码。

以下是我的代码:我在第dim[i]=espace;

时出错
  

传递const字符串作为此参数放弃限定符

我被困住了,任何帮助都会受到赞赏。

bool validerTelephone(const std::string& p_telephone) {
    string code_reg[28]={"403","780","604","236","250","778","902","506","514","408","204","905","519","289",
            "705","613","807","416","647","438","450","579","418","581","819","306","709","867"};
    bool etat;
    if(p_telephone.size()!=12){
        etat=false;

    }

    else {
        string mon_code_reg=p_telephone.substr (0,3);
        bool pas_trouve=true;
        int i=0;
        while (pas_trouve){
            if(mon_code_reg.compare(code_reg[i])==0)
                pas_trouve=false;


            char tire='-';
            char espace=' ';
            bool formatvalide=true;
            unsigned int dim=p_telephone.size();
            string telephone_valide=p_telephone;
            for(int i=0;i<dim;i++){
                if(i==3){
                    dim[i]=espace;

                }
                else if(i==7){
                    dim[i]=tire;

                }
                else{
                    cout<<"format telephone"<<p_telephone<<endl;
                }
            }
             formatvalide=false;
        }
        return etat;
    }
}

1 个答案:

答案 0 :(得分:0)

在使用正则表达式和STL发布解决方案后,我意识到OP的问题标记为C++03,这使我的回答无效。以下是使用sscanf的替代方法。回想一下sscanf返回它匹配的格式说明符的数量。

bool validerTelephoneSscanf(const std::string& p_telephone)
{
    int region = 0;
    int first = 0;
    int second = 0;

    int code_reg[] = { 403,780,604,236,250,778,902,506,514,408,204,905,519,289,
        705,613,807,416,647,438,450,579,418,581,819,306,709,867 };

    if (p_telephone.length() == 12 && sscanf(p_telephone.c_str(), "%3d %3d-%4d", &region, &first, &second) == 3)
    {
        for (unsigned i = 0; i < sizeof(code_reg)/sizeof(code_reg[0]); i++)
        {
            if (code_reg[i] == region)
            {
                return true;
            }
        }
    }
    return false;
}

使用正则表达式和一些标准库函数的原始解决方案。建议使用正则表达式模式匹配,而不是手工解析,这通常很乏味且容易出错。

bool validerTelephone(const std::string& p_telephone)
{
    //
    // Declare this as a vector so that we can use STL to easily search
    // it later
    //
    vector<string> code_reg = { "403","780","604","236","250","778","902","506","514","408","204","905","519","289",
        "705","613","807","416","647","438","450","579","418","581","819","306","709","867" };

    //
    // If writing a single-exit function, declare result variable false
    // and only set it to true when the success conditions are met
    //
    bool etat = false;

    //
    // The regex is delimited with start of line (^) and end of line ($)
    // because OP's original code enforced p_telephone.size() == 12
    //
    std::regex pattern("^([0-9]{3}) [0-9]{3}-[0-9]{4}$");
    std::smatch match;
    std::string region;  

    //
    // Match the input string to the pattern
    //
    if (std::regex_search(p_telephone, match, pattern)) 
    {
        // 
        // 'region' is the capture group at the start of the regex, i.e. ([0-9]{3})
        // We can access it via a member function of our std::smatch instance
        //
        region = match.str(1);

        //
        // A standard idiom to determine if the region (e.g. 514) is one of the valid
        // code regions contained in the code_reg vector
        //
        if (std::find(code_reg.begin(), code_reg.end(), region) != code_reg.end())
        {           
            etat = true;    // The telephone code region is valid, so all requirements are met
            std::cout << "format telephone " << p_telephone << '\n';
        }
    }   

    return etat;
}