fork download
  1. #include <stdio.h>
  2.  
  3. int compVersions ( const char * version1, const char * version2 ) {
  4. unsigned major1 = 0, minor1 = 0, bugfix1 = 0;
  5. unsigned major2 = 0, minor2 = 0, bugfix2 = 0;
  6. sscanf(version1, "%u.%u.%u", &major1, &minor1, &bugfix1);
  7. sscanf(version2, "%u.%u.%u", &major2, &minor2, &bugfix2);
  8. if (major1 < major2) return -1;
  9. if (major1 > major2) return 1;
  10. if (minor1 < minor2) return -1;
  11. if (minor1 > minor2) return 1;
  12. if (bugfix1 < bugfix2) return -1;
  13. if (bugfix1 > bugfix2) return 1;
  14. return 0;
  15. }
  16.  
  17.  
  18. int main ( ) {
  19. struct {
  20. const char * version1;
  21. const char * version2;
  22. } testVersions[] = {
  23. { "a141.1.23", "141.1.22" },
  24. { "141.1.23", "145.1.22" },
  25. { "141.2.23", "141.1.23" },
  26. { "141.1.0", "141.1" }
  27. };
  28.  
  29. size_t testCount = sizeof(testVersions) / sizeof(testVersions[0]);
  30. for (size_t i = 0; i < testCount; i++) {
  31. int res = compVersions(
  32. testVersions[i].version1, testVersions[i].version2);
  33. printf("%s %s %s\n",
  34. testVersions[i].version1,
  35. (res == 0 ? "=" : res == -1 ? "<" : ">"),
  36. testVersions[i].version2);
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
a141.1.23 < 141.1.22
141.1.23 < 145.1.22
141.2.23 > 141.1.23
141.1.0 = 141.1