码迷,mamicode.com
首页 > 其他好文 > 详细

Compare Version Numbers

时间:2015-07-05 12:28:43      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        string::iterator iter1, iter2, iter3;

        iter1 = version1.begin();
        while(iter1 != version1.end() && *iter1 != ‘.‘)
            iter1++;
        if(iter1 == version1.end())
            version1 += ".0";
        iter1 = version2.begin();
        while(iter1 != version2.end() && *iter1 != ‘.‘)
            iter1++;
        if(iter1 == version2.end())
            version2 += ".0";
        cout<< "first dot" << endl;
        cout << version1 << endl;
        cout << version2 << endl;

        iter1 = version1.begin();
        int counter = 0;
        for(; iter1 != version1.end(); iter1++)
            if(*iter1 == ‘.‘)
                counter++;
        if(counter < 2)
            version1 += ".0";
        iter1 = version2.begin();
        counter = 0;
        for(; iter1 != version2.end(); iter1++)
            if(*iter1 == ‘.‘)
                counter++;
        if(counter < 2)
            version2 += ".0";
        cout<< "second dot" << endl;
        cout << version1 << endl;
        cout << version2 << endl;

        iter1 = version1.begin();
        counter = 0;
        for(; iter1 != version1.end(); iter1++)
            if(*iter1 == ‘.‘)
                counter++;
        if(counter < 3)
            version1 += ".0";
        iter1 = version2.begin();
        counter = 0;
        for(; iter1 != version2.end(); iter1++)
            if(*iter1 == ‘.‘)
                counter++;
        if(counter < 3)
            version2 += ".0";
        cout<< "third dot" << endl;
        cout << version1 << endl;
        cout << version2 << endl;

        int version1ForwardInt,version1BackwardInt,version1BBackwardInt, version1BBBackwardInt;
        iter1 = version1.begin();
        while(*iter1 != ‘.‘)
            iter1++;
        iter2 = iter1;
        iter2++;
        while(*iter2 != ‘.‘)
            iter2++;
        iter3 = iter2;
        iter3++;
        while(*iter3 != ‘.‘)
            iter3++;
        version1ForwardInt = stoi(string(version1.begin(), iter1));
        version1BackwardInt = stoi(string(iter1 + 1, iter2));
        version1BBackwardInt = stoi(string(iter2 + 1, iter3));
        version1BBBackwardInt = stoi(string(iter3 + 1, version1.end()));
        cout << version1ForwardInt << endl;
        cout << version1BackwardInt << endl;
        cout << version1BBackwardInt << endl;
        cout << version1BBBackwardInt << endl;

        int version2ForwardInt,version2BackwardInt,version2BBackwardInt, version2BBBackwardInt;
        iter1 = version2.begin();
        while(*iter1 != ‘.‘)
            iter1++;
        iter2 = iter1;
        iter2++;
        while(*iter2 != ‘.‘)
            iter2++;
        iter3 = iter2;
        iter3++;
        while(*iter3 != ‘.‘)
            iter3++;
        version2ForwardInt = stoi(string(version2.begin(), iter1));
        version2BackwardInt = stoi(string(iter1 + 1, iter2));
        version2BBackwardInt = stoi(string(iter2 + 1, iter3));
        version2BBBackwardInt = stoi(string(iter3 + 1, version2.end()));
        cout << version2ForwardInt << endl;
        cout << version2BackwardInt << endl;
        cout << version2BBackwardInt << endl;
        cout << version2BBBackwardInt << endl;

        if(version1ForwardInt > version2ForwardInt)
            return 1;
        if(version1ForwardInt < version2ForwardInt)
            return -1;
        if(version1ForwardInt == version2ForwardInt) {
            if(version1BackwardInt > version2BackwardInt)
                return 1;
            if(version1BackwardInt < version2BackwardInt)
                return -1;
            if(version1BackwardInt == version2BackwardInt) {
                if(version1BBackwardInt > version2BBackwardInt)
                    return 1;
                if(version1BBackwardInt < version2BBackwardInt)
                    return -1;
                if(version1BBackwardInt == version2BBackwardInt) {
                    if(version1BBBackwardInt > version2BBBackwardInt)
                        return 1;
                    if(version1BBBackwardInt < version2BBBackwardInt)
                        return -1;
                    return 0;
                }   
            }
        }

        //return 0;


    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

Compare Version Numbers

标签:

原文地址:http://blog.csdn.net/guanzhongshan/article/details/46762069

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!