C++primer5th十四章_几个类的运算符重载

class Cdate {
    friend bool operator>(const Cdate &d1, const Cdate &d2);
    friend bool operator<(const Cdate &d1, const Cdate &d2);
    friend bool operator== (const Cdate &d1, const Cdate &d2);
    friend bool operator!= (const Cdate &d1, const Cdate &d2);
    friend Cdate operator+(const Cdate&, const Cdate&);
    friend ostream& operator << (ostream&, const Cdate&);
    friend istream& operator >> (istream& in, Cdate &d);

public:
    Cdate() = default;
    Cdate(int y, int m, int d) :year(y), month(m), day(d) {}
    Cdate operator+= (const Cdate&);
    Cdate& operator= (const string&);
    Cdate& operator++ ()
    {
        year++;
        month++;
        day++;
        return *this;
    }
    Cdate operator++(int)
    {
        Cdate dat = *this;
        ++*this;
        return dat;
    }
private:
    int year = 0, month = 0, day = 0;
};
Cdate& Cdate::operator= (const string &s)
{
    char spc1, spc2;
    istringstream in(s);
    in >> year >> spc1 >> month >> spc2 >> day;
    cout << "year is:" << year << " month is:" << month << " day is:" << day << endl;
    if (!in || spc1 != '-' || spc2 != '-')
        throw invalid_argument("bad date");
    if (month > 12 || month < 1 || day < 1 || day>31)
        throw invalid_argument("bad date");
    return *this;
}
Cdate operator +(const Cdate &d1, const Cdate &d2)
{
    Cdate d = d1;
    d += d2;
    return d;
}
Cdate Cdate::operator+= (const Cdate &d)
{
    year += d.year;
    month += d.month;
    day += d.day;
    return *this;
}
ostream& operator << (ostream& out, const Cdate &d)
{
    out << d.year << " 年 " << d.month << " 月 " << d.day << " 日";
    return out;
}
istream& operator >> (istream& in, Cdate &d)
{
    in >> d.year >> d.month >> d.day;
    if (!in)
        d = Cdate();
    return in;
}
bool operator== (const Cdate &d1, const Cdate &d2)
{
    return d1.day == d2.day && d1.month == d2.month && d1.year == d2.year;
}
bool operator!= (const Cdate &d1, const Cdate &d2)
{
    return !(d1 == d2);
}
bool operator<(const Cdate &d1, const Cdate &d2)
{
    return((d1.year < d2.year) || (d1.year == d2.year&& d1.month < d2.month) || (d1.year == d2.year&&d1.month == d2.month &&d1.day < d2.day));
}
bool operator>(const Cdate &d1, const Cdate &d2)
{
    return !(d1 < d2) && d1 != d2;
}

int main()
{
    Cdate d1, d2;
    string s1, s2;
    while (true)
    {
        cout << "请输入d1" << endl;
        cin >> d1;
        cout << "请输入d2" << endl;
        cin >> d2;
        cout << d1 << " " << d2 << endl;
        cout << "++d1:" << ++d1 << " ++d2:" << ++d2 << endl;
        cout << "d1++:" << d1++ << " d2++:" << d2++ << endl;
        cout << d1 << " " << d2 << endl;
    }
    return 0;
}

class PrintString {
public:
    PrintString(ostream &o = cout, char s = ' ') :os(o), sep(s) {}
    void operator() (const string &str) const { os << str << sep; }
private:
    char sep;
    ostream &os;
};
class IfElseThen {
public:
    IfElseThen() {};
    IfElseThen(int i1, int i2, int i3) :iv1(i1), iv2(i2), iv3(i3) {}
    int operator() (int i1, int i2, int i3) { return i1 ? i2 : i3; }
private:
    int iv1, iv2, iv3;
};
class ReadString {
public:
    ReadString(istream &i = cin) :in(i) {}
    string operator() () {
        string s;
        if (!getline(in, s))
            s = "";
        return s;
    }
private:
    istream &in;
};
class IntCompare {
public:
    IntCompare(int v) :val(v) {};
    bool operator() (int v) { return val == v; };
private:
    int val;
};
class IsString {
public:
    IsString(size_t t, size_t t2) :sz1(t), sz2(t2) {}
    bool operator() (const string &str) { return str.size() > sz1 && str.size() < sz2; }
private:
    size_t sz1;
    size_t sz2;
};
int main()
{
    IsString is(1, 9);
    vector<string> vi;
    ifstream in("data.txt");
    if (!in)
        return -1;
    string line;
    while (getline(in, line)) {
        string word;
        istringstream iss(line);
        while (iss >> word)
            vi.push_back(word);
    }
    for (size_t i = 1; i < 11; i++) {
        cout << "长度为" << i << "的单词数量为:" << count_if(vi.begin(), vi.end(), is) << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值