• 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:@"
    "];
        
        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;
    }
  • 相关阅读:
    分类问题的评价指标
    29 畅游 x86 分页机制(中)
    高手进阶,终极内存技术指南——完整/进阶版
    从ST官网获取STM32 AD封装库(包含原理图库和PCB库)详细教程
    标准SPI、DUAL SPI、Quad SPI;NorFlash、NandFlash、eMMC闪存的比较与区别
    ARM 之七 主流编译器(armcc、iar、gcc for arm)详细介绍
    STM32中ARM系列编译工具链的编译宏选择(__CC_ARM、__ICCARM__、__GNUC__、__TASKING__)
    每日总结
    每日总结
    每日总结
  • 原文地址:https://www.cnblogs.com/0515offer/p/4578835.html
Copyright © 2020-2023  润新知