//
// 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. Spring强制使用CGLIB代理事务

    Spring强制使用CGLIB代理事务   springaopjdkreferenceclasspath Spring1.2: 将事务代理工厂[TransactionProxyFactoryBean] ...

  2. Codeforces Round #280 (Div. 2) C. Vanya and Exams 贪心

    C. Vanya and Exams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...

  3. Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子

    Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...

  4. 04.URL路径访问与模块控制器之间的关系

    <?php //初使化,进行加载. //通过这个英文名来了解,他是定义的与thinkphp有关的核心框架文件目录路径 //他可以通过这一个常量,在以后运行的时候都去找这个路径,确保在运行过程当, ...

  5. android152 笔记 2

    27.谈谈UI中, Padding和Margin有什么区别? Padding 文字对边框, margin是控件对父窗体. 28. widget相对位置的完成在activity的哪个生命周期阶段实现.控 ...

  6. LINUX 内核导论

    http://blog.csdn.net/ljy1988123/article/category/1490573/2

  7. placement new 操作符

    placement new操作符能够在分配内存时指定内存位置.下面的程序使用了placement new操作符和常规new操作符给对象分配内存. // placenew.cpp -- new, pla ...

  8. c#线程问题(3)

    //执行回调函数 static void Main(string[] args) { UPPER = ; Thread thread1=new Thread(sops); thread1.Name = ...

  9. cocos2d-x使用tinyxml2存储解析xml

    我用的是2.1.4的cocos2d-x,里面自带有tinyxml2库. 导入头文件:#include "support/tinyxml2/tinyxml2.h" using nam ...

  10. Monolog - Logging for PHP 5.3+

    Monolog 是PHP的一个日志类库.相比于其他的日志类库,它有以下的特点: 功能强大.可以把日志发送到文件.socket.邮箱.数据库和各种web services. 遵循 PSR3 的接口规范. ...