我的预想是按2以后,根据1 2 3功能,分别进行歌曲播放,然后进入主界面,从新选择管理,播放歌曲,显示歌曲
但是按2以后再按1 ,输入自己想播放的那首歌,然后可以根据1 2 3切换歌曲,但是这里就出现bug了,没办法按照预想来
按2以后按目录列表播放,再按1 2 3进行歌曲切换,但是这里也出现问题
#include<iostream>
#include<string>
#include<vector>
#include <ctime>
#include <random>
#include<conio.h>
using namespace std;
class Album;
class Singer;
void shuanghengxian()
{
cout << "========================================================================================" << endl;
}
void _Operation() //操作函数
{
shuanghengxian();
cout << "1.管理歌曲" << endl << "2.播放歌曲" << endl << "3.查看歌曲" << endl;
shuanghengxian();
}
void bolangxian()
{
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
class Album //专辑类
{
public:
string name; //专辑名
Singer* singer; //演唱者
Album(string name, Singer* singer)
{
this->name = name;
this->singer = singer;
}
};
class Singer //歌手类
{
public:
string name; //姓名
//vector<string>songs; //所有歌曲名
Singer(string name)
{
this->name = name;
}
};
class Song //歌曲类
{
public:
string name; //歌名
Album* album; //专辑
Singer* singer; //演唱者
Song(string name, Album* album)
{
this->name = name;
this->album = album;
this->singer = album->singer;
}
void viewSingerProfile(); //查看歌手资料
void viewAlbumProfile(); //查看专辑资料
};
void Song::viewAlbumProfile()
{
cout << "专辑名:" << this->album->name << endl;
}
void Song::viewSingerProfile()
{
cout << "歌手名:" << this->singer->name << endl;
}
class Player //播放器类
{
public:
vector<Song>* list; //播放列表
int mode; //播放模式
Player()
{
}
Player(vector<Song>* list)
{
this->list = list;
//showList();
}
void showList(); //显示播放列表
void chooseOperation(); //提示选择操作
void manageSongs(); //管理歌曲
void add(); //添加歌曲
void _delete(); //删除歌曲
void play(); //播放
void pause(); //暂停
void previous(int a); //上一曲
void next(int a); //下一曲
void chooseMode(); //提示选择播放模式
void viewSongProfile(); //查看歌曲详情
void randomPlay(); //随机播放
};
void Player::previous(int a)
{
if (a == 1)
{
cout << "当前正在播放:" << endl;
cout << "歌名:" << list->back().name << "\t" << "歌手:" << list->back().singer->name << "\t" << "专辑:" << list->back().album->name << endl;
}
else
{
cout << "当前正在播放:" << endl;
cout << "歌名:" << (*list)[a - 1].name << "\t" << "歌手:" << (*list)[a - 1].singer->name << "\t" << "专辑:" << (*list)[a - 1].album->name << endl;
}
}
void Player::next(int a)
{
if (a == list->size())
{
cout << "当前正在播放:" << endl;
cout << "歌名:" << list->front().name << "\t" << "歌手:" << list->front().singer->name << "\t" << "专辑:" << list->front().album->name << endl;
}
else
{
cout << "当前正在播放:" << endl;
cout << "歌名:" << (*list)[a - 1].name << "\t" << "歌手:" << (*list)[a - 1].singer->name << "\t" << "专辑:" << (*list)[a - 1].album->name << endl;
}
}
void Player::chooseMode()
{
cout << "请选择播放类型:" << endl;
cout << "1.单曲循环" << endl << "2.列表循环" << endl << "3.随机播放" << endl;
}
void Player::chooseOperation()
{
cout << "1.管理歌曲" << endl << "2.播放歌曲" << endl << "3.查看歌曲" << endl;
}
void Player::randomPlay()
{
default_random_engine e;
uniform_int_distribution<int> u(1, list->size());// 左闭右闭区间
e.seed(time(0));
//for (int i = 0; i < 10; i++) {
// std::cout << u(e) << std::endl;
//}
cout << "当前正在播放:" << endl;
cout << "歌名:" << (*list)[u(e) - 1].name << "\t" << "歌手:" << (*list)[u(e) - 1].singer->name << "\t" << "专辑:" << (*list)[u(e) - 1].album->name << endl;
cout << "1.暂停;2.上一首;3.下一首" << endl;
while (1)
{
char c = _getch();
switch (c)
{
case '1':
pause();
break;
case '2':
previous(u(e));
break;
case '3':
next(u(e));
break;
}
break;
}
}
void Player::add()
{
string singer_name;
string album_name;
string song_name;
cout << "请输入您想添加歌曲的 歌名 歌手 专辑:" << endl;
cin >> song_name >> singer_name >> album_name;
//Singer singer(singer_name);
//Album album(album_name, &singer_name);
//Song song(song_name, &album);
//(*list).push_back(song);
Singer* singer = new Singer(singer_name);
Album* album = new Album(album_name, singer);
Song song(song_name, album);
(*list).push_back(song);
//delete singer, album;
}
void Player::_delete()
{
cout << "请输入您想删除的歌曲序号:" << endl;
shuanghengxian();
int a;
cin >> a;
if (a <= 0 || a > (*list).size())
{
cout << "请重新输入1-" << (*list).size() << "的数" << endl;
}
for (int i = a - 1; i < (*list).size() - 1; i++)
{
(*list)[i].name = (*list)[i + 1].name;
(*list)[i].singer->name = (*list)[i + 1].singer->name;
(*list)[i].album = (*list)[i + 1].album;
}
list->pop_back();
}
void Player::pause()
{
cout << "当前歌曲已暂停播放" << endl;
_Operation();
}
void Player::play()
{
chooseMode();
shuanghengxian();
while (1)
{
char c = _getch();
switch (c)
{
case '1':
cout << "请输入1-" << list->size() << "之间的歌曲序号" << endl;
/* cout << "请输入您要播放的歌曲序号:" << endl;*/
shuanghengxian();
int PLS;
cin >> PLS;
cout << "当前正在播放:" << endl;
cout << "歌名:" << (*list)[PLS - 1].name << "\t" << "歌手:" << (*list)[PLS - 1].singer->name << "\t" << "专辑:" << (*list)[PLS - 1].album->name << endl;
cout << "1.暂停;2.上一首;3.下一首" << endl;
while (1)
{
char c = _getch();
cin >> c;
switch (c)
{
case '1':
pause();
break;
case '2':
previous(PLS);
break;
case '3':
next(PLS);
break;
default:
printf("请重新输入1-3中的数字\n");
}
break;
}
break;
case '2':
cout << "当前正在播放:" << endl;
cout << "歌名:" << list->front().name << "\t" << "歌手:" << list->front().singer->name << "\t" << "专辑:" << list->front().album->name << endl;
cout << "1.暂停;2.上一首;3.下一首" << endl;
while (1)
{
char c = _getch();
switch (c)
{
case '1':
pause();
break;
case '2':
previous(1);
break;
case '3':
next(1);
break;
default:
printf("请重新输入1-3中的数字\n");
}
}
break;
case '3':
randomPlay();
break;
default:
printf("请重新输入1-3中的数字\n");
}
break;
}
}
void Player::showList()
{
shuanghengxian();
for (int i = 0; i < (*list).size(); i++)
{
cout << i + 1 << "." << "歌名:" << (*list)[i].name << "\t" << "歌手:" << (*list)[i].singer->name << "\t" << "专辑:" << (*list)[i].album->name << endl;
}
_Operation();
}
void Player::viewSongProfile()
{
cout << "请输入您需要查看的歌曲或者歌手或者专辑" << endl;
shuanghengxian();
string SSA; //用于判断
cin >> SSA;
for (int i = 0; i < (*list).size(); i++)
{
if (SSA == (*list)[i].name || SSA == (*list)[i].album->name || SSA == (*list)[i].singer->name)
{
cout << "歌名:" << (*list)[i].name << " " << "歌手:" << (*list)[i].singer->name << " " << "专辑:" << (*list)[i].album->name << endl;
}
}
shuanghengxian();
}
void Player::manageSongs()
{
shuanghengxian();
cout << "请选择 1.添加歌曲 2.删除歌曲" << endl;
shuanghengxian();
while (1)
{
char c = _getch();
switch (c)
{
case '1':
add();
break;
case '2':
_delete();
break;
default:
printf("请重新输入1-2中的数字\n");
////暂停程序
//system("pause");
////清空控制台
//system("cls");
//break;
}
break;
}
_Operation();
}
class User //用户类
{
public:
int operation; //操作
void sendOperation(Player* player, int Operation) //用户功能函数
{
/* while (1)
{
char c = _getch();
{*/
switch (Operation)
{
case 1:
(*player).manageSongs();
break;
case 2:
(*player).play();
break;
case 3:
(*player).showList();
break;
}
}
};
void system_songs(vector<Song>& _songs)
{
//Singer* singer1 = new Singer("薛之谦"); // 改成new
Singer* singer1 = new Singer("薛之谦");
//singer1.songs.push_back("怪咖"); 此处可添加歌手具体信息,对于此播放器操作功能有限,可不实现
Album* album1 = new Album("沉默羔羊", singer1);
Song song1("演员", album1);
_songs.push_back(song1);
Singer* singer2 = new Singer("伍佰");
Album* album2 = new Album("几时回忆", singer2);
Song song2("突然的自我", album2);
_songs.push_back(song2);
Singer* singer3 = new Singer("陈一发儿");
Album* album3 = new Album("电竞贾玲", singer3);
Song song3("童话镇", album3);
_songs.push_back(song3);
Singer* singer4 = new Singer("李荣浩");
Album* album4 = new Album("小眼帅哥", singer4);
Song song4("作曲家", album4);
_songs.push_back(song4);
Singer* singer5 = new Singer("邓紫棋");
Album* album5 = new Album("腿长一米五", singer5);
Song song5("喜欢你", album5);
_songs.push_back(song5);
Singer* singer6 = new Singer("周杰伦");
Album* album6 = new Album("Jay", singer6);
Song song6("简单爱", album6);
_songs.push_back(song6);
Singer* singer7 = new Singer("Beyond");
Album* album7 = new Album("超越", singer7);
Song song7("光辉岁月", album7);
_songs.push_back(song7);
Singer* singer8 = new Singer("陈奕迅");
Album* album8 = new Album("我叫eason", singer8);
Song song8("十年", album8);
_songs.push_back(song8);
Singer* singer9 = new Singer("张国荣");
Album* album9 = new Album("峥嵘岁月", singer9);
Song song9("当爱已成往事", album9);
_songs.push_back(song9);
Singer* singer10 = new Singer("许嵩");
Album* album10 = new Album("年少回忆", singer10);
Song song10("南山忆", album10);
_songs.push_back(song10);
/*for (int i = 0; i < 10; i++)
{
cout << i + 1 << "." << "歌名:" << _songs[i].name << "\t" << "歌手:" << _songs[i].singer->name << "\t" << "专辑:" << _songs[i].album->name << endl;
}*/
/*delete singer1, album1, singer2, album2, singer3, album3, singer4, album4, singer5, album5, singer6, album6, singer7, album7, singer8, album8, singer9, album9, singer10, album10;*/
}
int main(void)
{
User user;
vector<Song>_songs;
system_songs(_songs);
Player player(&_songs);
bolangxian();
cout << "欢迎使用控制台音乐播放器" << endl;
bolangxian();
player.showList();
cout << "已展示当前所有歌曲,请选择您的操作:" << endl;
Jump:int a;
cin >> a;
if (a == 1 || a == 2 || a == 3)
{
user.sendOperation(&player, a);
}
else
{
cout << "请输入1-3之间的数字" << endl;
}
goto Jump;
}
造成这个问题的原因是,当进入自由播放模式后,播放器没有记录当前歌曲所属的目录列表,无法确定应该从哪个目录列表中继续切换歌曲,添加个变量记录即可