-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeechManager.cpp
More file actions
228 lines (225 loc) · 5.24 KB
/
speechManager.cpp
File metadata and controls
228 lines (225 loc) · 5.24 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include"speechManager.h"
using namespace std;
speechManager::speechManager()
{
this->initspeech();
this->createSpeaker();
this->loadRecord();
}
void speechManager::Menu()
{
cout << "******************************************" << endl;
cout << "************ 歡迎參加演講比賽 ************" << endl;
cout << "************* 1.開始演講比賽 *************" << endl;
cout << "************* 2.查看歷史紀錄 *************" << endl;
cout << "************* 3.刪除比賽紀錄 *************" << endl;
cout << "************* 0.退出演講比賽 *************" << endl;
cout << "******************************************" << endl;
}
void speechManager::initspeech()
{
this->v1.clear();
this->v2.clear();
this->vVictory.clear();
this->m_speaker.clear();
this->m_index = 1;
}
void speechManager::createSpeaker()
{
string nameSpeed = "ABCDEFGHJKLM";
for (int i = 0; i < nameSpeed.size(); i++)
{
string name = "選手";
name += nameSpeed[i];
speaker sp;
sp.m_name = name;
for(int j = 0; j < 2; j++)
sp.m_Score[j] = 0;
v1.push_back(1001 + i);
m_speaker.insert(make_pair(1001+i, sp));
}
//cout << m_speaker[10011].m_name;
}
void speechManager::speechDraw()
{
cout << "-------------------------------" << endl;
cout << "第"<<m_index<<"輪抽簽開始" << endl;
cout << "結果: ";
if (m_index == 1)
{
random_shuffle(v1.begin(), v1.end());
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
cout << *it<<" ";
cout << endl;
}
else
{
random_shuffle(v2.begin(), v2.end());
for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
cout << *it << " ";
cout << endl;
}
cout << "-------------------------------" << endl;
system("pause");
cout << endl;
}
void speechManager::speechContest()
{
cout << "----------第" << m_index << "輪比賽開始----------" << endl;
int num = 0;
multimap<double, int, greater<double>> groupScore;
vector<int> v_Scr;
if (m_index == 1)
{
v_Scr = v1;
}
else
{
v_Scr = v2;
}
for (vector<int>::iterator it = v_Scr.begin(); it != v_Scr.end(); it++)
{
num++;
deque<double> d;
for (int i = 0; i < 10; i++)
{
double score = (rand() % 401 + 600) / 10.f;
d.push_back(score);
}
sort(d.begin(), d.end(), greater<double>());
d.pop_back();
d.pop_front();
double sum = accumulate(d.begin(), d.end(), 0.0f);
double avg = sum / d.size();
this->m_speaker[*it].m_Score[m_index - 1] = avg;
groupScore.insert(make_pair(avg, *it));
if (num % 6 == 0)
{
cout << "第<" << num / 6 << ">組名次: " << endl;
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end(); it++)
{
cout << "編號:" << it->second << " 姓名:" << m_speaker[it->second].m_name
<< " 分數:" << m_speaker[it->second].m_Score[m_index-1]<<endl;
}
//cout << endl;
int count = 0;
for (multimap<double, int, greater<double>>::iterator it = groupScore.begin(); it != groupScore.end() && count < 3; it++, count++)
{
if (m_index == 1)
v2.push_back(it->second);
else
vVictory.push_back(it->second);
}
groupScore.clear();
}
}
cout << "----------第" << m_index << "輪比賽結束----------" << endl;
system("pause");
}
void speechManager::showScore()
{
//cout << "-------------------------------" << endl;
cout << "----------第" << m_index << "輪晉級名單如下:----------" << endl;
vector<int> v_Scr;
if (m_index == 1)
v_Scr = v2;
else
v_Scr = vVictory;
for (vector<int>::iterator it = v_Scr.begin(); it != v_Scr.end(); it++)
cout << "編號:" << *it << " 分數:" << m_speaker[*it].m_Score[m_index - 1] << endl;;
cout << endl;
system("pause");
system("cls");
}
void speechManager::saveRecord()
{
fstream ofs;
ofs.open("speech.csv", ios::app | ios::out);
for (vector<int>::iterator it = vVictory.begin(); it != vVictory.end(); it++)
{
ofs << *it << "," << m_speaker[*it].m_name << "," << m_speaker[*it].m_Score[1]<<",";
}
ofs << endl;
ofs.close();
cout << "文件以保存" << endl;
}
void speechManager::loadRecord()
{
ifstream ifs("speech.csv", ios::in);
if (!ifs.is_open())
{
this->fileIsEmpty = true;
cout << "文件不存在" << endl;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件為空" << endl;
fileIsEmpty = true;
ifs.close();
return;
}
this->fileIsEmpty = false;
ifs.putback(ch);
string data;
vector<string> v;
int index = 0;
while (ifs>>data)
{
int pos = -1, start = 0;
while (true)
{
pos = data.find(",", start);
if (pos == -1)
{
break;
}
string temp = data.substr(start, pos - start);
v.push_back(temp);
start = pos + 1;
}
m_Record.insert(make_pair(m_index, v));
index++;
}
ifs.close();
for (map<int, vector<string>>::iterator it = m_Record.begin(); it != m_Record.end(); it++)
{
cout << it->first <<" " << it->second[0] << " " << it->second[2];
}
}
void speechManager::startSpeech()
{
//1開始抽籤
this->speechDraw();
//2開始比賽
this->speechContest();
//3顯示晉級結果
this->showScore();
//第二輪
m_index++;
this->Menu();
//1開始抽籤
this->speechDraw();
//2開始比賽
this->speechContest();
//3顯示晉級結果
this->showScore();
//4保存比賽紀錄
saveRecord();
m_index = 1;
cout << "本屆比賽已完畢" << endl;
system("pause");
system("cls");
}
void speechManager::exitSystem()
{
cout << "歡迎下次使用" << endl;
system("pause");
exit(0);
}
speechManager::~speechManager()
{
}