-
Notifications
You must be signed in to change notification settings - Fork 8
/
compare_versions.cpp
100 lines (50 loc) · 1.38 KB
/
compare_versions.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
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int compareVersion(string version1, string version2) {
string a = ""; string b = "";
int lena=version1.size();
int lenb=version2.size();
int a_st=0;
int b_st=0;
int i=0;
int j=0;
while(true){
bool a_dot=false;
bool b_dot=false;
for(i=a_st;i<lena;i++){
if(version1[i]=='.'){
a=version1.substr(a_st,i-a_st);
a_dot=true;
break;
}
}
if(!a_dot) a = version1;
for(j=b_st;j<lenb;j++){
if(version2[j]=='.'){
b=version2.substr(b_st,j-b_st);
b_dot=true;
break;
}
}
if(!b_dot) b = version2;
if(atoi(a.c_str()) > atoi(b.c_str())){
return 1;
}
else if(atoi(a.c_str()) < atoi(b.c_str())){
return -1;
}
else{
a_st=i+1;
b_st=j+1;
if ( !a_dot || !b_dot)
return 0;
}
}
}
int main()
{
string a = "1.0"; string b = "1.1";
compareVersion(a,b);
}