-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm165 v2.c
100 lines (86 loc) · 2.15 KB
/
m165 v2.c
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
int compareVersion(char* version1, char* version2) {
int one = 0;
int two = 0;
while (true) {
int oneRight = one + 1;
int twoRight = two + 1;
while (version1[oneRight] && version1[oneRight] != '.') {
oneRight++;
}
while (version2[twoRight] && version2[twoRight] != '.') {
twoRight++;
}
int oneVal = 0;
int twoVal = 0;
for (int i = one; i < oneRight; i++) {
oneVal = 10 * oneVal + (version1[i] - '0');
}
for (int i = two; i < twoRight; i++) {
twoVal = 10 * twoVal+ (version2[i] - '0');
}
if (oneVal < twoVal) {
return -1;
}
if (oneVal > twoVal) {
return 1;
}
one = oneRight;
two = twoRight;
if (!version1[oneRight] && !version2[twoRight]) {
break;
}
if (!version1[oneRight]) {
two++;
break;
}
if (!version2[twoRight]) {
one++;
break;
}
one++;
two++;
}
// Finish off one
while (version1[one]) {
int oneRight = one + 1;
while (version1[oneRight] && version1[oneRight] != '.') {
oneRight++;
}
int oneVal = 0;
for (int i = one; i < oneRight; i++) {
oneVal = 10 * oneVal + (version1[i] - '0');
}
if (oneVal < 0) {
return -1;
}
if (oneVal > 0) {
return 1;
}
if (!version1[oneRight]) {
break;
}
one = oneRight + 1;
}
// Finish off two
while (version2[two]) {
int twoRight = two + 1;
while (version2[twoRight] && version2[twoRight] != '.') {
twoRight++;
}
int twoVal = 0;
for (int i = two; i < twoRight; i++) {
twoVal = 10 * twoVal + (version2[i] - '0');
}
if (twoVal > 0) {
return -1;
}
if (twoVal < 0) {
return 1;
}
if (!version2[twoRight]) {
break;
}
two = twoRight + 1;
}
return 0;
}