OC1_汉字拼音转换 练习
// // WordManager.h // OC1_汉字拼音转换 // // Created by zhangxueming on 15/4/27. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "WordItem.h" #define FILEPATH @"/Users/qianfeng/Downloads/Day15_复合类设计与字典/汉字拼音转换/汉字-拼音.txt" @interface WordManager : NSObject { NSString *_name; NSMutableArray *_wordList; } - (id)initWithName:(NSString *)name andFile:(NSString *)path; - (void)parseWordFile:(NSString *)path; - (NSString *)searchCharactersByWord:(NSString *)aWord options:(ToneMask)mask; + (void)userInterface; @end
// // WordManager.m // OC1_汉字拼音转换 // // Created by zhangxueming on 15/4/27. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "WordManager.h" @implementation WordManager - (id)initWithName:(NSString *)name andFile:(NSString *)path { if (self = [super init]) { _name = name; _wordList = [NSMutableArray array]; [self parseWordFile:path]; } return self; } - (void)parseWordFile:(NSString *)path { //1.读取文件 NSString *fileContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; if (!fileContent) {//判断文件是否读取成功 return; } //2.分割字符串 NSArray *fileArray = [fileContent componentsSeparatedByString:@"\n"]; NSLog(@"%@",fileArray[0]); //3.遍历数组解析 for (NSString *list in fileArray) { if ([list compare:@""]==NSOrderedSame) { continue; } WordItem *item = [[WordItem alloc] init]; NSArray *tempArray = [list componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"[]"]]; //NSLog(@"%@",tempArray[0]); [item setWord:[tempArray objectAtIndex:0] andCharacters:[tempArray objectAtIndex:1]]; [self->_wordList addObject:item]; // NSLog(@"%@",_wordList); } } - (NSString *)searchCharactersByWord:(NSString *)aWord options:(ToneMask)mask { for (WordItem *item in _wordList) { NSRange range = [[item word] rangeOfString:aWord]; if (range.location != NSNotFound) { return [item parseCharacters:mask]; } } return nil; } + (void)userInterface { WordManager *wordManager=[[WordManager alloc] initWithName:@"汉字拼音-转换器" andFile:FILEPATH]; // for (WordItem *item in wordManager->_wordList) { // [item print]; // } char wordArray[20]={}; scanf("%s", wordArray); NSString *word = [NSString stringWithUTF8String:wordArray] ; NSLog(@"%@", [wordManager searchCharactersByWord:word options:NoneTone]); // WordItem *it=[[WordItem alloc] init]; // [it print]; } @end
// // WordItem.h // OC1_汉字拼音转换 // // Created by zhangxueming on 15/4/27. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> typedef enum { ToneUpside=1L, ToneBack, NoneTone }ToneMask; @interface WordItem : NSObject { NSString *_word; NSString *_characters; } - (id)init; - (void)setWord:(NSString *)aWord andCharacters:(NSString *)aCharacters; - (NSString *)word; - (NSString *)characters; - (NSString *)parseCharacters:(ToneMask)option; - (void)print; @end
// // WordItem.m // OC1_汉字拼音转换 // // Created by zhangxueming on 15/4/27. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "WordItem.h" @implementation WordItem - (id)init { if (self = [super init]) { } return self; } - (void)setWord:(NSString *)aWord andCharacters:(NSString *)aCharacters { _word = aWord; _characters = aCharacters; } - (NSString *)word { return _word; } - (NSString *)characters { return _characters; } - (NSString *)parseCharacters:(ToneMask)option { NSArray *wordArray = [_characters componentsSeparatedByString:@","]; switch (option) { case ToneUpside: return [wordArray objectAtIndex:0]; case ToneBack: return [wordArray objectAtIndex:1]; case NoneTone: { NSString *str = [wordArray objectAtIndex:1]; NSInteger len = [str length]; unichar ch = [str characterAtIndex:len-1]; if (isdigit(ch)) { return [str substringToIndex:len-1]; } return str; } } return nil; } - (void)print { NSLog(@"汉字: %@ 拼音: %@", [self word], [self characters]); } @end
// // main.m // OC1_汉字拼音转换 // // Created by zhangxueming on 15/4/27. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "WordManager.h" int main(int argc, const char * argv[]) { @autoreleasepool { [WordManager userInterface]; } return 0; }