问题描述 建立一个供个人使用的音乐信息管理系统,用于存储、管理和快速查找歌曲信息;能够生成收藏夹和按歌名排序的播放列表。 2.1.2分析与设计提示 1.歌曲信息 音乐信息可能包括两方面的内容。 (1)音乐的基本信息,如名称、演唱者、创作者等 (2)根据个人查找偏好的查找信息,如类别、年份、喜爱程度、曲风等。 2.功能设计 作为一个乐库管理系统,最基本的功能如下。 (1)歌曲的增、删、改。通过这些操作更新乐库。 (2)歌曲查找。查找功能需考虑到用户的多种需求,如按演唱者查找、按歌名查找、按类别查找或多条件组合查找 (3)歌曲浏览。该功能可能是总歌单浏览,也可能是查找后的歌曲浏览。总歌单浏览可以显示歌曲的所有信息,也可以显示部分信息。 如果提供收藏夹功能,需考虑收藏夹的创建,在收藏夹中增、删、查歌 曲等功能。 除上述功能外,用户根据乐库管理的需求增设其他功能。例如,按查找次数形成热门歌单等。 3.存储设计 每一首歌曲有多个属性,查找时按关键字查找,关键字由用户的查找偏好决定。存储设计既要考虑歌曲信息管理的方便,也需考虑到查找的需要。用c++写这段代码
时间: 2024-03-26 10:39:13 浏览: 15
以下是用 C++ 实现音乐信息管理系统的代码示例:
```
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Song {
string name;
string singer;
string creator;
string category;
string year;
string style;
int likes;
int play_count;
};
class MusicLibrary {
vector<Song> songs;
vector<Song> favorites;
public:
void addSong(Song song) {
songs.push_back(song);
}
void removeSong(int index) {
songs.erase(songs.begin() + index);
}
void updateSong(int index, Song song) {
songs[index] = song;
}
vector<Song> searchSong(string keyword) {
vector<Song> result;
for (Song song : songs) {
if (song.name.find(keyword) != string::npos ||
song.singer.find(keyword) != string::npos ||
song.category.find(keyword) != string::npos ||
song.year.find(keyword) != string::npos ||
song.style.find(keyword) != string::npos) {
result.push_back(song);
}
}
return result;
}
vector<Song> sortBySongName() {
vector<Song> result = songs;
sort(result.begin(), result.end(), [](const Song& a, const Song& b) {
return a.name < b.name;
});
return result;
}
void viewAllSongs(bool showAllInfo) {
for (Song song : songs) {
if (showAllInfo) {
cout << "Name: " << song.name << endl;
cout << "Singer: " << song.singer << endl;
cout << "Creator: " << song.creator << endl;
cout << "Category: " << song.category << endl;
cout << "Year: " << song.year << endl;
cout << "Style: " << song.style << endl;
cout << "Likes: " << song.likes << endl;
cout << "Play Count: " << song.play_count << endl;
} else {
cout << song.name << " - " << song.singer << endl;
}
}
}
void addSongToFavorites(int index) {
favorites.push_back(songs[index]);
}
void removeSongFromFavorites(int index) {
favorites.erase(favorites.begin() + index);
}
vector<Song> searchSongInFavorites(string keyword) {
vector<Song> result;
for (Song song : favorites) {
if (song.name.find(keyword) != string::npos ||
song.singer.find(keyword) != string::npos ||
song.category.find(keyword) != string::npos ||
song.year.find(keyword) != string::npos ||
song.style.find(keyword) != string::npos) {
result.push_back(song);
}
}
return result;
}
vector<Song> sortByPlayCount() {
vector<Song> result = songs;
sort(result.begin(), result.end(), [](const Song& a, const Song& b) {
return a.play_count > b.play_count;
});
return result;
}
};
int main() {
MusicLibrary library;
// Add some songs to the library
Song song1 = {"Song 1", "Singer 1", "Creator 1", "Category 1", "Year 1", "Style 1", 10, 100};
Song song2 = {"Song 2", "Singer 2", "Creator 2", "Category 2", "Year 2", "Style 2", 20, 200};
Song song3 = {"Song 3", "Singer 3", "Creator 3", "Category 3", "Year 3", "Style 3", 30, 300};
library.addSong(song1);
library.addSong(song2);
library.addSong(song3);
// Search for a song
vector<Song> searchResult = library.searchSong("2");
cout << "Search Result:" << endl;
for (Song song : searchResult) {
cout << song.name << " - " << song.singer << endl;
}
cout << endl;
// Sort songs by name
vector<Song> sortedBySongName = library.sortBySongName();
cout << "Songs sorted by name:" << endl;
for (Song song : sortedBySongName) {
cout << song.name << " - " << song.singer << endl;
}
cout << endl;
// View all songs
cout << "All songs:" << endl;
library.viewAllSongs(false);
cout << endl;
// Add a song to favorites
library.addSongToFavorites(0);
cout << "Favorites:" << endl;
for (Song song : library.searchSongInFavorites("")) {
cout << song.name << " - " << song.singer << endl;
}
cout << endl;
// Remove a song from favorites
library.removeSongFromFavorites(0);
cout << "Favorites:" << endl;
for (Song song : library.searchSongInFavorites("")) {
cout << song.name << " - " << song.singer << endl;
}
cout << endl;
// Sort songs by play count
vector<Song> sortedByPlayCount = library.sortByPlayCount();
cout << "Songs sorted by play count:" << endl;
for (Song song : sortedByPlayCount) {
cout << song.name << " - " << song.play_count << endl;
}
cout << endl;
return 0;
}
```
在这个示例代码中,我们定义了一个Song结构体来表示每首歌曲的信息,以及一个MusicLibrary类来管理所有的歌曲和收藏夹。MusicLibrary类中实现了增、删、改、查、排序、浏览等功能,以及收藏夹的相关操作。我们在main函数中使用这个类来演示了这些功能的使用。
请注意,这只是一个示例代码,您可能需要根据自己的需求进行修改和扩展。