c++高级编程 音乐播放器歌曲列表翻页

/*

用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;
}
 
 
 
 
 
 
 
 

                
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值