帮我用c加加写一个个人乐库管理系统
时间: 2023-11-12 15:04:24 浏览: 40
好的,下面是一个简单的个人乐库管理系统的代码示例,包括添加歌曲、删除歌曲、浏览歌曲等基本功能。代码中使用了结构体来存储歌曲信息,使用动态数组来存储多个歌曲信息。
```c++
#include <iostream>
#include <string>
using namespace std;
// 歌曲结构体
struct Song {
string name; // 歌曲名
string artist; // 艺术家
string album; // 专辑
int year; // 发行年份
};
// 添加歌曲
void addSong(Song*& songs, int& count) {
// 获取用户输入的歌曲信息
cout << "请输入歌曲名:";
string name;
getline(cin, name);
cout << "请输入艺术家:";
string artist;
getline(cin, artist);
cout << "请输入专辑:";
string album;
getline(cin, album);
cout << "请输入发行年份:";
int year;
cin >> year;
cin.ignore(); // 忽略回车符
// 创建新歌曲结构体
Song newSong = { name, artist, album, year };
// 将新歌曲结构体添加到动态数组中
Song* newSongs = new Song[count + 1];
for (int i = 0; i < count; i++) {
newSongs[i] = songs[i];
}
newSongs[count] = newSong;
count++;
delete[] songs;
songs = newSongs;
cout << "添加成功!" << endl;
}
// 删除歌曲
void deleteSong(Song*& songs, int& count) {
// 获取用户要删除的歌曲名
cout << "请输入要删除的歌曲名:";
string name;
getline(cin, name);
// 查找要删除的歌曲
int index = -1;
for (int i = 0; i < count; i++) {
if (songs[i].name == name) {
index = i;
break;
}
}
if (index == -1) {
cout << "没有找到该歌曲!" << endl;
return;
}
// 从动态数组中删除该歌曲
for (int i = index; i < count - 1; i++) {
songs[i] = songs[i + 1];
}
count--;
Song* newSongs = new Song[count];
for (int i = 0; i < count; i++) {
newSongs[i] = songs[i];
}
delete[] songs;
songs = newSongs;
cout << "删除成功!" << endl;
}
// 浏览歌曲
void viewSongs(Song* songs, int count) {
cout << "歌曲列表:" << endl;
for (int i = 0; i < count; i++) {
cout << "歌曲名:" << songs[i].name << endl;
cout << "艺术家:" << songs[i].artist << endl;
cout << "专辑:" << songs[i].album << endl;
cout << "发行年份:" << songs[i].year << endl;
cout << endl;
}
}
int main() {
Song* songs = NULL; // 动态数组指针,用于存储歌曲信息
int count = 0; // 当前歌曲数量
while (true) {
// 输出菜单
cout << "===== 个人乐库管理系统 =====" << endl;
cout << "1. 添加歌曲" << endl;
cout << "2. 删除歌曲" << endl;
cout << "3. 浏览歌曲" << endl;
cout << "0. 退出" << endl;
cout << "请输入操作编号:";
int choice;
cin >> choice;
cin.ignore(); // 忽略回车符
switch (choice) {
case 1:
addSong(songs, count);
break;
case 2:
deleteSong(songs, count);
break;
case 3:
viewSongs(songs, count);
break;
case 0:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "无效的操作编号!" << endl;
break;
}
cout << endl;
}
return 0;
}
```
这个代码示例只是一个简单的个人乐库管理系统,还有很多功能可以进一步完善。希望能对你有所帮助!