1037

//超时了,无法通过!
//

#include "stdafx.h"
#include
#include
#include

using namespace std;
using namespace std::regex_constants;

class Harry_Potter_Money
{
public:

    Harry_Potter_Money() = default;
    Harry_Potter_Money(string m) { str_to_money(m); }
    Harry_Potter_Money(long long k) { k_to_money(k); }

    long long to_knut();
    string money_to_str();

private:
    long long galleon = 0;
    long long sickle = 0;
    long long knut = 0;
    bool zf = false;
    void str_to_money(string m);
    void k_to_money(long long k);
};

void Harry_Potter_Money::str_to_money(string m)
{
    string pattern = "([[:alnum:]]+)\\.([[:alnum:]]+)\\.([[:alnum:]]+)";
    regex r(pattern);
    smatch part_num;
    if (regex_search(m, part_num, r))
    {
        galleon = stoll(part_num.str(1));   //strtoll为c的函数,针对const char*!
        sickle = stoll(part_num.str(2));
        knut = stoll(part_num.str(3));
    }
}

void Harry_Potter_Money::k_to_money(long long k)
{
    if (k < 0)
    {
        k = -k;
        zf = true;
    }
    galleon = k / 493;
    long long tmp = k % 493;
    sickle = tmp / 29;
    knut = tmp % 29;
}

string Harry_Potter_Money::money_to_str()
{
    string tmp;

    if (true == zf)
    {
        tmp = tmp + string("-");
    }
    tmp = tmp+to_string(galleon) + string(".") + to_string(sickle) + string(".") + to_string(knut);
    return tmp;
}

long long Harry_Potter_Money::to_knut()
{
    long long k = 0;
    k = 493 * galleon + 29 * sickle + knut;
    return k;
}

int main()
{
    string own_money, paid_money;
    cin >> paid_money>> own_money;

    Harry_Potter_Money ow(own_money);
    Harry_Potter_Money pd(paid_money);

    long long tmp = ow.to_knut()-pd.to_knut();
    Harry_Potter_Money out(tmp);

    cout << out.money_to_str();
    


    system("pause");
    return 0;
}

你可能感兴趣的:(1037)