NSArray *temp = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", nil];
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:temp];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
int i;
int count = temp.count;
for (i = 0; i < count; i ++) {
int index = arc4random() % (count - i);
[resultArray addObject:[tempArray objectAtIndex:index]];
[tempArray removeObjectAtIndex:index];
}
[tempArray release];
NSLog(@"resultArray is %@",resultArray);
NSLog(@"看看结果楼");
看不懂在下面留言,我会解释
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
[self orderToPlayBackGroundMusic];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self orderToPlayBackGroundMusic];
}
-(void)orderToPlayBackGroundMusic
{
[SingleTonTool defaultMemory].musicIndex++;
if ([SingleTonTool defaultMemory].musicIndex == [[SingleTonTool defaultMemory].musicArray count])
{
[SingleTonTool defaultMemory].musicIndex = 0;
}
NSString * musicName = [[SingleTonTool defaultMemory].musicArray objectAtIndex:[SingleTonTool defaultMemory].musicIndex];
NSString * musicFilePath = [[NSBundle mainBundle] pathForResource:musicName ofType:@"mp3"]; //创建音乐文件路径
NSURL * musicURL= [[NSURL alloc] initFileURLWithPath:musicFilePath];
AVAudioPlayer * thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
thePlayer.delegate = self;
[thePlayer play];
[thePlayer setVolume:1]; //设置音量大小
thePlayer.numberOfLoops = 1;//设置音乐播放次数 -1为一直循环
[musicURL release];
[SingleTonTool defaultMemory].thePlayer = thePlayer;
[[SingleTonTool defaultMemory].playerArray removeAllObjects];
[[SingleTonTool defaultMemory].playerArray addObject:thePlayer];
}
-(void)playBackGroundSound
{
[SingleTonTool defaultMemory].musicArray = [[NSMutableArray alloc] initWithCapacity:0];
[[SingleTonTool defaultMemory].musicArray addObject:@"初音ミク-World Is Mine"];
[[SingleTonTool defaultMemory].musicArray addObject:@"Avril Lavigne-Girlfrieng"];
[[SingleTonTool defaultMemory].musicArray addObject:@"浜崎あゆみ-evolution"];
[[SingleTonTool defaultMemory].musicArray addObject:@"植物大战僵尸 通关歌曲"];
[[SingleTonTool defaultMemory].musicArray addObject:@"初音ミク-炉心融解"];
[[SingleTonTool defaultMemory].musicArray addObject:@"杜岩-千年缘《仙剑奇侠传四》主题曲"];
[[SingleTonTool defaultMemory].musicArray addObject:@"初音ミク-深海少女"];
int k = arc4random()%3;
switch (k)
{
case 0:
[SingleTonTool defaultMemory].musicIndex = 0;
break;
case 1:
[SingleTonTool defaultMemory].musicIndex = 2;
break;
case 2:
[SingleTonTool defaultMemory].musicIndex = 3;
break;
default:
[SingleTonTool defaultMemory].musicIndex = 3;
break;
}
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err)
{
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}
[audioSession setActive:YES error:&err];
err = nil;
if(err)
{
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}
//禁止程序运行时自动锁屏
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
NSString * musicFilePath = [[NSBundle mainBundle] pathForResource:[[SingleTonTool defaultMemory].musicArray objectAtIndex:[SingleTonTool defaultMemory].musicIndex] ofType:@"mp3"]; //创建音乐文件路径
NSURL * musicURL= [[NSURL alloc] initFileURLWithPath:musicFilePath];
AVAudioPlayer * thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
thePlayer.delegate = self;
[thePlayer play];
[thePlayer setVolume:1]; //设置音量大小
thePlayer.numberOfLoops = 1;//设置音乐播放次数 -1为一直循环
[musicURL release];
[[SingleTonTool defaultMemory].playerArray addObject:thePlayer];
[SingleTonTool defaultMemory].thePlayer = thePlayer;
//播一半的歌曲退出后台后停止
/*MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
MPMusicPlaybackState playbackState = musicPlayer.playbackState;
if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {
[musicPlayer play];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
[musicPlayer pause];
}*/
}
一,指针和数组
1) C语言中只有一维数组,而且数组的大小必须在编译期就作为一个常数确定下来
2) 对于一个数组,我们只能够做两件事:确定该数组的大小,以及获得指向该数组下标为0的元素的指针.
sizeof(a) = 12 结果是整个数组a的大小,而不是指向数据a的元素的指针的大小.
#include<stdio.h>int main(){int calendar[12][31];int (*monthp)[31];for(monthp = calendar; monthp < &calendar[12]; ++monthp){int *dayp;for(dayp = *monthp; dayp < &(*monthp)[31]; ++dayp)*dayp = 0;}}
#include<stdio.h>#include<string.h>#include<stdlib.h>int main(){char *r;char *s = "hi";char *t = "man";r = malloc(strlen(s)+strlen(t)+1); //包括最后的'/0',strlen不包括'/0'if(!r){exit(1);}strcpy(r,s);strcat(r,t);printf("%s\n",r);free(r);}
#include<stdio.h>int * bsearch(int *t,int n,int x){int *lo = t,*hi = t + n-1;while(lo < hi){int *mid = lo + ((hi-lo) >> 1);//不能为int *mid = (hi + lo) / 2因为对指针进行加法是错误的,//只能对指针进行迭代即++或--,或相减计算距离if(x < *mid){hi = mid - 1;}else if (x > *mid){lo = mid + 1;}else{return mid;}}return NULL;}int main(){int a[] = {1,2,3,4};printf("%d",*bsearch(a,4,2));}
任何时候已存放的字符数都是bufptr-buffer