一、功能设计
(1) 每个文件内容包括音乐编号、歌名、专辑、歌手、作曲、作词,文件路径;
(2) 建立适当的查找表以提高查找效率;
(3)实现查找表的插入删除操作;
(4) 设计高效的查找算法实现高效歌曲检索
二、实现代码
/*
* Music File Manager —— 哈希索引 + 文本 CSV 持久化
* gcc music_mgr.c -o music_mgr
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef _WIN32
#include <windows.h>
#define CLEAR "cls"
#else
#define CLEAR "clear"
#endif
#define HASH_SIZE 2048 /* 哈希桶数,2 的幂次 */
#define MAX_LEN 256
#define CSV_FILE "music.csv"
typedef struct Music {
int id; /* 音乐编号 主键 */
char title [MAX_LEN]; /* 歌名 */
char album [MAX_LEN];
char artist [MAX_LEN];
char composer [MAX_LEN];
char lyricist [MAX_LEN];
char path [MAX_LEN]; /* 绝对文件路径 */
struct Music *next_id; /* 编号哈希链 */
struct Music *next_title; /* 歌名哈希链 */
struct Music *next_artist; /* 歌手哈希链 */
} Music;
/* 三个哈希表头 */
static Music *hash_id[HASH_SIZE];
static Music *hash_title[HASH_SIZE];
static Music *hash_artist[HASH_SIZE];
/* ====================== 哈希函数 ====================== */
static unsigned int hash_int(int key) {
return (unsigned)key & (HASH_SIZE - 1);
}
static unsigned int hash_str(const char *s) {
unsigned h = 0;
while (*s) h = h * 31 + tolower(*s++);
return h & (HASH_SIZE - 1);
}
/* ====================== 工具函数 ====================== */
void pause_key() {
puts("\n按回车键继续...");
getchar();
}
char *trim_newline(char *s) {
s

最低0.47元/天 解锁文章
8268

被折叠的 条评论
为什么被折叠?



