OC 通讯录 中等难度

Contact.h

#import <Foundation/Foundation.h>

@interface Contact : NSObject

{

NSString *_name;

NSString *_gender;

NSInteger _age;

NSString *_address;

NSString *_phoneNumber;

NSString *_group;

}

- (void)setName:(NSString *)name;

- (NSString *)name;

- (void)setGender:(NSString *)gender;

- (NSString *)gender;

- (void)setAge:(NSInteger)age;

- (NSInteger)age;

- (void)setAddress:(NSString *)address;

- (NSString *)address;

- (void)setPhoneNumber:(NSString *)phoneNumber;

- (NSString *)phoneNumber;

- (void)setGroup:(NSString *)group;

- (NSString *)group;

- (instancetype)initWithName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age address:(NSString *)address phoneNumber:(NSString *)phoneNumber;

- (void)introduce;

- (NSString *)description;

- (NSComparisonResult)compareByName:(Contact *)contact;

- (NSComparisonResult)compareByAge:(Contact *)contact;

@end

Contact.m

#import "Contact.h"

@implementation Contact

- (void)setName:(NSString *)name {

_name = name;

}

- (NSString *)name {

return _name;

}

- (void)setGender:(NSString *)gender {

_gender = gender;

}

- (NSString *)gender {

return _gender;

}

- (void)setAge:(NSInteger)age {

_age = age;

}

- (NSInteger)age {

return _age;

}

- (void)setAddress:(NSString *)address {

_address = address;

}

- (NSString *)address {

return _address;

}

- (void)setPhoneNumber:(NSString *)phoneNumber {

_phoneNumber = phoneNumber;

}

- (NSString *)phoneNumber {

return _phoneNumber;

}

- (void)setGroup:(NSString *)group {

_group = group;

}

- (NSString *)group {

return _group;

}

- (instancetype)initWithName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age address:(NSString *)address phoneNumber:(NSString *)phoneNumber  {

self = [super init];

if (self) {

[self setName:name];

[self setGender:gender];

[self setAge:age];

[self setAddress:address];

[self setPhoneNumber:phoneNumber];

_group = [_name substringToIndex:1];

}

return self;

}

- (void)introduce {

NSLog(@"%@ %@ %ld %@ %@ %@",_name, _gender, _age, _address, _phoneNumber, _group);

}

- (NSString *)description {

return [[NSString alloc] initWithFormat:@"%@ %@ %ld %@ %@ %@",_name, _gender, _age, _address, _phoneNumber, _group];

}

- (NSComparisonResult)compareByName:(Contact *)contact {

NSString *selfName = [self name];

NSString *contactName = [contact name];

NSComparisonResult result = [selfName compare:contactName];

return result;

}

- (NSComparisonResult)compareByAge:(Contact *)contact {

NSInteger selfAge = [self age];

NSInteger contactAge = [contact age];

if (selfAge > contactAge) {

return NSOrderedAscending;

}

else if (selfAge < contactAge) {

return NSOrderedDescending;

}

else

{

return NSOrderedSame;

}

}

@end

ContactManger.h

#import <Foundation/Foundation.h>

#import "Contact.h"

@interface ContactManager : NSObject

{

NSString *_name;

NSMutableDictionary *_contactList;

}

- (void)setName:(NSString *)name;

- (NSString *)name;

- (instancetype)initWithName:(NSString *)name;

//添加联系人

- (void)addNewContact:(Contact *)contact;

//获取分组下所有的联系人,并且按照姓名升序排列

- (NSArray *)getAllContactsByGroupName:(NSString *)groupName;

//根据号码搜索对应的联系人

- (Contact *)findContactByPhoneNumber:(NSString *)phoneNumber;

//获取所有女性联系人 按照年龄降序排列

- (NSArray *)getAllFemalContactsByGender:(NSString *)gender;

//根据姓名删除某个联系人

- (void)removeContactByName:(NSString *)name;

//删除某个分组下的所有联系人

- (void)removeAllContactsByGroupName:(NSString *)groupName;

//展示所有联系人

- (void)showAllContacts;

@end

ContactManger.m

#import "ContactManager.h"

@implementation ContactManager

- (void)setName:(NSString *)name {

_name = name;

}

- (NSString *)name {

return _name;

}

- (instancetype)initWithName:(NSString *)name {

self = [super init];

if (self) {

[self setName:name];

_contactList = [[NSMutableDictionary alloc] initWithCapacity:0];

}

return self;

}

