//
// 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:@",. \n"]];
NSMutableArray *mulWords = [NSMutableArray array];
for (NSString *item in words) {
if (![item isEqualToString:@""]) {
[mulWords addObject:[item lowercaseString]];
}
}
//统计单词出现的次数, 并添加到字典中
while ([mulWords count]) {
NSString *key = [mulWords objectAtIndex:];
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 =;
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 ;
}

OC7_单词个数的更多相关文章

  1. NOIP2001统计单词个数[序列DP]

    题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包含的单 ...

  2. 洛谷 P1026 统计单词个数 Label:dp

    题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包含的单 ...

  3. 第六章 第一个Linux驱动程序:统计单词个数

    现在进入了实战阶段,使用统计单词个数的实例让我们了解开发和测试Linux驱动程序的完整过程.第一个Linux驱动程序是统计单词个数. 这个Linux驱动程序没有访问硬件,而是利用设备文件作为介质与应用 ...

  4. 第六章第一个linux个程序:统计单词个数

    第六章第一个linux个程序:统计单词个数 从本章就开始激动人心的时刻——实战,去慢慢揭开linux神秘的面纱.本章的实例是统计一片文章或者一段文字中的单词个数.  第 1 步:建立 Linu x 驱 ...

  5. 【wikioi】1040 统计单词个数

    题目链接 算法:划分型DP PS:被卡过3天.日期:2013-10-10 ~ 2013-10-12 18:52:48 这题是我提交了13次AC= =汗= = 题目描述: 给出一个长度不超过200的由小 ...

  6. 统计单词个数及词频(C++实现)

    #include<iostream> #include<fstream> #include<string> using namespace std; struct ...

  7. NOIP200107统计单词个数

    NOIP200107统计单词个数 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给出一个长度不超过200的由 ...

  8. NOIP2001 统计单词个数

    题三 统计单词个数(30分) 问题描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k&l ...

  9. Codevs_1040_[NOIP2001]_统计单词个数_(划分型动态规划)

    描述 http://codevs.cn/problem/1040/ 与Codevs_1017_乘积最大很像,都是划分型dp. 给出一个字符串和几个单词,要求将字符串划分成k段,在每一段中求共有多少单词 ...

随机推荐

  1. Hadoop对小文件的解决方式

    小文件指的是那些size比HDFS的block size(默认64M)小的多的文件.不论什么一个文件,文件夹和block,在HDFS中都会被表示为一个object存储在namenode的内存中, 每一 ...

  2. SpringMVC4.2.4 xml配置

    环境:1.基于spring4.2.4版本,也是spring当前(2016.2)最新的GA版本 2.maven 3.2.1 3.jdk1.7 xml配置1: web.xml <?xml versi ...

  3. c# 将PPT转换成HTML

    这只是一个小程序,就是将ppt转换成html,方法很多,为了以后备用,在此记录一下,也和大家分享 源码如下: using System; using System.Collections.Generi ...

  4. 关于flash player debugger 无法弹窗报错的解决办法

    第一个是IE的插件, Download the Windows Flash Player 10.2 ActiveX control content debugger (for IE) (EXE, 2. ...

  5. oc-08-内存分析

    说有对象公用类的一个方法

  6. Sales_item例子

    Sales_item.h #ifndef SALES_ITEM_H #define SALES_ITEM_H #include<iostream> #include<string&g ...

  7. jquery.validate.js使用id验证控件

    jquery.validate.js默认的是元素的name. 例如:<input name="username" id="username" size=& ...

  8. 步进循环语句for

    一.for语句的基本格式 for 变量 in 列表 do 语句块 done 二.使用for语句处理列表(数组) [root@localhost shell]# cat use_for_deal_wit ...

  9. c语言_判断例子

    例一: #include "stdio.h" int main() { ; if(i) printf("hi"); if(!i) printf("hi ...

  10. 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "StdAfx.h"”?

    在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加“#include "StdAfx.h"”? 右键选择该文件.cpp格式的->属性->预编译头,→ 不使用预编译 ...