-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentence.cpp
More file actions
166 lines (133 loc) · 2.55 KB
/
Sentence.cpp
File metadata and controls
166 lines (133 loc) · 2.55 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "Sentence.hpp"
#include <cstdlib>
#include <fstream>
Sentence::Sentence()
{
}
Sentence::Sentence(const string & newSentence)
{
converting(newSentence);
}
Sentence::~Sentence()
{
}
void Sentence::setSentence(const string& newSentence)
{
sentence.clear();
converting(newSentence);
}
void Sentence::converting(const string & newSentence)
{
string temp;
for (unsigned int i = 0; i < newSentence.size(); i++)
{
if (newSentence[i] == ' ')
{
sentence.push_back(temp);
temp.clear();
}
else
temp.push_back(newSentence[i]);
}
sentence.push_back(temp);
}
void Sentence::showSentence()
{
for (unsigned int i = 0; i < sentence.size(); i++)
cout << sentence[i] << ' ';
cout << '\b';
}
void Sentence::replaceKey(const string & key, const string & dataKey)
{
for (unsigned int i = 0; i < sentence.size(); i++)
{
if (sentence[i] == key)
sentence[i] = dataKey;
}
}
string Sentence::getStr()
{
string str;
for (unsigned int i = 0; i < sentence.size(); i++)
{
str += sentence[i] + ' ';
}
str.pop_back();
str.push_back('.');
return str;
}
Generator::Generator()
{
loadData();
congratulation.push_back("Дорогой $surname$ поздравляю вас с $holiday$ и желаю вам $goods$");
congratulation.push_back("Желаю $goods$ в этот прекрасный $holiday$ самому лучшему $surname$");
}
Generator::~Generator()
{
}
string Generator::generate()
{
int index = rand() % congratulation.size();
static int i = 0;
string str = congratulation[index];
sentence.setSentence(str);
index = rand() % holiday.size();
sentence.replaceKey("$holiday$", holiday[index]);
index = rand() % goods.size();
sentence.replaceKey("$goods$", goods[index]);
if (i < surname.size())
{
sentence.replaceKey("$surname$", surname[i++]);
str = sentence.getStr();
}
else
{
str = "None";
}
return str;
}
void Generator::loadData()
{
ifstream file("holiday.txt");
string str;
char* letter = new char[255];
if (file.is_open())
{
while (true)
{
file.getline(letter, 254, '\n');
if (file.eof())
break;
str = letter;
holiday.push_back(str);
}
}
file.close();
file.open("goods.txt");
if (file.is_open())
{
while (true)
{
file.getline(letter, 254, '\n');
if (file.eof())
break;
str = letter;
goods.push_back(str);
}
}
file.close();
file.open("surname.txt");
if (file.is_open())
{
while (true)
{
file.getline(letter, 254, '\n');
if (file.eof())
break;
str = letter;
surname.push_back(str);
}
}
file.close();
delete[] letter;
}