//添加联系人

- (void)addNewContact:(Contact *)contact {

if (nil == [contact name] || nil == [contact phoneNumber]) {

NSLog(@"你脑子坏了吧?");

} else {

NSMutableArray *array = [_contactList objectForKey:[contact group]];

if (nil == array) {

//要初始化数组

array = [[NSMutableArray alloc] initWithCapacity:0];

[array addObject:contact];

[_contactList setObject:array forKey:[contact group]];

}

else {

[array addObject:contact];

}

}

}

//获取分组下所有的联系人,并且按照姓名升序排列

- (NSArray *)getAllContactsByGroupName:(NSString *)groupName {

NSArray *contacts = [_contactList objectForKey:groupName];

NSArray *newArray = [contacts sortedArrayUsingSelector:@selector(compareByName:)];

return newArray;

}

//根据号码搜索对应的联系人

- (Contact *)findContactByPhoneNumber:(NSString *)phoneNumber {

for (NSString *key in _contactList) {

NSMutableArray *array = [_contactList objectForKey:key];

for (Contact *contact in array) {

if ([phoneNumber isEqualToString:[contact phoneNumber]]) {

return contact;

}

}

}

return nil;

}

//获取所有女性联系人 按照年龄降序排列

- (NSArray *)getAllFemalContactsByGender:(NSString *)gender {

NSMutableArray *femalArray = [[NSMutableArray alloc] initWithCapacity:0];

for (NSString *key in _contactList) {

NSMutableArray *array = [_contactList objectForKey:key];

for (Contact *contact in array) {

if ([gender isEqualToString:[contact gender]]) {

[femalArray addObject:contact];

}

}

}

[femalArray sortUsingSelector:@selector(compareByAge:)];

return femalArray;

}

//根据姓名删除某个联系人

- (void)removeContactByName:(NSString *)name {

Contact *c;

NSString *newKey;

for (NSString *key in _contactList) {

NSMutableArray *array = [_contactList objectForKey:key];

for (Contact *contact in array) {

if ([name isEqualToString:[contact name]]) {

[array removeObject:contact];

return;

//                c = contact;

//                newKey = key;

}

}

}

[[_contactList objectForKey:newKey] removeObject:c];

}

//删除某个分组下的所有联系人

- (void)removeAllContactsByGroupName:(NSString *)groupName {

[_contactList removeObjectForKey:groupName];

}

//展示所有联系人

- (void)showAllContacts {

NSArray *allKeys = [_contactList allKeys];

NSArray *newArray = [allKeys sortedArrayUsingSelector:@selector(compare:)];

for (NSString *key in newArray) {

NSMutableArray *array = [_contactList objectForKey:key];

NSLog(@"-----%@-----",key);

for (Contact *contact in array) {

[contact introduce];

}

}

}

@end

main.m 

#import <Foundation/Foundation.h>

#import "ContactManager.h"

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

ContactManager *cm = [[ContactManager alloc] initWithName:@"SHS150913通讯录(中等难度)"];

Contact *c1 = [[Contact alloc] initWithName:@"wangzhiwen" gender:@"男" age:18 address:@"丽德" phoneNumber:@"15156056193"];

Contact *c2 = [[Contact alloc] initWithName:@"xumin" gender:@"女" age:19 address:@"丽德酒店" phoneNumber:@"15951758095"];

Contact *c3 = [[Contact alloc] initWithName:@"zhanglu" gender:@"女" age:20 address:@"丽德酒店" phoneNumber:@"15951758639"];

Contact *c4 = [[Contact alloc] initWithName:@"xuzizhong" gender:@"男" age:21 address:@"丽德" phoneNumber:@"18862162030"];

Contact *c5 = [[Contact alloc] initWithName:@"wangtao" gender:@"男" age:22 address:@"丽德" phoneNumber:@"18329032011"];

Contact *c6 = [[Contact alloc] initWithName:@"qiuse" gender:@"男" age:23 address:@"丽德" phoneNumber:@"18321978135"];

Contact *c7 = [[Contact alloc] initWithName:@"juju" gender:@"女" age:24 address:@"丽德" phoneNumber:@"15156697015"];

[cm addNewContact:c1];

[cm addNewContact:c2];

[cm addNewContact:c3];

[cm addNewContact:c4];

