-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNamePool.cpp
107 lines (93 loc) · 2.13 KB
/
NamePool.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "NamePool.h"
#include <fstream>
#include <random>
using namespace std;
NamePool NamePool::name_pool;
NamePool::NamePool()
{
}
NamePool::~NamePool()
{
}
static default_random_engine e(time(NULL));
std::string NamePool::GetName()
{
if (m_pool.size() <= 0)
{
return "没有名字了";
}
/*随机取出姓*/
int first_index = e() % m_pool.size();
FirstName_vector &first_vec = m_pool[first_index];
string firstname = first_vec.m_first_name;
/*随机取出名*/
int name_index = e() % first_vec.m_last_vect.size();
string lastname = first_vec.m_last_vect[name_index];
/*摘除名字*/
first_vec.m_last_vect.erase(first_vec.m_last_vect.begin() + name_index);
/*若摘完所有名字了--》摘掉姓*/
if (first_vec.m_last_vect.size() <= 0)
{
m_pool.erase(m_pool.begin() + first_index);
}
/*拼接姓名*/
return firstname+" " + lastname;
}
void NamePool::ReleaseName(std::string _name)
{
/*拆分全名--》姓和名*/
int space_pos = _name.find(' ');
string first_name = _name.substr(0, space_pos);
string last_name = _name.substr(space_pos + 1, _name.size() - space_pos - 1);
bool flag = false;
/*根据姓找到名应该存放的容器*/
for (auto first : m_pool)
{
if (first_name == first.m_first_name)
{
/*找到了-》将名存到容器中*/
first.m_last_vect.push_back(last_name);
flag = true;
break;
}
}
if (false == flag)
{
/*没找到---》构造姓和姓所对应的名容器的对象---》存名*/
FirstName_vector tmp;
tmp.m_first_name = first_name;
tmp.m_last_vect.push_back(last_name);
/*存到姓名池中*/
m_pool.push_back(tmp);
}
}
bool NamePool::LoadFile()
{
/*读名文件,每行名字都存到vector里*/
ifstream name_file("random_last.txt");
if (true != name_file.is_open())
{
return false;
}
string tmp;
vector<string> tmp_last_name;
while (getline(name_file, tmp))
{
tmp_last_name.push_back(tmp);
}
/*读姓文件,每行姓都要添加一个姓名池的成员*/
ifstream first_file("random_first.txt");
if (true != first_file.is_open())
{
return false;
}
while (getline(first_file, tmp))
{
/*构造姓和其对应的名容器类的对象*/
FirstName_vector tmp_vect;
tmp_vect.m_first_name = tmp;
tmp_vect.m_last_vect = tmp_last_name;
m_pool.push_back(tmp_vect);
}
return true;
}