zoukankan      html  css  js  c++  java
  • OC7_单词个数

    //
    //  WordManger.h
    //  OC7_单词个数
    //
    //  Created by zhangxueming on 15/6/17.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface WordManger : NSObject
    {
        NSMutableDictionary *_wordList;
    }
    
    - (id)initWithFile:(NSString *)path;
    
    - (void)parseWordFile:(NSString *)path;
    
    - (NSInteger)firstWordCountInArray:(NSString *)word withArray:(NSMutableArray *)mulArray;
    
    + (void)userInterface;
    @end
    //
    //  WordManger.m
    //  OC7_单词个数
    //
    //  Created by zhangxueming on 15/6/17.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "WordManger.h"
    
    @implementation WordManger
    
    - (id)initWithFile:(NSString *)path
    {
        self = [super init];
        if (self) {
            _wordList = [NSMutableDictionary dictionary];
            [self parseWordFile:path];
        }
        return self;
    }
    
    - (void)parseWordFile:(NSString *)path
    {
        //读取单词文件
        NSString *fileContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        if (!fileContent) {
            return;
        }
        //分割字符串
        NSArray *words = [fileContent componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@",. 
    "]];
        NSMutableArray *mulWords = [NSMutableArray array];
        for (NSString *item in words) {
            if (![item isEqualToString:@""]) {
                [mulWords addObject:[item lowercaseString]];
            }
        }
        //统计单词出现的次数, 并添加到字典中
        while ([mulWords count]) {
            NSString *key = [mulWords objectAtIndex:0];
            NSNumber *value = [NSNumber numberWithInteger:[self firstWordCountInArray:key withArray:mulWords]];
            [_wordList setObject:value forKey:key];
            [mulWords removeObject:key];
        }
    }
    
    - (NSInteger)firstWordCountInArray:(NSString *)word withArray:(NSMutableArray *)mulArray
    {
        NSInteger cnt =0;
        for (NSString *item in mulArray) {
            if ([item isEqualToString:word]) {
                cnt++;
            }
        }
        return cnt;
    }
    
    + (void)userInterface
    {
        WordManger *manger = [[WordManger alloc] initWithFile:@"/Users/zhangxueming/Downloads/qfile-10.txt"];
        NSInteger cnt;
        scanf("%li",&cnt);
        NSLog(@"keys = %@", [manger->_wordList allKeysForObject:[NSNumber numberWithInteger:cnt]]);
    }
    @end
    //
    //  main.m
    //  OC7_单词个数
    //
    //  Created by zhangxueming on 15/6/17.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "WordManger.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
           
            [WordManger userInterface];
            
        }
        return 0;
    }
  • 相关阅读:
    Android安全研究经验谈
    论文相关笔记5
    论文相关笔记4
    论文相关笔记3
    论文相关笔记2
    论文相关笔记1
    朝鲜RedStar_OS_3.0安装图解
    Careerup上的简历模板
    冒泡、二分排序
    火狐插件
  • 原文地址:https://www.cnblogs.com/0515offer/p/4584168.html
Copyright © 2011-2022 走看看