[cm addNewContact:c5];

[cm addNewContact:c6];

[cm addNewContact:c7];

[cm showAllContacts];

NSLog(@"-----华丽的分割线-----");

NSArray *array = [cm getAllContactsByGroupName:@"w"];

NSLog(@"%@",array);

NSLog(@"-----华丽的分割线-----");

Contact *resultContact = [cm findContactByPhoneNumber:@"15156697015"];

NSLog(@"%@",resultContact);

NSLog(@"-----华丽的分割线-----");

NSArray *femalArray = [cm getAllFemalContactsByGender:@"女"];

NSLog(@"%@",femalArray);

NSLog(@"-----华丽的分割线-----");

[cm removeContactByName:@"juju"];

[cm showAllContacts];

NSLog(@"-----华丽的分割线-----");

[cm removeAllContactsByGroupName:@"w"];

[cm showAllContacts];

return 0;

}

时间: 2024-10-24 03:55:25

OC 通讯录 中等难度的相关文章

用OC实现实现中等难度通讯录详解

用OC实现中等难度通讯录.需求:      1.定义联系人类Contact.实例变量:姓名(拼音,?字?大写).性别.电话号码. 住址.分组名称.年龄.?法:?定义初始化方法或便利构造器方法.显?联系人信息      2.在main.m中定义字典,分组管理所有联系人.分组名为26个大写的英文字母.      3.可以添加联系?对象,如果姓名或电话号码为空,添加失败.添加联系?到匹配的分组.      4.获取某个分组名称下所有联系人,并且按照姓名升序排列.      5.从通讯录中根据电话号码搜

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

【bfs】【中等难度】wikioi3055 青铜莲花池

3055 青铜莲花池 题目描述 Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M 行N 列个方格(1 ≤ M, N ≤ 30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是美丽.纯净.湛蓝的水.贝西正在练习芭蕾舞,她站在一朵莲花上,想跳到另一朵莲 花上去,她只能从一朵莲花跳到另一朵莲花上,既不能跳到水里,也不能跳到岩石上.      贝西的舞步很像象棋中的马步:每次总是先横向移动M1 (1 ≤ M1 ≤ 30)格,再纵向移

【快速幂+中等难度】Calculation 哈工大HITOJ2901

这些天好颓啊..都没有A题,只是各种等着填的坑..简直.. 这道题....其实是快速幂模板题..为了学习矩阵快速幂,顺手复习下快速幂... 哈工大的OJ其实还挺友好的.速度也快..赞一个.. 翻译 给你两个数A,B,要你求(1b + 2b + ... + ab) 这个式子mod a的结果.(b是奇数) 每行一组数据 (我以下的代码和解释都是用大写的A,B来代替原题小写a,b,代码中的小写a只是一个类似tmp的东西) 原题 http://acm.hit.edu.cn/hoj/problem/vie

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

poj 1062 昂贵的聘礼 Dijkstra算法,中等难度,,,一道让我累觉不爱的题目

昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38474   Accepted: 11132 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要5000

【二分】【中等难度】noip模拟赛 聪哥的工资

聪哥的工资 (money/money.in/money.out) 时限1000ms 内存256MB 题目描述 lwher: 了体验劳苦大众的生活,聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi.有m个结算工钱的时间,聪哥可以自由安排这些时间,也就是说什么时候拿钱,老板说的不算,聪哥才有发言权! (因为聪哥是土豪,他是老板的老板,你觉得老板敢给聪哥安排任务吗?所以聪哥的工作就是看心情去拿钱拿完就走人啦...) 聪哥不喜欢身上一次性有太多的钱,于是他想安排一下拿钱的时间

【乱搞】【中等难度】noip模拟赛 藏妹子之处

藏妹子之处(excel) 问题描述: 今天陈泽宇又找到了三个妹子,有着收藏爱好的他想要找三个地方将妹子们藏起来,将一片空地抽象成一个R行C列的表格,CZY要选出3个单元格.但要满足如下的两个条件: (1)任意两个单元格都不在同一行. (2)任意两个单元格都不在同一列. 选取格子存在一个花费,而这个花费是三个格子两两之间曼哈顿距离的和(如(x1,y1)和(x,y2)的曼哈顿距离为|x1-x2|+|y1-y2|).狗狗想知道的是,花费在minT到maxT之间的方案数有多少. 答案模100000000

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.