forked from demon90s/CppStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_14_39.cpp
76 lines (62 loc) · 1.47 KB
/
exercise_14_39.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 练习14.39:修改上一题的程序令其报告长度在1至9之间的单词有多少个、长度在10
// 以上的单词又有多少个?
// 练习14.38:编写一个类令其检查某个给定的string对象的长度是否与一个阙值相等。
// 使用该对象编写程序,统计并报告在输入文件中长度为1的单词有多少个、长度为2
// 的单词有多少个、....、长度为10的单词又有多少个。
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
class SizeCmp1
{
public:
SizeCmp1(size_t lower, size_t upper) : m_lower(lower), m_upper(upper) {}
bool operator()(size_t len) const { return len >= m_lower && len < m_upper; }
private:
size_t m_lower;
size_t m_upper;
};
class SizeCmp2
{
public:
SizeCmp2(size_t lower) : m_lower(lower) {}
bool operator()(size_t len) const { return len >= m_lower; }
private:
size_t m_lower;
};
int main()
{
string file_name;
cin >> file_name;
ifstream ifs(file_name);
if (!ifs.is_open())
{
cerr << "open file fail" << endl;
return -1;
}
string line;
string word;
SizeCmp1 cmper1(1, 9);
SizeCmp2 cmper2(10);
size_t n1_9 = 0;
size_t n10 = 0;
while (getline(ifs, line))
{
istringstream iss(line);
while (iss >> word)
{
if (cmper1(word.length()))
{
++n1_9;
}
if (cmper2(word.length()))
{
++n10;
}
}
}
cout << "number of len in 1-9: " << n1_9 << endl;
cout << "number of len >= 10 : " << n10 << endl;
return 0;
}