英汉字典

MyDictionary.h
 
  
#import 

@interface MyDictionary : NSObject


@property (nonatomic, retain)NSString * engWord;
@property(nonatomic, retain)NSString * chiMean;

@end
MyDictionary.m
#import "MyDictionary.h"

@implementation MyDictionary

@end

MyTranslator.h

#import 
#import "MyDictionary.h"

@interface MyTranslator : NSObject

@property (nonatomic, retain) NSMutableDictionary * dicTranslator;


-(void)storeDic:(NSString *)filePath;
-(void)wordInputAndTranslate;
-(void)printMena:(char *)word;
-(id)init;

@end
MyTranslator.m

#import "MyTranslator.h"


@implementation MyTranslator


//读取文件并按换行符切割保存
-(NSArray *)readFile:(NSString *)filePath{
    //从文件中读取文件内容保存到NSString对象fileContent中
    NSString * fileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    //将fileContent中字符串按换行符”\n“切割并保存到数组中
    NSArray * array = [fileContent componentsSeparatedByString:@"\n"];
    
    return array;
}


//将单词包括中文翻译保存到字典中
-(void)storeDic:(NSString *)filePath{
    
    NSArray * arrayFileContent = [self readFile:filePath];
    for(int i = 0; i < [arrayFileContent count]; i += 2)
    {
        //数组arrayFileContent中偶数下标内容是单词,奇数下标内容是中文翻译
        if (arrayFileContent[i] != nil && arrayFileContent[i+1] != nil) {
            NSString * word = [self analyWord:arrayFileContent[i]];
            NSString * mean = [self analyMean:arrayFileContent[i+1]];
            //将两者保存到MyDictionary类的对象dic中
            MyDictionary * dic = [[MyDictionary alloc] init];
            dic.engWord = word;
            dic.chiMean = mean;
            //将每个dic对象放入字典中,以单词为Key
            [_dicTranslator setObject:dic forKey:word];
        }
    }
}


//解析单词
-(NSString *)analyWord:(NSString *)str{
    //将字符串按“#”进行切割
    NSArray * array = [str componentsSeparatedByString:@"#"];
   
    //切割后数组中只有两个元素,下标为1的元素是单词
    return array[1];
}

//解析中文翻译
-(NSString *)analyMean:(NSString *)str{
    //将字符串按“:”进行切割
    NSArray * array = [str componentsSeparatedByString:@":"];
    
    //切割后数组中只有两个元素,下标为1的元素是中文翻译
    return array[1];
}

//打印中文翻译
-(void)printMena:(char *)word{
    NSString * str = [NSString stringWithFormat:@"%s", word];
    //将输入的单词转为小写,支持不区分大小写
    NSString * str1 = [str lowercaseString];
    if ([_dicTranslator objectForKey:str1] != nil) {
        NSLog(@"%@", [[_dicTranslator objectForKey:str1] chiMean]);
    }else{
        NSLog(@"找不到该单词!");
    }
}

//单词输入并翻译
-(void)wordInputAndTranslate{
    
    while(1)
    {
        NSLog(@"=================================");
        NSLog(@"=======欢迎使用小伍的英汉字典========");
        NSLog(@"=================================");
        NSLog(@"===请输入要查询的单词(输入“0”退出)====");
        
        char word[20] = "\n";
        scanf("%[^\n]", word);
        getchar();
        
        //判断输入的是否”0“,是的话则退出
        if ([@"0" isEqualToString:[NSString stringWithFormat:@"%s", word]]) {
            break;
        }
        [self printMena: word];
    }
}


-(id)init{
    if (self = [super init]) {
        _dicTranslator = [[NSMutableDictionary alloc] init];
    }
    return self;
}

@end
main.m

#import 
#import "MyTranslator.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        MyTranslator * translator = [[MyTranslator alloc] init];
        [translator storeDic:@"这里写入英汉字典文件的路径即可"];
        [translator wordInputAndTranslate];
    }
    return 0;
}
字典内容格式如下:

#a
Trans:art. 一;字母A
#a.m.
Trans:n. 上午
#a/c
Trans:n. 往来帐户@往来:come - and - go; contact; intercourse@n. 往来帐户
#aardvark
Trans:n. 土猪
#aardwolf
Trans:n. 土狼
#aasvogel
Trans:n. 秃鹰之一种
#abaci
Trans:n. 算盘
#aback
Trans:ad. 向后地;朝后地
#abacus
Trans:n. 算盘
#abaft
Trans:ad. 向船尾@prep. 在...后
#abalone
Trans:n. 鲍鱼
#abandon
Trans:vt. 放弃;沉溺@n. 放任
#abandoned
Trans:a. 被抛弃的;自弃的;自甘堕落的
#abandonee
Trans:n. 被遗弃者;被委付者
#abandoner
Trans:n. 遗弃者;委付者
由于字典里单词比较多,就不全贴出来,以上的内容可以作为参考,自己新建一个.txt文件,将以上字典内容粘贴进去测试。




你可能感兴趣的:(英汉字典)