马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
创建一个简单的点歌体系可以用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则根据用户的选择“播放”歌曲(在这里只是输出歌曲的信息)。
请注意,这只是一个非常底子的实现,实际应用中还需要考虑错误处理惩罚、输入验证、更复杂的用户界面等。此外,为了使程序更加完整,还可以添加删除歌曲、修改歌曲信息等功能。假如要保存歌曲信息,可以考虑利用文件利用来读写数据。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |