-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompareVersion.cpp
More file actions
72 lines (66 loc) · 1.65 KB
/
compareVersion.cpp
File metadata and controls
72 lines (66 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void findAmendmentNo(string & version, vector<int>& vecAmendment)
{
string sAmendment;
while(version.size())
{
size_t pos = version.find('.');
if(pos != string::npos)
{
sAmendment = version.substr(0, pos);
version = version.substr(pos + 1);
vecAmendment.push_back(atoi(sAmendment.c_str()));
//cout << vecAmendment[vecAmendment.size() - 1] << endl;
}
else
{
vecAmendment.push_back(atoi(version.c_str()));
version = "";
//cout << vecAmendment[vecAmendment.size() - 1] << endl;
}
}
}
int compareVersion(string version1, string version2)
{
vector<int> vec1;
vector<int> vec2;
findAmendmentNo(version1, vec1);
findAmendmentNo(version2, vec2);
int i = 0;
for(; i < vec1.size() && i < vec2.size(); ++i)
{
if(vec1[i] < vec2[i])
{
return -1;
}
if(vec1[i] > vec2[i])
{
return 1;
}
}
while (i < vec1.size())
{
if(vec1[i] != 0)
{
return 1;
}
++i;
}
while (i < vec2.size())
{
if(vec2[i] != 0)
{
return -1;
}
++i;
}
return 0;
}
int main()
{
cout << compareVersion("1.01", "1.001.1") << endl;;
return 0;
}