Compare versions as strings

后端 未结 4 1940
一整个雨季
一整个雨季 2020-12-16 05:20

Comparing version numbers as strings is not so easy...
\"1.0.0.9\" > \"1.0.0.10\", but it\'s not correct.
The obvious way to do it properly is to parse these strings

4条回答
  •  渐次进展
    2020-12-16 05:56

    int VersionParser(char* version1, char* version2) {
    
        int a1,b1, ret; 
        int a = strlen(version1); 
        int b = strlen(version2);
        if (b>a) a=b;
        for (int i=0;ia1) ret = 1 ; // second version is fresher
        else if (b1==a1) ret=-1; // versions is equal
        else ret = 0; // first version is fresher
        return ret;
    }
    

提交回复
热议问题