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
|
/**
* Assignment#6.1
* DUE: 3/13/16
* NAME: <Joshua Cisneros>
* Purpose: Write the following function that compares the two version strings, ver1 and ver2,
* of a software product to determine which product version is the latest:
* int compareVersions(string ver1, string ver2)
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
/*
* return 1 if v1 > v2
* return 0 if v1 = v2
* return -1 if v1 < v2
*/
using namespace std;
int compareVersions(const char *version1, const char *version2)
{
int i;
int oct_version1[3], oct_version2[3];
sscanf(version1, "%d.%d.%d", &oct_version1[0], &oct_version1[1], &oct_version1[2]);
sscanf(version2, "%d.%d.%d", &oct_version2[0], &oct_version2[1], &oct_version2[2]);
for (i = 0; i < 4; i++) {
if (oct_version1[i] > oct_version2[i])
return 1;
else if (oct_version1[i] < oct_version2[i])
return -1;
}
return 0;
}
int main()
{
std::cout << ("%d\n", compareVersions("0.1.2", "0.2.3")) << endl;
std::cout << ("%d\n", compareVersions("3.2.0", "3.2.0")) << endl;
std::cout << ("%d\n", compareVersions("5.2.0", "3.2.0")) << endl;
}
|