C语言实现一个简单的点歌系统

创建一个简单的点歌系统可以用C语言实现,这里提供一个基本的框架。这个系统可以包括歌曲列表、用户选择歌曲的功能以及播放歌曲的功能。以下是一个示例代码:

c 复制代码
#include <stdio.h>
#include <string.h>

#define MAX_SONGS 100
#define MAX_LENGTH 100

typedef struct {
    char name[MAX_LENGTH];
    char artist[MAX_LENGTH];
} Song;

typedef struct {
    Song songs[MAX_SONGS];
    int count;
} Playlist;

// 函数声明
void addSong(Playlist *playlist, const char *name, const char *artist);
void listSongs(const Playlist *playlist);
int chooseSong(const Playlist *playlist);

int main() {
    Playlist playlist = { .count = 0 };
    
    // 添加一些示例歌曲
    addSong(&playlist, "Yesterday", "The Beatles");
    addSong(&playlist, "Bohemian Rhapsody", "Queen");
    addSong(&playlist, "Hotel California", "Eagles");

    int choice;
    do {
        listSongs(&playlist);
        printf("请输入您想要听的歌曲编号(输入-1退出): ");
        scanf("%d", &choice);
        if (choice != -1) {
            playSong(&playlist, choice);
        }
    } while (choice != -1);

    return 0;
}

void addSong(Playlist *playlist, const char *name, const char *artist) {
    if (playlist->count < MAX_SONGS) {
        strcpy(playlist->songs[playlist->count].name, name);
        strcpy(playlist->songs[playlist->count].artist, artist);
        playlist->count++;
    } else {
        printf("播放列表已满。\n");
    }
}

void listSongs(const Playlist *playlist) {
    printf("播放列表:\n");
    for (int i = 0; i < playlist->count; i++) {
        printf("%d. %s - %s\n", i + 1, playlist->songs[i].name, playlist->songs[i].artist);
    }
}

int chooseSong(const Playlist *playlist) {
    int choice;
    printf("请选择歌曲编号: ");
    scanf("%d", &choice);
    return choice - 1;
}

void playSong(Playlist *playlist, int index) {
    if (index >= 0 && index < playlist->count) {
        printf("正在播放: %s - %s\n", playlist->songs[index].name, playlist->songs[index].artist);
    } else {
        printf("无效的选择。\n");
    }
}

这个程序定义了一个Playlist结构体来存储歌曲列表,并定义了Song结构体来保存每首歌的名字和艺术家名字。addSong函数用于添加新歌到播放列表,listSongs用于列出所有歌曲,chooseSong让用户选择歌曲,而playSong则根据用户的选择"播放"歌曲(在这里只是输出歌曲的信息)。

请注意,这只是一个非常基础的实现,实际应用中还需要考虑错误处理、输入验证、更复杂的用户界面等。此外,为了使程序更加完整,还可以添加删除歌曲、修改歌曲信息等功能。如果要保存歌曲信息,可以考虑使用文件操作来读写数据。

相关推荐
丁劲犇1 分钟前
用 Turbo Vision 2 为 Qt 6 控制台应用创建 TUI 字符 MainFrame
开发语言·c++·qt·tui·字符界面·curse
旷世奇才李先生14 分钟前
Next.js 安装使用教程
开发语言·javascript·ecmascript
charlie1145141911 小时前
深入理解Qt的SetWindowsFlags函数
开发语言·c++·qt·原理分析
呜喵王阿尔萨斯1 小时前
编程中的英语
c语言·c++
likeGhee2 小时前
python缓存装饰器实现方案
开发语言·python·缓存
whoarethenext2 小时前
使用 C++/Faiss 加速海量 MFCC 特征的相似性搜索
开发语言·c++·faiss
项目題供诗2 小时前
黑马python(二十五)
开发语言·python
only-lucky2 小时前
C语言socket编程-补充
服务器·c语言·php
慌糖2 小时前
RabbitMQ:消息队列的轻量级王者
开发语言·javascript·ecmascript
醇醛酸醚酮酯2 小时前
Qt项目锻炼——TODO清单(二)
开发语言·数据库·qt