您的位置:首页 > 编程语言 > C语言/C++

C++ 实现比较版本号

2016-03-17 19:21 337 查看
code:
int iRetVersion = ComparerVersion("6.6.1512.401","6.6.1512.409");
//retval: 0:相等;1: 大于此版本,-1:小于当前版本 3:其他情况;
int ComparerVersion(char * version_get ,char * version)
{
string strversion_get = version_get;
string strversion = version;
if (strversion_get.empty() || strversion.empty())
{
return 3;
}

////////////////////////////////////////////////
size_t pos = 0;
int number = 0;
string pattern(".");
int icpt = pattern.size();
std::vector<string> result;
int iposbf = 0;
while(pos != string::npos)
{
pos = strversion_get.find(pattern, pos);
if(pos != string::npos)
{
int istart = iposbf;
if (0 ==istart)
{
}
else
{
istart = istart + icpt;
}

std::string s = strversion_get.substr(istart,pos - istart);
result.push_back(s);
++number;
iposbf = pos;
pos += (pattern.size() + 1);

}
else
{
iposbf += (pattern.size());
std::string s = strversion_get.substr(iposbf,strversion_get.size());
result.push_back(s);
}
}
////////////////////////////////////////////////////
if (number != 3)
{
return 3;
}

//////////////////////////////////////////////////////
size_t pos_v = 0;
int number_v = 0;
string pattern_v(".");
int icpt_v = pattern_v.size();
std::vector<string> result_v;
int iposbf_v = 0;
while(pos_v != string::npos)
{
pos_v = strversion.find(pattern_v, pos_v);
if(pos_v != string::npos)
{
int istart_v = iposbf_v;
if (0 ==istart_v)
{
}
else
{
istart_v = istart_v + icpt_v;
}

std::string s_v = strversion.substr(istart_v,pos_v - istart_v);
result_v.push_back(s_v);
++number_v;
iposbf_v = pos_v;
pos_v += (pattern_v.size() + 1);
}
else
{
iposbf_v += (pattern_v.size());
std::string s_v = strversion.substr(iposbf_v,strversion.size());
result_v.push_back(s_v);
}
}
if (number_v != 3)
{
return 3;
}

//////////////////////////////////////////////////////
ostringstream oversion;
for(int i=0; i < result.size(); i++)
{
oversion<<result[i].c_str();//打印容器的内容
OutputDebugStringA(const_cast<char*>(result[i].c_str()));
OutputDebugStringA("\r\n");
OutputDebugStringA(const_cast<char*>(oversion.str().c_str()));
OutputDebugStringA("\r\n");
}

ostringstream oversionv;
for(int j=0; j < result_v.size(); j++)
{
oversionv<<result_v[j].c_str();//打印容器的内容
OutputDebugStringA(const_cast<char*>(result_v[j].c_str()));
OutputDebugStringA("\r\n");
OutputDebugStringA(const_cast<char*>(oversionv.str().c_str()));
OutputDebugStringA("\r\n");
}

for(int m=0; m < result.size(); m++)
{
oversion<<result[m].c_str();//打印容器的内容
OutputDebugStringA(const_cast<char*>(result[m].c_str()));
OutputDebugStringA("\r\n");
OutputDebugStringA(const_cast<char*>(oversion.str().c_str()));
OutputDebugStringA("\r\n");

int istrcmp = 0;
istrcmp = result[m].compare(result_v[m]);
if ( istrcmp == 0)
{
if (m == result.size() -1)
{
return 0;
}
}
else if( istrcmp == 1)
{
return 1;
}
else if (istrcmp < 0)
{
return -1;
}
}

return 0;
}


code:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: