-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSong.cpp
More file actions
245 lines (232 loc) · 5.55 KB
/
Copy pathSong.cpp
File metadata and controls
245 lines (232 loc) · 5.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <algorithm> // std::sort / std::find_if / std::find_if_not
#include <iostream>
#include <string>
#include <vector>
#include "Song.h"
int Song::next_id_ = 1;
namespace
{
std::string trim_copy(const std::string &s)
{
std::string stripHead;
bool start = false;
for (int i = 0; i < s.length(); ++i)
{
if (start)
{
stripHead = stripHead + s[i];
}
else
{
if (s[i] != ' ')
{
stripHead = stripHead + s[i];
start = true;
}
}
}
std::string stripTail;
bool end = false;
for (int i = stripHead.length() - 1; i >= 0; --i)
{
if (end)
{
stripTail = stripHead[i] + stripTail;
}
else
{
if (stripHead[i] != ' ')
{
stripTail = stripTail + stripHead[i];
end = true;
}
}
}
return stripTail;
}
std::string to_lower_copy(const std::string &s)
{
std::string lowerString;
for (unsigned char c : s)
{
lowerString += static_cast<char>(std::tolower(c));
}
return lowerString;
}
std::string join_tags(const std::vector<std::string> &tags)
{
if (tags.empty())
{
return "";
}
std::string result;
for (int i = 0; i < tags.size() - 1; ++i)
{
result += tags[i];
result += ", ";
}
result += tags.back();
return result;
}
}
Song::Song(const std::string &title, const std::string &artist, int duration_sec, int rating)
{
std::string trimTitle = trim_copy(title);
std::string trimArtist = trim_copy(artist);
bool error = false;
if (trimTitle.empty())
{
std::cerr << "[错误] 标题不能为空\n";
error = true;
}
if (trimArtist.empty())
{
std::cerr << "[错误] 艺人不能为空\n";
error = true;
}
if (duration_sec <= 0)
{
std::cerr << "[错误] 时长必须为正整数(秒)\n";
error = true;
}
if (rating < 1 || rating > 5)
{
std::cerr << "[错误] 评分必须在 1...5 之间\n";
error = true;
}
if (error)
{
valid_ = false;
return;
}
id_ = next_id_++;
title_ = trimTitle;
artist_ = trimArtist;
duration_sec_ = duration_sec;
rating_ = rating;
valid_ = true;
}
bool Song::set_title(const std::string &t)
{
std::string trimTitle = trim_copy(t);
if (trimTitle.empty())
{
std::cerr << "[提示] 标题不能为空,已忽略本次修改\n";
return false;
}
title_ = trimTitle;
return true;
}
bool Song::set_artist(const std::string &a)
{
std::string trimArtist = trim_copy(a);
if (trimArtist.empty())
{
std::cerr << "[提示] 艺人不能为空,已忽略本次修改\n";
return false;
}
artist_ = trimArtist;
return true;
}
bool Song::set_duration(int sec)
{
if (sec <= 0)
{
std::cerr << "[提示] 时长需为正整数,已忽略本次修改\n";
return false;
}
duration_sec_ = sec;
return true;
}
bool Song::set_rating(int r)
{
if (r < 1 || r > 5)
{
std::cerr << "[提示] 评分需在 1..5,已忽略本次修改\n";
return false;
}
rating_ = r;
return true;
}
bool Song::add_tag(const std::string &tag)
{
std::string trimTag = trim_copy(tag);
if (trimTag.empty())
{
std::cout << "[提示] 空标签已忽略\n";
return false;
}
std::string lowerTag = to_lower_copy(trimTag);
for (const auto &t : tags_)
{
if (to_lower_copy(t) == lowerTag)
{
std::cout << "[提示] 标签已存在(忽略大小写)\n";
return false;
}
}
tags_.push_back(trimTag);
return true;
}
bool Song::remove_tag(const std::string &tag)
{
std::string trimTag = trim_copy(tag);
std::string lowerTag = to_lower_copy(trimTag);
for (int i = 0; i < tags_.size(); ++i)
{
if (to_lower_copy(tags_[i]) == lowerTag)
{
tags_.erase(tags_.begin() + i);
return true;
}
}
std::cout << "[提示] 未找到该标签\n";
return false;
}
bool Song::matches_keyword(const std::string &kw) const
{
std::string trimKeyword = trim_copy(kw);
if (trimKeyword.empty())
return false;
std::string keyword = to_lower_copy(trimKeyword);
if (to_lower_copy(title_).find(keyword) != std::string::npos)
{
return true;
}
if (to_lower_copy(artist_).find(keyword) != std::string::npos)
{
return true;
}
for (const auto &t : tags_)
{
if (to_lower_copy(t).find(keyword) != std::string::npos)
{
return true;
}
}
return false;
}
std::ostream &operator<<(std::ostream &os, const Song &s)
{
os << "[#" << s.id_ << "] "
<< s.artist_ << " - " << s.title_
<< " (" << s.duration_sec_ << "s) "
<< std::string(s.rating_, '*');
if (!s.tags_.empty())
{
os << " [tags: " << join_tags(s.tags_) << "]";
}
return os;
}
bool operator<(const Song &a, const Song &b)
{
if (a.rating_ != b.rating_)
{
return a.rating_ > b.rating_;
}
if (a.title_ != b.title_)
{
return a.title_ < b.title_;
}
return a.id_ < b.id_;
}