OC3_歌词解析

//
//  LrcManager.h
//  OC3_歌词解析
//
//  Created by zhangxueming on 15/6/15.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "LrcItem.h"

#define FilePath @"/Users/zhangxueming/Desktop/ios1509/Day16_类的复合设计/OC3_歌词解析/传奇.txt"

@interface LrcManager : NSObject

- (id)initWithFile:(NSString *)path;

+ (NSString *)userInterface:(float)aTime;

@end
//
//  LrcManager.m
//  OC3_歌词解析
//
//  Created by zhangxueming on 15/6/15.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "LrcManager.h"

//匿名类别(类扩展)
//能扩展方法与实例变量,实现了实例变量与方法的私有化
@interface LrcManager ()
{
    NSString *_title; // 歌曲名
    NSString *_author;// 歌词作者
    NSMutableArray *_lrcList;//存lrcItem
}

//解析歌词文件
- (BOOL)parseLrcFile:(NSString *)path;
//解析歌词标题
- (void)parseLrcTitle:(NSString *)lrcString;
//解析歌词演唱者
- (void)parseLrcAuthor:(NSString *)lrcString;
//解析歌词词条
- (void)parseLrcItem:(NSString *)lrcString;
//将时间字符串转换成浮点时间
- (float)timeByTimeString:(NSString *)aString;

@end


@implementation LrcManager

- (id)initWithFile:(NSString *)path
{
    self = [super init];
    if (self) {
        _lrcList = [NSMutableArray array];
        [self parseLrcFile:path];
    }
    return self;
}


- (BOOL)parseLrcFile:(NSString *)path
{
    //读取歌词文件
    NSString *fileContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    if (!fileContent) {
        return NO;
    }
    //分割歌词字符串
    NSArray *lrcArray = [fileContent componentsSeparatedByString:@"\n"];
    
    for (NSString *lrcString in lrcArray) {
        if ([lrcString isEqualToString:@""]) {
            continue;
        }
        else if ([lrcString hasPrefix:@"[ti:"]) {
            [self parseLrcTitle:lrcString];
        }
        else if ([lrcString hasPrefix:@"[ar:"])
        {
            [self parseLrcAuthor:lrcString];
        }
        else
        {
            [self parseLrcItem:lrcString];
        }
    }
    //对可变数组排序
    [_lrcList sortUsingSelector:@selector(isSortByTime:)];
    return YES;
}

- (void)parseLrcTitle:(NSString *)lrcString
{
    _title = [lrcString substringWithRange:NSMakeRange(4, [lrcString length]-5)];
}

- (void)parseLrcAuthor:(NSString *)lrcString
{
    _author = [lrcString substringWithRange:NSMakeRange(4, [lrcString length]-5)];
}
//02:39.90
- (float)timeByTimeString:(NSString *)aString
{
    NSArray *timeArray = [aString componentsSeparatedByString:@":"];
    return [[timeArray objectAtIndex:0] intValue]*60+[[timeArray objectAtIndex:1] floatValue];
}

- (void)parseLrcItem:(NSString *)lrcString
{
    NSArray *itemArray = [lrcString componentsSeparatedByString:@"]"];
    NSInteger len = [itemArray count];
    for (NSInteger i=0; i<len-1; i++)
    {
        LrcItem  *item = [[LrcItem alloc] init];
        NSString *timeString = [[itemArray objectAtIndex:i] substringFromIndex:1];
        [item setTime:[self timeByTimeString:timeString]];
        [item setLrc:[itemArray lastObject]];
        [_lrcList addObject:item];
    }
}
//3.5 5.5 8.5  10.5   12.0
+ (NSString *)userInterface:(float)aTime
{
    LrcManager *manager = [[LrcManager alloc] initWithFile:FilePath];
    for (LrcItem *item in manager->_lrcList) {
        [item printLrcItem];
    }
    NSInteger len = [manager->_lrcList count];
    NSInteger i;
    for (i=1; i<len; i++) {
        LrcItem *item = [manager->_lrcList objectAtIndex:i];
        if (aTime < [item time]) {
            return [[manager->_lrcList objectAtIndex:i-1] lrc];
        }
    }
    if (i==len) {
        return [[manager->_lrcList objectAtIndex:i-1] lrc];
    }
    return nil;
}
@end
//
//  LrcItem.h
//  OC3_歌词解析
//
//  Created by zhangxueming on 15/6/15.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface LrcItem : NSObject
{
    float    _time;
    NSString *_lrc;
}

- (float)time;
- (NSString *)lrc;

- (void)setTime:(float)aTime;
- (void)setLrc:(NSString *)aLrc;

- (BOOL)isSortByTime:(LrcItem *)aItem;

- (void)printLrcItem;

@end
//
//  LrcItem.m
//  OC3_歌词解析
//
//  Created by zhangxueming on 15/6/15.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "LrcItem.h"

@implementation LrcItem

- (float)time
{
    return _time;
}

- (NSString *)lrc
{
    return _lrc;
}

- (void)setTime:(float)aTime
{
    _time = aTime;
}

- (void)setLrc:(NSString *)aLrc
{
    _lrc = aLrc;
}

- (BOOL)isSortByTime:(LrcItem *)aItem
{
    if ([self time] > [aItem time]) {
        return YES;
    }
    return NO;
}

- (void)printLrcItem
{
    NSLog(@"time = %.2f lrc = %@", [self time], [self lrc]);
}

@end
//
//  main.m
//  OC3_歌词解析
//
//  Created by zhangxueming on 15/6/15.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "LrcManager.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        float time;
        scanf("%f",&time);
        NSLog(@"%@", [LrcManager userInterface:time]);
    }
    return 0;
}

 

posted @ 2015-06-15 17:54  sirzhang  阅读(254)  评论(1编辑  收藏  举报
AmazingCounters.com