简单的时间计算方法

#include <iostream>
#include <string>
enum _MLINE
{
    ML_FILE     = 5,
    ML_TEN      = 10,
    ML_FIFTEEN  = 15,
    ML_TWENTY   = 20,
    ML_THIRTY   = 30
};

int test1(std::string &r, double d, _MLINE mline);
int test2(std::string &mytime, _MLINE mline);

// 给一个时间 + 分钟数,计算结束时间
int test3(std::string &beginTime, int minutes);

int main(int argc, char *argv[])
{
    /*std::string r;
    test1(r, 4.0, ML_FILE);
    test1(r, 5.1, ML_TEN);
    test1(r, 5.2, ML_FIFTEEN);
    test1(r, 5.3, ML_TWENTY);
    test1(r, 5.4, ML_THIRTY);
    std::cout << "-------------" << std::endl;
    test1(r, 4.5, ML_FILE);
    test1(r, 5.6, ML_TEN);
    test1(r, 5.7, ML_FIFTEEN);
    test1(r, 5.8, ML_TWENTY);
    test1(r, 5.9, ML_THIRTY);*/

    /*
    std::string mytime = "5:20";
    test2(mytime, ML_FILE);
    test2(mytime, ML_TEN);
    test2(mytime, ML_FIFTEEN);
    test2(mytime, ML_TWENTY);
    test2(mytime, ML_THIRTY);

    std::cout << "--------------" << std::endl;
    mytime = "5:25";
    test2(mytime, ML_FILE);
    test2(mytime, ML_TEN);
    test2(mytime, ML_FIFTEEN);
    test2(mytime, ML_TWENTY);
    test2(mytime, ML_THIRTY);
    */

    std::string beginTime = "5:20";
    test3(beginTime, 150);
    return 0;
}

int test3(std::string &beginTime, int minutes)
{
    std::string::size_type pos = beginTime.find(":");
    if (std::string::npos == pos)
    {
        return -1;
    }

    int h = atoi(beginTime.substr(0, pos).c_str());
    int m = atoi(beginTime.substr(pos+1).c_str());

    m += minutes;
    h += m/60;
    m = m%60;

    std::cout << beginTime << ", " << minutes << "----" << h << ":" << m << std::endl;
    return 0;
}

int test1(std::string &r, double d, _MLINE mline)
{
    int h = int(d);
    int m = int(d * 60) % 60;
    m = (m/mline)*mline;
    std::cout << d << "---" << h << ":" << m << std::endl;

    return 0;
}

int test2(std::string &mytime, _MLINE mline)
{
    int topGutterHeight = 50;
    int hourHeight      = (60/mline) * 20;

    std::string::size_type pos = mytime.find(":");
    if (std::string::npos == pos)
    {
        return -1;
    }

    int h = atoi(mytime.substr(0, pos).c_str());
    int m = atoi(mytime.substr(pos+1).c_str());

    int y = topGutterHeight + hourHeight*h + (m/mline)*20;

    std::cout << mytime << "----" << "y=" << y << std::endl;

    return 0;
}

你可能感兴趣的:(时间)