Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. str1 = "141.1.23"
  2. str2 = "141.1.22"
  3.  
  4. strunct version_t {
  5. int major;
  6. int minor;
  7. int build;
  8. };
  9.  
  10. version_t parse_ver(const char* version_str) {
  11. version_t res;
  12. // Use strtok_r to split the string, and atoi to convert tokens to ints
  13. return res;
  14. }
  15.  
  16. #include <assert.h>
  17. #include <stdlib.h>
  18.  
  19. int versionCmp( char *pc1, char *pc2)
  20. {
  21. int result = 0;
  22. /* loop through each level of the version string */
  23. while (result == 0) {
  24. /* extract leading version numbers */
  25. char* tail1;
  26. char* tail2;
  27. unsigned long ver1 = strtoul( pc1, &tail1, 10 );
  28. unsigned long ver2 = strtoul( pc2, &tail2, 10 );
  29. /* if numbers differ, then set the result */
  30. if (ver1 < ver2)
  31. result = -1;
  32. else if (ver1 > ver2)
  33. result = +1;
  34. else {
  35. /* if numbers are the same, go to next level */
  36. pc1 = tail1;
  37. pc2 = tail2;
  38. /* if we reach the end of both, then they are identical */
  39. if (*pc1 == '' && *pc2 == '')
  40. break;
  41. /* if we reach the end of one only, it is the smaller */
  42. else if (*pc1 == '')
  43. result = -1;
  44. else if (*pc2 == '')
  45. result = +1;
  46. /* not at end ... so far they match so keep going */
  47. else {
  48. pc1++;
  49. pc2++;
  50. }
  51. }
  52. }
  53. return result;
  54. }
  55.  
  56. int main( void )
  57. {
  58. assert(versionCmp("1.2.3" , "1.2.3" ) == 0);
  59. assert(versionCmp("1.2.3" , "1.2.4" ) < 0);
  60. assert(versionCmp("1.2.4" , "1.2.3" ) > 0);
  61. assert(versionCmp("10.2.4", "9.2.3" ) > 0);
  62. assert(versionCmp("9.2.4", "10.2.3") < 0);
  63. /* Trailing 0 ignored. */
  64. assert(versionCmp("01", "1") == 0);
  65. /* Any single space delimiter is OK. */
  66. assert(versionCmp("1a2", "1b2") == 0);
  67. return EXIT_SUCCESS;
  68. }
  69.  
  70. ...
  71. while (result == 0) {
  72. /* ignore leading zeroes */
  73. pc1 += strspn( pc1, "0" );
  74. pc2 += strspn( pc2, "0" );
  75. /* extract leading version strings */
  76. int len1 = strcspn( pc1, "." );
  77. int len2 = strcspn( pc2, "." );
  78. /* if one is shorter than the other, it is the smaller version */
  79. result = len1 - len2;
  80. /* if the same length then compare as strings */
  81. if (result == 0)
  82. result = strncmp( pc1, pc2, len1 );
  83. if (result == 0) {
  84. pc1 += len1;
  85. pc2 += len2;
  86. if (*pc1 == '' && *pc == '')
  87. ...
  88.  
  89. #define _GNU_SOURCE
  90. #include <assert.h>
  91. #include <stdlib.h>
  92. #include <string.h>
  93.  
  94. int main(void) {
  95. assert(strverscmp("1.2.3" , "1.2.3" ) == 0);
  96. assert(strverscmp("1.2.3" , "1.2.4" ) < 0);
  97. assert(strverscmp("1.2.3" , "1.2.2" ) > 0);
  98. assert(strverscmp("9.2.3" , "10.2.3") < 0);
  99. assert(strverscmp("10.2.3", "9.2.3" ) > 0);
  100.  
  101. /* Delimiers are also compared. */
  102. assert(strverscmp("1a2", "1b2" ) < 0);
  103. assert(strverscmp("1b2", "1a2" ) > 0);
  104.  
  105. /* Leading 0s: number gets treated as 0.X, e.g. 01 means 0.1.
  106. * Maybe not perfect for version strings, but sane version strings
  107. * should not have leading 0s.
  108. */
  109. assert(strverscmp("01", "9" ) < 0);
  110. assert(strverscmp("01", "09") < 0);
  111. assert(strverscmp("01", "09") < 0);
  112. assert(strverscmp("09", "1") < 0);
  113.  
  114. return EXIT_SUCCESS;
  115. }
  116.  
  117. vector<char*> tokenize(char *s)
  118. {
  119. vector<char*> svec;
  120.  
  121. char *stp = strtok(s,".");
  122. while(stp != NULL)
  123. {
  124. svec.push_back(stp);
  125. stp = strtok(NULL,".");
  126. }
  127. cout << endl;
  128. return svec;
  129.  
  130. }
  131.  
  132. int version_compare(char *s1, char *s2)
  133. {
  134. vector<char*> tokens_s1 = tokenize(s1);
  135. vector<char*> tokens_s2 = tokenize(s2);
  136.  
  137. int st1, st2, flag, maxf,result;
  138. st1 = tokens_s1.size();
  139. st2 = tokens_s2.size();
  140. flag = st1 < st2 ? st1 : st2;
  141.  
  142.  
  143. for(int i=0; i < flag ;i++)
  144. {
  145.  
  146. int one = *(tokens_s1[i]);
  147. int two = *(tokens_s2[i]);
  148. if(one > two)
  149. return 1;
  150. else if(one < two)
  151. return 2;
  152. else
  153. result = 0;
  154.  
  155. }
  156. }
  157.  
  158. if((st1 == st2) && (result == 0)) return 0;
  159. return (st1 > st2 ? 1 : 2);
  160.  
  161.  
  162.  
  163. }
  164.  
  165.  
  166. int main()
  167. {
  168. char s1[] = "1.2.3.4";
  169. char s2[] = "2.2.3.3.3";
  170. int st;
  171. st = version_compare(s1,s2);
  172. cout<<st<<endl;
  173.  
  174. }
  175.  
  176. int version_compare(char *s1, char *s2)
  177. {
  178. char *delim = ".:-";
  179. while(1) {
  180. if (*s1 == *s2) {
  181. if (!*s1)
  182. return 0;
  183. s1++; s2++;
  184. } else if (strchr(delim, *s1) || !*s1) {
  185. return -1;
  186. } else if (strchr(delim, *s2) || !*s2) {
  187. return 1;
  188. } else {
  189. int diff;
  190. char *end1, *end2;
  191. diff = strtoul(c1, &end1, 10) - strtoul(c2, &end2, 10);
  192. if (!diff) {
  193. c1 += (end1 - c1);
  194. c2 += (end2 - c2);
  195. } else {
  196. return diff;
  197. }
  198. }
  199. }
  200. }
  201.  
  202. int compVersions ( const char * version1, const char * version2 ) {
  203. unsigned major1 = 0, minor1 = 0, bugfix1 = 0;
  204. unsigned major2 = 0, minor2 = 0, bugfix2 = 0;
  205. sscanf(version1, "%u.%u.%u", &major1, &minor1, &bugfix1);
  206. sscanf(version2, "%u.%u.%u", &major2, &minor2, &bugfix2);
  207. if (major1 < major2) return 1;
  208. if (major1 > major2) return -1;
  209. if (minor1 < minor2) return 1;
  210. if (minor1 > minor2) return -1;
  211. if (bugfix1 < bugfix2) return 1;
  212. if (bugfix1 > bugfix2) return -1;
  213. return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement