Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用正则表达式C+计算数字中的十进制空间+;_C++_Regex_Decimal - Fatal编程技术网

C++ 使用正则表达式C+计算数字中的十进制空间+;

C++ 使用正则表达式C+计算数字中的十进制空间+;,c++,regex,decimal,C++,Regex,Decimal,我想检查十进制数是否符合预定义的标准。我已经使用了下面的正则表达式^ [++]?[0[9] {(-09] {2,4})$,但是它似乎在C++中不起作用。我已经尝试了许多regex验证解决方案,并且它在那里按预期工作。例如,如果我的数字是0.00000,那么输出应该是false,另一方面,如果数字是0.00,那么输出应该是true。在我的例子中,输出似乎没有检测到任何错误。这是我的源代码,你能帮我吗?多谢各位 void detectFormatConsistency() { std::re

我想检查十进制数是否符合预定义的标准。我已经使用了下面的正则表达式^ [++]?[0[9] {(-09] {2,4})$,但是它似乎在C++中不起作用。我已经尝试了许多regex验证解决方案,并且它在那里按预期工作。例如,如果我的数字是0.00000,那么输出应该是false,另一方面,如果数字是0.00,那么输出应该是true。在我的例子中,输出似乎没有检测到任何错误。这是我的源代码,你能帮我吗?多谢各位

void detectFormatConsistency()
{
    std::regex dbl("^[+-]?[0-9]+(\\.[0-9]{2,4})$");
    std::smatch matches;
    int detected = 0;
    for(unsigned int index = 0; index < mixedDistro.size(); index++)
    {
        double currentValue = mixedDistro[index];
        std::string test = std::to_string(currentValue);
        if(std::regex_search(test, dbl)) \\I also tried with match
        {
            detected++;
        }
    }
    printf("Number of points detected: %d\n", detected);
}
无效检测格式一致性()
{
std::regexdbl(“^[+-]?[0-9]+(\\.[0-9]{2,4})$”;
std::smatch匹配;
检测到的int=0;
for(unsigned int index=0;index
更新:以下源代码现在可以正常工作:

void detectFormatConsistency()
{
    std::regex dbl("^[+-]?[0-9]+(\\.[0-9]{2,4})$");
    std::smatch matches;
    int detected = 0;
    for(unsigned int index = 0; index < mixedDistro.size(); index++)
    {
        double currentValue = mixedDistro[index];
        std::string test = tostring(currentValue);
        if(std::regex_match(test, dbl))
        {
            detected++;
        }
    }
    printf("Number of points detected: %d\n", detected);
}



  //https://stackoverflow.com/questions/13686482/c11-stdto-stringdouble-no-trailing-zeros     
  //Thanks to: @Silencer
    template<typename T>
    std::string tostring(const T &n) {
        std::ostringstream oss;
        oss << n;
        std::string s =  oss.str();
        unsigned int dotpos = s.find_first_of('.');
        if(dotpos!=std::string::npos){
             unsigned int ipos = s.size()-1;
            while(s[ipos]=='0' && ipos>dotpos){
                --ipos;
            }
            s.erase ( ipos + 1, std::string::npos );
        }
        return s;
    }
无效检测格式一致性()
{
std::regexdbl(“^[+-]?[0-9]+(\\.[0-9]{2,4})$”;
std::smatch匹配;
检测到的int=0;
for(unsigned int index=0;index

谢谢大家的帮助

如果你使用字符串,我觉得你的方法很好

bool checkFormatConsistency(const string& d) {
    static std::regex dbl("^[+-]?[0-9]+(\\.[0-9]{3,4})$");
    return std::regex_search(d, dbl);
}

int main() {
    vector<string> tests = {
        "0.00", "0.000", "0.0000", "0.00000", "10", "10.1", "-1000", "-99.99", "-345.236"
    };
    for (const string& test : tests) {
        cout << test << " -> " << (checkFormatConsistency(test) ? "ok" : "not ok") << endl;
    }

    return 0;
}

如果你使用字符串,我觉得你的方法很好

bool checkFormatConsistency(const string& d) {
    static std::regex dbl("^[+-]?[0-9]+(\\.[0-9]{3,4})$");
    return std::regex_search(d, dbl);
}

int main() {
    vector<string> tests = {
        "0.00", "0.000", "0.0000", "0.00000", "10", "10.1", "-1000", "-99.99", "-345.236"
    };
    for (const string& test : tests) {
        cout << test << " -> " << (checkFormatConsistency(test) ? "ok" : "not ok") << endl;
    }

    return 0;
}
结果的字符串与控制台的输出相同,控制台使用6位的默认精度

因此,您的测试设置不合适,您应该使用字符串数组

请注意,在C++中,所有的双文本“代码>0 < /代码>,<代码> 0 < /代码>,<代码> 0 < /代码>,…结果是相同的双精度值(即尾随的零只是不相关)。实际上,你总是有64位来放置你的值,分布在符号位、指数和尾数上——对于所有指数相等的值,你总是有完全相同的精度。查看详细信息。

生成的字符串与控制台的输出相同,控制台使用默认精度6位

因此,您的测试设置不合适,您应该使用字符串数组


请注意,在C++中,所有的双文本“代码>0 < /代码>,<代码> 0 < /代码>,<代码> 0 < /代码>,…结果是相同的双精度值(即尾随的零只是不相关)。实际上,你总是有64位来放置你的值,分布在符号位、指数和尾数上——对于所有指数相等的值,你总是有完全相同的精度。查看详细信息。

正则表达式中没有任何内容阻止
0.0000
匹配。它遵循这种模式,在
之前有一个
0
,后面有4个
0.00
无法在另一侧匹配,因为后面只有2
0
而您代码中的正则表达式声明至少有3个。我将检查
mixedDistro
值。此数组中的所有值
double vals[]={0.00,0.0000,0.0,0}通过
std::to_string
转换为
0.000000
。您应该使用
std::regex_match
,因为
std::regex_search
匹配任何子字符串。此外,您问题中的regex与代码中的regex不同,它们都允许您所说的模式被拒绝。。。“我已经尝试了许多regex验证解决方案,它在那里按预期工作。“-您能否提供一个,以便我们进行比较?您的正则表达式中没有任何内容阻止
0.0000
匹配。它遵循这种模式,在
之前有一个
0
,后面有4个
0.00
无法在另一侧匹配,因为后面只有2
0
而您代码中的正则表达式声明至少有3个。我将检查
mixedDistro
值。此数组中的所有值
double vals[]={0.00,0.0000,0.0,0}通过
std::to_string
转换为
0.000000
。您应该使用
std::regex_match
,因为
std::regex_search
匹配任何子字符串。此外,您问题中的regex与代码中的regex不同,它们都允许您所说的模式被拒绝。。。“我已经尝试了许多regex验证解决方案,并且它在那里按预期工作。”-您能提供一个,以便我们进行比较吗?