c语言如何随机获取1kb,基于VS2010+C语言实现播放器的顺序播放、随机播放
1.[文件] music.h?~?920B???? 下載(38)
/*
* File: music.h
* Time: 2014/10/11
*/
#ifndef __MUSIC_H__
#define __MUSIC_H__
typedef enum { UNPLAYED, PLAYED } BOOL; // 自定義一個(gè)bool類(lèi)型
typedef enum { ORDER, RANDOM } PLAY_MODEL; // 自定義一個(gè)播放類(lèi)型
typedef char *MUSIC_NAME; // 歌曲名
typedef MUSIC_NAME *MUSIC_NAME_TAB; // 歌曲名表
#define MUSIC_NAME_SIZE 60
typedef struct _music_ {
char str[MUSIC_NAME_SIZE];
BOOL isPlayed; // 是否被播放過(guò)
} MUSIC; // 歌曲 MUSIC == struct _music_
typedef MUSIC *MUSIC_TAB; // 歌曲播放列表 MUSIC_TAB == MUSIC *
void systemInit(void); // 系統(tǒng)初始化,必須在listPlay()之前調(diào)用,且只能調(diào)用一次
void play(const MUSIC_NAME_TAB musNametab, const int musnum, const PLAY_MODEL playModel);
// 按照歌曲名表musNametab播放歌曲;musnum為歌曲數(shù)量;playModel為播放模式;
// playModel:ORDER(按歌曲名稱(chēng)順序播放) RANDOM(隨機(jī)播放)
#endif
/*
* End of file
*/
2.[文件] music.c?~?4KB???? 下載(33)
/*
* File: music.c
* Time: 2014/10/11
*/
#include "music.h"
#include
#include
#include
#include
#include
static MUSIC_TAB newtab = NULL; // 全局變量,一張經(jīng)過(guò)處理(排序/洗牌)的新的歌曲表單
static void playOneMusic(MUSIC mus); // 播放歌曲mus
static MUSIC_TAB orderList(const MUSIC_TAB oldtab, const int musnum); // 獲得新的有序列表
static MUSIC_TAB randomLis(const MUSIC_TAB oldtab, const int musnum); // 獲得新的隨機(jī)列表
static void listPlay(const MUSIC_TAB mustab, int musnum, const PLAY_MODEL playModel); // 按照新表單播放
//==========================================================================================================
// 系統(tǒng)初始化,必須在listPlay()之前調(diào)用,且只能調(diào)用一次
void systemInit(void) {
srand((unsigned int)time(NULL));
}
// 按照歌曲名表musNametab播放歌曲
void play(const MUSIC_NAME_TAB musNametab, const int musnum, const PLAY_MODEL playModel) {
int i;
newtab = (MUSIC_TAB)malloc(sizeof(MUSIC) * musnum);
if (newtab != NULL) {
for (i = 0; i < musnum; i++) { // 裝載歌曲名信息,并清零已播放標(biāo)志
memset(&(newtab[i].str[0]), 0, sizeof(char)*MUSIC_NAME_SIZE);
strcpy(newtab[i].str, musNametab[i]);
newtab[i].isPlayed = UNPLAYED;
}
listPlay(newtab, musnum, playModel);
} else {
fprintf(stderr,"\n Program Running Wrong!\n");
return;
}
}
//==========================================================================================================
static void listPlay(const MUSIC_TAB mustab, int musnum, const PLAY_MODEL playModel) { // 按照新表單播放
//以下注釋部分是最初的錯(cuò)誤代碼
//if (playModel == ORDER) {
//newtab = orderList(mustab, musnum); // 獲得新的有序列表
//} else {
//newtab = randomLis(mustab, musnum); // 獲得新的隨機(jī)列表
//}
//while(--musnum >= 0) {
//playOneMusic(*newtab++);// 播放某一歌曲
//}
//free(newtab);
//以下部分是調(diào)試之后改正的代碼 ========================================
MUSIC_TAB tmp = NULL;
if (playModel == ORDER) {
newtab = orderList(mustab, musnum); // 獲得新的有序列表
} else {
newtab = randomLis(mustab, musnum); // 獲得新的隨機(jī)列表
}
tmp = newtab;
while(--musnum >= 0) {
playOneMusic(*tmp++);// 播放某一歌曲
}
if (newtab != NULL) {
free(newtab);
}
}
static void playOneMusic(MUSIC mus) {
if (mus.isPlayed == UNPLAYED) { // 如果歌曲mus在當(dāng)前循環(huán)尚未播放過(guò)
if (mus.str != NULL) {
if (strlen(mus.str) <= 0) { // 歌曲名為空字符串
fprintf(stderr,"\n The file of the song \"%s\" is empty !!!\n");
} else {
printf("\n Start playing the song: "); // 模擬歌曲播放
puts(mus.str);
printf(" ");
putchar('.'); Sleep(500); putchar('.'); Sleep(500); putchar('.');
printf("\n Stop playing the song.\n ");
Sleep(500);
}
} else { // 歌曲名為空
fprintf(stderr,"\n Unfound the song \"%s\" !!!\n", mus.str);
}
}
mus.isPlayed = PLAYED;
}
static MUSIC_TAB orderList(const MUSIC_TAB oldtab, const int musnum) { // 獲得新的有序列表
if (newtab != NULL) { // newtab為全局變量
int i,j;
MUSIC temp;
memcpy(newtab, oldtab, sizeof(MUSIC)*musnum);
// 將newtab[0]~newtab[musnum-1]排序
for (i = 0; i < musnum-1; i++) {
for (j = i+1; j < musnum; j++) {
if (strcmp(newtab[i].str, newtab[j].str) > 0) { // newtab[i]->str > newtab[j]->str
temp = newtab[i];
newtab[i] = newtab[j];
newtab[j] = temp;
}
}
}
return newtab;
} // @ if (newtab != NULL)
// 如果newtab==NULL則不作任何處理,統(tǒng)一由listPlay()處理
}
static int myRand(int len) { //生成一個(gè)[0,len-1]之間的隨機(jī)數(shù)
int iRand;
iRand = rand()%len; // 這就是為什么需要systemInit()的原因
return iRand;
}
#define SWAP(a,b,_t) do { _t t = a; a = b; b = t; } while(0)
static void shuffle(const MUSIC_TAB table, const int musnum) { // 洗牌
int n;
// 將table[0]~table[musnum-1]洗牌
// 具體算法:http://bbs.bccn.net/thread-331122-1-1.html
for (n = musnum; n > 1; --n) {
int i = myRand(n);
SWAP(table[n-1], table[i], MUSIC);
}
}
static MUSIC_TAB randomLis(const MUSIC_TAB oldtab, const int musnum) { // 獲得新的隨機(jī)列表
if (newtab != NULL) { // newtab為全局變量
memcpy(newtab, oldtab, sizeof(MUSIC)*musnum);
shuffle(newtab, musnum);
return newtab;
} // @ if (newtab != NULL)
// 如果newtab==NULL則不作任何處理,統(tǒng)一由listPlay()處理
}
/*
* End of file
*/
3.[文件] playmusic.c?~?1KB???? 下載(30)
/*
* File: playmusic.c
* Time: 2014/10/11
*/
#include "music.h"
#include
// �?���?��5��������?�����4��?�?���
// �?������?�http://music.baidu.com/explore/������?�?���?fm=altg8
#define SONGS_LEN 6
char *Songs[SONGS_LEN] = {
// strlen(������) <= MUSIC_NAME_SIZE ��@ music.h��
// �?�������= NULL
"Moves Like Jagger",
"Love Story",
"Unforgivable Sinner",
"Bad Romance",
"Father And Son",
"Wonderful",
};
int main(void) {
systemInit();
puts("\n Initialize system....");
puts("\n ==== ORDER ==== ");
play(Songs, SONGS_LEN, ORDER);
puts("\n ==== RANDOM ==== ");
play(Songs, SONGS_LEN, RANDOM);
puts("\n ==== ORDER ==== ");
play(Songs, SONGS_LEN, ORDER);
puts("\n ==== RANDOM ==== ");
play(Songs, SONGS_LEN, RANDOM);
puts("\n ==== ORDER ==== ");
play(Songs, SONGS_LEN, ORDER);
puts("\n ==== RANDOM ==== ");
play(Songs, SONGS_LEN, RANDOM);
puts("\n Exit system....\n");
return 0;
}
/*
* End of file
*/
總結(jié)
以上是生活随笔為你收集整理的c语言如何随机获取1kb,基于VS2010+C语言实现播放器的顺序播放、随机播放的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 同花顺双向交易快捷键(同花顺交易快捷键列
- 下一篇: 女真族是现在哪个民族(金国和清朝是一个民