/*
用c++实现模拟音乐播放列表浏览功能,和实际的播放列表浏览功能一样。
操作:按键w向上浏览、s向下浏览、q推出控制台。
*/
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <conio.h>
using namespace std;
class song{
public:
song(){}
void setName(const string name)
{
this->name = name;
}
void setArtist(const string Artist)
{
this->artist = Artist;
}
void seTime(const float time)
{
this->time = time;
}
void setinFormation(const string information)
{
this->information = information;
}
public:
string name;
float time;
string artist;
string information;
};
class MP3{
public:
MP3(){}
void setMaxShow(int maxShow)
{
this->maxShow = maxShow;
}
void songAdd(const song song)
{
songlist.push_back(song);
songLength++;
}
void pageUp()
{
index = (index+songLength - 1)%songLength;
reloadCurrentList("up",index);
}
void pageDown()
{
index = (index+songLength + 1)%songLength;
reloadCurrentList("down",index);
}
void play()
{
int count = 0;
vector<song>::iterator it;
for(it = songlist.begin();it != songlist.end();it++)
{
currentsong.push_back(count);
if(count++>maxShow)
break;
}
index = 0;
}
void showScream()
{
int length = 0;
length = currentsong.size();
cout<<"---------------------------------------"<<endl;
for(int i=0;i<maxShow;i++)
{
if(index == currentsong.at(i))
cout<<songlist.at(currentsong.at(i)).name<<"<-- sing"<<endl;
else
cout<<songlist.at(currentsong.at(i)).name<<endl;
}
cout<<"---------------------------------------"<<endl;
}
void reloadCurrentList(string direction,int g_begin_index)
{
if(direction == "down")
{
if(g_begin_index>currentsong.at(maxShow-1))
{
g_begin_index = currentsong.at(1);
currentsong.clear();
for(int i = 0;i<maxShow;i++)
{
currentsong.push_back(g_begin_index);
g_begin_index = (g_begin_index+songLength + 1)%songLength;
}
}
else
if(g_begin_index == 0)
{
currentsong.clear();
for(int i = 0;i<maxShow;i++)
{
currentsong.push_back(g_begin_index);
g_begin_index = (g_begin_index+songLength + 1)%songLength;
}
}
}
if(direction == "up")
{
if(g_begin_index<currentsong.at(0))
{
currentsong.clear();
for(int i = 0;i<maxShow;i++)
{
currentsong.push_back(g_begin_index);
g_begin_index = (g_begin_index+songLength + 1)%songLength;
}
}
else
if( g_begin_index == songLength-1 )
{
currentsong.clear();
g_begin_index = songLength - maxShow;
for(int i = 0;i<maxShow;i++)
{
currentsong.push_back(g_begin_index);
g_begin_index = (g_begin_index+songLength + 1)%songLength;
}
}
}
}
private:
vector<song> songlist;
vector<int> currentsong;
int songLength = 0;
int maxShow = 0;
int index;
};
int main()
{
char keydown;
song song;
MP3 mp3;
mp3.setMaxShow(5);
char songname[256];
for(int i = 0;i<15;i++)
{
sprintf(songname,"NO.%d song",i);
song.setName(songname);
song.setArtist("dao lang");
song.setinFormation("mp3");
song.seTime(36.9);
mp3.songAdd(song);
}
mp3.play();
do
{
if(keydown == 'w')
mp3.pageUp();
if(keydown == 's')
mp3.pageDown();
if(keydown == 'q')
break;
system("cls");
keydown = 'r';
mp3.showScream();
}while(keydown = getchar());
return 0;
}