您的位置:首页 > 编程语言 > C语言/C++

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

2016-11-15 22:20 274 查看
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<
4000
/span> || 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 ∈
};
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: