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则根据用户的选择"播放"歌曲(在这里只是输出歌曲的信息)。

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

相关推荐
古城小栈15 小时前
Rust 学习路线,供初学者参考
开发语言·rust
喵了几个咪16 小时前
GoWind CMS 拆解:给 NL2SQL 上四把锁——智能问数的安全边界
开发语言·后端·安全·微服务·golang·gowind
多弗朗皮卡丘16 小时前
C++的类型转换
开发语言·c++
FLJwu16 小时前
运动相机图像传感器选型实战|第二篇:参数看着一模一样,为什么骑行录像疯狂果冻、拖影翻车?
android·c语言·开发语言·数码相机·运动相机·oem 量产
驭渊的小故事16 小时前
从 JVM 底层看泛型:泛型擦除、Signature 属性与 Super Type Token 模式
java·开发语言·jvm
Escalating_xu16 小时前
【C语言项目实战】贪吃蛇(下):移动、碰撞与计分,完成一局可玩的游戏
c语言·开发语言·游戏
qq_4017004117 小时前
Qt Modbus 源码剖析:从 QModbusClient 到工业级通信框架
开发语言·qt·系统架构
星夜离尘5617 小时前
MATLAB 角度解缠遇到丢样:先分段,再说明圈数为什么不可恢复
开发语言·matlab
我命由我1234517 小时前
6-2.Python 异常处理 - 异常捕获(捕获指定异常、常见异常类型、异常信息)
java·运维·开发语言·python·http·https·java-ee
ZoeLandia17 小时前
React08:自定义 Hook
开发语言·前端·react.js·typescript