码迷,mamicode.com
首页 > 编程语言 > 详细

C++信息管理系统练习

时间:2020-07-30 01:44:14      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:bre   while   sys   pac   div   fine   for   出版   mui   

音乐管理器

#include<iostream>
#include<string>
#include<unistd.h>
using namespace std;

#define MAX 300

//界面清空函数
void clear()
{
    char buf[1024]={0};
    cout<<"请输入回车键继续……"<<endl;
    fgets(buf,1024,stdin);
    system("clear");
}
 
//主界面显示函数
void showMenu()
{
    //主界面
    cout << "****  1.  添加歌曲\t****" <<endl;
    cout << "****  2.  显示歌曲\t****" <<endl;
    cout << "****  3.  删除歌曲\t****" <<endl;
    cout << "****  4.  查找歌曲\t****" <<endl;
    cout << "****  5.修改歌曲信息\t****" <<endl;
    cout << "****  6.清空全部歌曲\t****" <<endl;
    cout << "****  0.  退出应用\t****" <<endl;
}

struct music
{
    string m_name;
    string m_singer;
    string m_album;
    string m_time;
    string m_publish;
};

struct music_bank
{
    
    struct music musicArray[MAX];//muisc内最大容量
    
    int m_size;//记录歌曲个数


};

//添加歌曲函数
void add_music(music_bank *abs)
{
    if (abs->m_size == MAX)
    {
        cout <<"音乐库已满"<<endl;
    }
    else
    {
        string name;
        cout<<"请输入歌曲名:"<<endl;
        cin>>name;
        abs->musicArray[abs->m_size].m_name = name;

        string singer;
        cout<<"请输入歌手:"<<endl;
        cin>>singer;
        abs->musicArray[abs->m_size].m_singer = singer;

        string album;
        cout<<"请输入专辑:"<<endl;
        cin>>album;
        abs->musicArray[abs->m_size].m_album = album;

        string time;
        cout<<"请输入时长:"<<endl;
        cin>>time;
        abs->musicArray[abs->m_size].m_time = time;

        string publish;
        cout<<"请输入出版日期:"<<endl;
        cin>>publish;
        char buf[1024]={0};
        fgets(buf,1024,stdin); 
        abs->musicArray[abs->m_size].m_publish = publish;

        abs->m_size++;
        cout<<"添加成功"<<endl;
        clear();
    }
}

//显示歌曲信息函数
void show_music(music_bank *abs)
{
    if(abs->m_size==0)
    {
        cout<<"目前没有歌曲在库中"<<endl;
    }
    else
    {
        for(int i=0;i<(abs->m_size);i++)
        {
            cout<<"歌曲名:"<<abs->musicArray[i].m_name<<"\t";
            cout<<"歌手名:"<<abs->musicArray[i].m_singer<<"\t";
            cout<<"专辑名:"<<abs->musicArray[i].m_album<<"\t";
            cout<<"时长:"<<abs->musicArray[i].m_time<<"\t";
            cout<<"出版日期:"<<abs->musicArray[i].m_publish<<endl;
        }  
    }
    clear();
}

//删除歌曲函数
//判断歌曲是否在库中
int exist(music_bank *abs,string name)
{
    for (int i = 0; i < abs->m_size; i++)
    {
        if(abs->musicArray[i].m_name == name)
        {
            return i;
        }
    }
    return -1;   
}
//删除歌曲
void delete_music(music_bank *abs)
{
    cout<<"请输入需要删除的歌曲"<<endl;
    string name;
    cin>>name;
    char buf[1024]={0};
    fgets(buf,1024,stdin);     
    int ret=exist(abs,name);

    if (ret != -1)
    {
        for (int i = ret; i <(abs->m_size); i++)
        {
        abs->musicArray[i] = abs->musicArray[i+1];
        abs->m_size--;
        cout<<"删除成功"<<endl;
        }
    }
    else
    {
        cout<<"没有找到您要删除的歌曲"<<endl;
    }
    clear();
}

//查找歌曲
void find_music(music_bank *abs)
{
    cout<<"请输入你想查找的歌曲"<<endl;
    string name;
    cin>>name;
    char buf[1024]={0};
    fgets(buf,1024,stdin); 
    int ret=exist(abs,name);

    if (ret != -1)
    {
        cout<<"歌曲名:"<<abs->musicArray[ret].m_name<<"\t";
        cout<<"歌手名:"<<abs->musicArray[ret].m_singer<<"\t";
        cout<<"专辑名:"<<abs->musicArray[ret].m_album<<"\t";
        cout<<"时长:"<<abs->musicArray[ret].m_time<<"\t";
        cout<<"出版日期:"<<abs->musicArray[ret].m_publish<<endl;
    }
    else
    {
        cout<<"没有找到该歌曲"<<endl;
    }
    clear();
}

//修改歌曲信息
void modify_music(music_bank *abs)
{
    cout<<"请输入需要修改信息的歌曲名"<<endl;
    string name;
    cin>>name;
    char buf[1024]={0};
    fgets(buf,1024,stdin); 
 

    int ret=exist(abs,name);
    if (ret != -1)
    {
        string name;
        cout<<"请输入修改后的歌曲名:"<<endl;
        cin>>name;
        abs->musicArray[ret].m_name=name;

        string singer;
        cout<<"请输入修改后的歌手名:"<<endl;
        cin>>singer;
        abs->musicArray[ret].m_singer=singer;

        string album;
        cout<<"请输入修改后的专辑名:"<<endl;
        cin>>album;
        abs->musicArray[ret].m_album=album;

        string time;
        cout<<"请输入修改后的时长:"<<endl;
        cin>>time;
        abs->musicArray[ret].m_time=time;

        string publish;
        cout<<"请输入修改后的歌曲名:"<<endl;
        cin>>publish;
    char buf[1024]={0};
    fgets(buf,1024,stdin); 
        abs->musicArray[ret].m_publish=publish;
        clear();
    }
    else
    {
        cout<<"没有找到该歌曲的位置"<<endl;
    }
    clear();
}

//清空本地歌曲信息库
void clear_music(music_bank *abs)
{
    abs->m_size=0;
    cout<<"本地歌曲库已清空"<<endl;
}


int main()
{

music_bank abs;
abs.m_size = 0;

while (1)
{
   showMenu();

    //用户选择
    int select = 0;
    cin >> select;
    char buf[1024]={0};
    fgets(buf,1024,stdin); 

    switch (select)
    {

         case 1://添加歌曲
            add_music(&abs);
         break;

         case 2://显示歌曲
            show_music(&abs);
         break;

         case 3://删除歌曲
            delete_music(&abs);
         break;

         case 4://查找歌曲
            find_music(&abs);
         break;

         case 5://修改歌曲信息
            modify_music(&abs);
         break;

         case 6://清空全部歌曲
            clear_music(&abs);
         break;

         case 0://退出应用
            cout << "欢迎下次使用" << endl;
            clear();
            return 0;
         break;
    }
}
    return 0;
}

 

C++信息管理系统练习

标签:bre   while   sys   pac   div   fine   for   出版   mui   

原文地址:https://www.cnblogs.com/hyby/p/13401303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!