-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompareVersionNumers.cpp
101 lines (94 loc) · 2.56 KB
/
CompareVersionNumers.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @file CompareVersionNumers.cpp
* @brief
* @author nhzc [email protected]
* @version 1.0.0
* @date 2015-04-19
*/
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> version2vector(const string &version){
vector<string> result;
string dig = "";
for (int i = 0; i < version.length(); i ++){
if (version[i] == '.'){
result.push_back(dig);
dig = "";
}
else{
dig += version[i];
}
}
if (dig != ""){
result.push_back(dig);
}
return result;
}
int cmp(const vector<string> &v1, const vector<string> &v2){
//the first position
int len = v1.size() > v2.size() ? v2.size() : v1.size();
for (int k = 0; k < len; k ++){
int pos1 = 0;
while(v1[k][pos1] == '0'){
pos1 ++;
}
int pos2 = 0;
while(v2[k][pos2] == '0'){
pos2 ++;
}
string s1 = v1[k].substr(pos1);
string s2 = v2[k].substr(pos2);
if (s1.length() > s2.length()){
return 1;
}
if (s1.length() < s2.length()){
return -1;
}
for (int i = 0; i < s1.length(); i ++){
if (s1[i] > s2[i]){
return 1;
}
else if (s1[i] < s2[i]){
return -1;
}
}
}
if (v1.size() > v2.size()){
for (int k = v2.size(); k < v1.size(); k++){
for (int z = 0; z < v1[k].length(); z++){
if (v1[k][z] != '0'){
cout<<v1[k][z]<<endl;
cout<<"hehe"<<endl;
return 1;
}
}
}
return 0;
}
else if(v1.size() < v2.size()){
for (int k = v1.size(); k < v2.size(); k++){
for (int z = 0; z < v2[k].length(); z++){
if (v2[k][z] != '0'){
return -1;
}
}
}
return 0;
}
else{
return 0;
}
}
int compareVersion(string version1, string version2) {
vector<string> v1 = version2vector(version1);
vector<string> v2 = version2vector(version2);
return cmp(v1, v2);
}
int main(){
string v1 = "1.0";
string v2 = "1";
cout<<compareVersion(v1, v2)<<endl;;
return 0;
}