File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
165_compare_version_numbers Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ all :
2+ gcc -O2 -o test version.c
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+
4+ static int compareVersioin (char * v1 , char * v2 )
5+ {
6+ int ret = 0 ;
7+ while (* v1 != '\0' || * v2 != '\0' ) {
8+ while (* v1 != '\0' && * v1 == '0' ) {
9+ v1 ++ ;
10+ }
11+ while (* v2 != '\0' && * v2 == '0' ) {
12+ v2 ++ ;
13+ }
14+
15+ int n1 = 0 ;
16+ while (* v1 != '\0' && * v1 != '.' ) {
17+ n1 = n1 * 10 + (* v1 - '0' );
18+ v1 ++ ;
19+ }
20+
21+ int n2 = 0 ;
22+ while (* v2 != '\0' && * v2 != '.' ) {
23+ n2 = n2 * 10 + (* v2 - '0' );
24+ v2 ++ ;
25+ }
26+
27+ ret = n1 - n2 ;
28+ if (ret != 0 ) {
29+ return ret > 0 ? 1 : -1 ;
30+ }
31+
32+ if (* v1 == '.' ) {
33+ v1 ++ ;
34+ }
35+ if (* v2 == '.' ) {
36+ v2 ++ ;
37+ }
38+ }
39+ return ret ;
40+ }
41+
42+ int main (int argc , char * * argv )
43+ {
44+ if (argc != 3 ) {
45+ fprintf (stderr , "Usage: ./test versioin1 version2\n" );
46+ exit (-1 );
47+ }
48+ printf ("%d\n" , compareVersioin (argv [1 ], argv [2 ]));
49+ return 0 ;
50+ }
You can’t perform that action at this time.
0 commit comments