6

我们的教授希望我们编写一个程序来比较两个版本号,例如 0.1 < 0.2 或 1 < 1.1。还有一些技巧,例如 .0.4 < .1。所以,我的想法是首先判断数字是否以点开头,如果是,我添加一个0。之后,我删除了除第一个点之外的其他点。然后我将字符串转换为数字并进行比较。这是我在第一步中所做的。

string numb1,numb2;
if(numb1[0]=='.')
{
    numb1 ="0"+ numb1;
}

我对第二个数字做同样的事情。现在我需要帮助来告诉我如何去除除了第一个点之外的点。我们的教授希望我们使用这个特定的函数:int compareVersions(string ver1, string ver2)。如果 ver1 > ver2:返回 1 如果 ver1 < ver2:返回 -1,否则返回 0。

顺便说一句,有些愿景号可能很长,例如 2.3.2.2.3.1.1.5.3.5.6.2 或 1.1.1.1.1.1.1。

4

5 回答 5

5

这是一种适用于数字版本号的方法:

  • 使用将输入字符串拆分为多个片段getline(strstream, token, ".")
  • atoi使用or将相应的部分转换为数字stol并进行数字比较

基本上,将版本号视为由分隔的数字序列,.并按字典顺序比较这些序列。


请注意,实用的通用版本号比较算法可能需要处理额外的棘手问题,例如字母后缀(例如,,,1.1e2.4b243.5rc1。我假设这超出了课堂练习的范围,但方法类似:split这些部分分成数字和非数字部分的序列,并比较每个部分(例如2.4b7 < 2.4b24,因为4, "b", 7 < 4, "b", 24)。

于 2015-10-14T20:45:38.577 回答
1

像这样的东西可以进行检查并且相当少。它利用 boost 来拆分字符串,然后逐步比较版本。它会自动处理丢失的前导零。

#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream>

int version_a_newer_than_b(const std::string& a, const std::string& b)
{
    // First, split the string.
    std::vector<std::string> va, vb;
    boost::split(va, a, boost::is_any_of("."));
    boost::split(vb, b, boost::is_any_of("."));

    // Compare the numbers step by step, but only as deep as the version
    // with the least elements allows.
    const int depth = std::min(va.size(), vb.size());
    int ia,ib;
    for (int i=0; i<depth; ++i)
    {
        ia = atoi(va[i].c_str());
        ib = atoi(vb[i].c_str());
        if (ia != ib)
            break;
    }

    // Return the required number.
    if (ia > ib)
        return 1;
    else if (ia < ib)
        return -1;
    else
    {
        // In case of equal versions, assumes that the version
        // with the most elements is the highest version.
        if (va.size() > vb.size())
            return 1;
        else if (va.size() < vb.size())
            return -1;
    }

    // Everything is equal, return 0.
    return 0;
}

int main()
{
    std::string a = "0.1.32.8";
    std::string b = "0.1";

    std::cout << "Is a newer than b: " << version_a_newer_than_b(a, b) << std::endl;

    return 0;
}
于 2015-10-14T21:15:08.500 回答
1

您需要做的是遍历字符串,忽略 '.' 并将数字的 char 表示形式转换为整数。然后比较两个最终结果。

string numb1 = "4.3.2";
string numb2 = "3.4.5";
int n1 = 0;
int n2 = 0; 

for (int i = 0; i < numb1.length(); i++)
{
    if (numb1[i] != '.')
    {   
        n1 = n1 * 10;
        n2 = n2 * 10;
        n1 += (int(numb1[i]) - '0');
        n2 += (int(numb2[i]) - '0');                
    }

}

这会给你 432 和 345,比较它们会给你哪个是更高的版本。

于 2015-10-14T21:14:04.950 回答
0

因为你知道numb[1]等于'。你可以使用

 numb1.erase(std::remove(numb1.begin() + 2, numb1.end(), '.'), numb1.end());

numb1这将删除第二个字符之后的所有点。

于 2015-10-14T20:46:00.293 回答
-1

以下示例将演示以下版本格式之间的比较: major.minor.revision.build或任何较短的版本,例如仅major当它允许您扩展它以满足您的需求时,

"some of the version numbers may be very long like 2.3.2.2.3.1.1.5.3.5.6.2"

使用下面的示例,版本字符串的开头和结尾处的点被处理,直到.0.4被认为等于0.0.4并且.1.被认为等于0.1.0

比较版本.h

#ifndef COMPAREVERSION_H_
#define COMPAREVERSION_H_
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
struct CompareVersion {
public:
    int maj;
    int min;
    int rev;
    int build;
    CompareVersion(std::string);
    bool operator < (const CompareVersion&);
    bool operator <= (const CompareVersion&);
    bool operator == (const CompareVersion&);
    friend std::ostream& operator << (std::ostream& stream, const CompareVersion& ver) {
        stream << ver.maj;
        stream << '.';
        stream << ver.min;
        stream << '.';
        stream << ver.rev;
        stream << '.';
        stream << ver.build;
        return stream;
    };
    void reset();
};
#endif /* COMPAREVERSION_H_ */

比较版本.cpp

#include "CompareVersion.h"
CompareVersion::CompareVersion(std::string version) 
{
    reset();
    if (version.compare(0,1,".") == 0)
        version = "0"+version;
    if (version.compare(version.size()-1,1,".") == 0)
        version.append("0");
    sscanf(version.c_str(), "%d.%d.%d.%d", &maj, &min, &rev, &build);
    if (maj <= 0) maj = 0;
    if (min <= 0) min = 0;
    if (rev <= 0) rev = 0;
    if (build <= 0) build = 0;
}
bool CompareVersion::operator < (const CompareVersion& other)
{
    if (maj < other.maj) return true;
    if (min < other.min) return true;
    if (rev < other.rev) return true;
    if (build < other.build) return true;

    return false;
}
bool CompareVersion::operator <= (const CompareVersion& other)
{
    if (maj >= other.maj) return true;
    if (min >= other.min) return true;
    if (rev >= other.rev) return true;
    if (build >= other.build) return true;

    return false;
}
bool CompareVersion::operator == (const CompareVersion& other)
{
    return maj == other.maj
    && min == other.min
    && rev == other.rev
    && build == other.build;
}
void CompareVersion::reset()
{
    maj = 0;
    min = 0;
    rev = 0;
    build = 0;
}

主文件

#include <iostream>
#include "CompareVersion.h"
using namespace std;
int main() 
{
    if((CompareVersion("1.2.3.4") == CompareVersion("1.2.3.4")) == true)
    cout << "Version 1.2.3.4 and version 1.2.3.4 are equal" << endl;

    if((CompareVersion("1.2.3.3") < CompareVersion("1.2.3.4")) == true)
    cout << "Version 1.2.3.3 is smaller than 1.2.3.4. " << endl;

    if((CompareVersion("1.2.3.4") < CompareVersion("1.2.3.4")) == true)
    cout << "You won't see that. " << endl;

    if((CompareVersion("1.2.3.4") <= CompareVersion("1.2.3.4")) == true)
    cout << "Version 1.2.3.4 is smaller or equal to 1.2.3.4" << endl;
    if((CompareVersion("1") <= CompareVersion("1.0.0.1")) == true)
    cout << "Version 1 is smaller or equal to 1.0.0.1" << endl;
    /* THE DOTS */
    if((CompareVersion(".0.4") == CompareVersion("0.0.4")) == true)
    cout << "Version .0.4 and version 0.0.4 are equal" << endl;
    if((CompareVersion(".1.") == CompareVersion("0.1.0")) == true)
    cout << "Version .1. and version 0.1.0 are equal" << endl;
    if((CompareVersion("1") == CompareVersion("1.0.0.0")) == true)
    cout << "Version 1 and version 1.0.0.0 are equal" << endl;
    return 0;
}

输出

Version 1.2.3.4 and version 1.2.3.4 are equal
Version 1.2.3.3 is smaller than 1.2.3.4. 
Version 1.2.3.4 is smaller or equal to 1.2.3.4
Version 1 is smaller or equal to 1.0.0.1
Version .0.4 and version 0.0.4 are equal
Version .1. and version 0.1.0 are equal
Version 1 and version 1.0.0.0 are equal
于 2015-12-27T19:52:27.070 回答