当前位置: 首页 > article >正文

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

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


http://www.kler.cn/news/304231.html

相关文章:

  • XSS和sql注入部分场景测试用例样例
  • 将复杂类型列展开成多行,附带json解析
  • pandas 将多条记录整合成一条记录,每条记录的year和month字段组成新的字段名
  • MySQL从C盘迁移到D盘
  • Git的学习笔记
  • 服务器与个人计算机之间的区别
  • Java项目: 基于SpringBoot+mybatis+maven课程答疑系统(含源码+数据库+毕业论文)
  • 【Ubuntu】Ubuntu双网卡配置 实现内外网互不影响同时可用
  • KubeCon China 回顾|快手的 100% 资源利用率提升:从裸机迁移大规模 Redis 到 Kubernetes
  • 深度学习--对抗生成网络(GAN, Generative Adversarial Network)
  • Pr 入门系列之三:挑选与添加媒体到序列(上)
  • UQpy | 不确定性量化Python工具箱推荐
  • Spring和MyBatis常见面试题总结
  • 房屋租赁|基于springboot的房屋租赁管理系统设计与实现(附项目源码+论文+数据库)
  • python-游戏自动化(一)(实战-自动刷视频点赞)
  • activiti第五步流程图定义会审并设置串行用户任务
  • 在RabbitMQ中四种常见的消息路由模式
  • 电能质量监测装置和防孤岛装置在特斯拉工厂分布式光伏项目的应用
  • Node.js Express 框架
  • 6、Flume安装
  • 通信工程学习:什么是PC永久连接、SPC软永久连接
  • Open-Sora代码详细解读(2):时空3D VAE
  • 双流join
  • Vmware 傻瓜式安装( Linux 网络操作系统 01)
  • 【python计算机视觉编程——10.OpenCV】
  • python画图|3D surface基础教程
  • GO Server-Sent Events (SSE)
  • Linux 基础命令-系统信息查看
  • 可测试,可维护,可移植:上位机软件分层设计的重要性
  • 【Python机器学习】循环神经网络(RNN)——审察模型内部情况