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

打印 上一主题 下一主题

主题 1734|帖子 1734|积分 5202

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
创建一个简单的点歌体系可以用C语言实现,这里提供一个根本的框架。这个体系可以包罗歌曲列表、用户选择歌曲的功能以及播放歌曲的功能。以下是一个示例代码:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SONGS 100
  4. #define MAX_LENGTH 100
  5. typedef struct {
  6.     char name[MAX_LENGTH];
  7.     char artist[MAX_LENGTH];
  8. } Song;
  9. typedef struct {
  10.     Song songs[MAX_SONGS];
  11.     int count;
  12. } Playlist;
  13. // 函数声明
  14. void addSong(Playlist *playlist, const char *name, const char *artist);
  15. void listSongs(const Playlist *playlist);
  16. int chooseSong(const Playlist *playlist);
  17. int main() {
  18.     Playlist playlist = { .count = 0 };
  19.    
  20.     // 添加一些示例歌曲
  21.     addSong(&playlist, "Yesterday", "The Beatles");
  22.     addSong(&playlist, "Bohemian Rhapsody", "Queen");
  23.     addSong(&playlist, "Hotel California", "Eagles");
  24.     int choice;
  25.     do {
  26.         listSongs(&playlist);
  27.         printf("请输入您想要听的歌曲编号(输入-1退出): ");
  28.         scanf("%d", &choice);
  29.         if (choice != -1) {
  30.             playSong(&playlist, choice);
  31.         }
  32.     } while (choice != -1);
  33.     return 0;
  34. }
  35. void addSong(Playlist *playlist, const char *name, const char *artist) {
  36.     if (playlist->count < MAX_SONGS) {
  37.         strcpy(playlist->songs[playlist->count].name, name);
  38.         strcpy(playlist->songs[playlist->count].artist, artist);
  39.         playlist->count++;
  40.     } else {
  41.         printf("播放列表已满。\n");
  42.     }
  43. }
  44. void listSongs(const Playlist *playlist) {
  45.     printf("播放列表:\n");
  46.     for (int i = 0; i < playlist->count; i++) {
  47.         printf("%d. %s - %s\n", i + 1, playlist->songs[i].name, playlist->songs[i].artist);
  48.     }
  49. }
  50. int chooseSong(const Playlist *playlist) {
  51.     int choice;
  52.     printf("请选择歌曲编号: ");
  53.     scanf("%d", &choice);
  54.     return choice - 1;
  55. }
  56. void playSong(Playlist *playlist, int index) {
  57.     if (index >= 0 && index < playlist->count) {
  58.         printf("正在播放: %s - %s\n", playlist->songs[index].name, playlist->songs[index].artist);
  59.     } else {
  60.         printf("无效的选择。\n");
  61.     }
  62. }
复制代码
这个程序定义了一个Playlist结构体来存储歌曲列表,并定义了Song结构体来保存每首歌的名字和艺术家名字。addSong函数用于添加新歌到播放列表,listSongs用于列出全部歌曲,chooseSong让用户选择歌曲,而playSong则根据用户的选择“播放”歌曲(在这里只是输出歌曲的信息)。
请注意,这只是一个非常底子的实现,实际应用中还需要考虑错误处理惩罚、输入验证、更复杂的用户界面等。此外,为了使程序更加完整,还可以添加删除歌曲、修改歌曲信息等功能。假如要保存歌曲信息,可以考虑利用文件利用来读写数据。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊落一身雪

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表