POOPE 发表于 2021-7-30 18:12:40

C++ MP3播放器按钮类似功能实现

// Class01.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <stdlib.h>
#include <vector>
#include <string>
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;

class Mp3Menu
{
private:
vector<string> SongList;//歌曲容器,用于保存歌曲列表
    vector<string>::iterator SongListIter;//容器迭代器,用于指示歌曲
int ListNO;
public:
Mp3Menu();
Mp3Menu(vector<string> &SongList);
void Interface();//展示歌曲列表的界面函数,用于上下的功能
void AddSong(const string SongName);//添加歌曲的函数
void Init();
};

void Mp3Menu::Init()
{
this->SongListIter = SongList.begin();
this->ListNO = 0;
}

Mp3Menu::Mp3Menu()
{
SongListIter = SongList.begin();
this->ListNO = 0;
}

Mp3Menu::Mp3Menu(vector<string> &SongList)
{
this->SongList = SongList;
this->SongListIter = SongList.begin();
this->ListNO = 0;
}

void Mp3Menu::AddSong(const string SongName)
{
this->SongList.push_back(SongName);
}

void Mp3Menu::Interface()
{
char button;
int SongNumber = 0,i = 0,FourSong = 1;
int CurrentNO = 1;
Init();
while( this->SongListIter != this->SongList.end() )
{
this->SongListIter++;
SongNumber++;
}
while( 1 )
{
system("cls");
if( (FourSong >= SongNumber-3) && SongNumber >= 4 )
FourSong = SongNumber - 3;
if( SongNumber >= 4 )
{
for( i = 1; i <= 4; i++ )
{
cout<<"--------------------"<<endl;
if( i == CurrentNO )
cout<<"-->"<<FourSong<<"."<<SongList.at(FourSong)<<endl;
else
cout<<FourSong<<"."<<SongList.at(FourSong)<<endl;
FourSong++;
}
}
else
{
for( i = 1; i <= SongNumber; i++ )
{
cout<<"--------------------"<<endl;
if( i == CurrentNO )
cout<<"-->"<<FourSong<<"."<<SongList.at(FourSong)<<endl;
else
cout<<FourSong<<"."<<SongList.at(FourSong)<<endl;
FourSong++;
}
}
button = getch();
switch (button)
{
case '0':
exit(1);break;
case 's':
this->ListNO++;
CurrentNO++;
if( this->ListNO > SongNumber - 1 )
{
this->ListNO = 0;
CurrentNO = 1;
}
if( CurrentNO > 4 )
{
CurrentNO = 4;
}
break;
case 'w':
this->ListNO--;
//CurrentNO--;
if( this->ListNO < 0 )
{
this->ListNO = SongNumber - 1;
CurrentNO = 4;
}
if( this->ListNO <= 3 )
CurrentNO--;
if( CurrentNO <= 0 )
{
CurrentNO = 1;
}
break;
default:break;
}
if( this->ListNO > 3 )
FourSong = this->ListNO - 3;
else
FourSong = 1;
}

}



int _tmain(int argc, _TCHAR* argv[])
{
vector<string> SongList;
string temp;
string SongName;
//for( i = 0; i< 20; i++ )
//{
//
//SongName = "love" + temp;
//SongList.push_back(SongName);
//}
SongList.push_back("a");
SongList.push_back("b");
SongList.push_back("c");
SongList.push_back("d");
SongList.push_back("f");
SongList.push_back("e");
SongList.push_back("f");
SongList.push_back("g");
SongList.push_back("h");
SongList.push_back("i");
SongList.push_back("j");
SongList.push_back("k");
Mp3Menu test(SongList);
test.Interface();

return 0;
}




文档来源:51CTO技术博客https://blog.51cto.com/u_6220803/3198413
页: [1]
查看完整版本: C++ MP3播放器按钮类似功能实现