OC上机考试试题 ————通讯录
要求描述:⽤OC语言完成简易通讯录(实现增删改查)功 能.
(注:使⽤用MRC)
一、功能描述:
main函数中通过使用Person类和AddressBook类,创建对象、调⽤方法,实现以下要求 :
1).使⽤便利构造器创建四个Person类的实例(联系人).
姓名:Lucy 性别:⼥ 电话:123 年龄:21 姓名:Joe 性别:男 电话:456 年龄:17 姓名:Baby 性别:女 电话:789 年龄:27 姓名:Linda 性别:⼥ 电话:000 年龄:21
2).创建⼀一个AddressBook类的实例对象(通讯录). 3).使用AddressBook类中定义的⽅法实现:添加联系人,将四个联系人添加到通讯录
4).使⽤AddressBook类中定义的方法实现:根据电话查找联系人,并打印(查找电话456)
5).使用AddressBook类中定义的方法实现:根据性别查找联系人,并将查到的联系⼈按照年龄升序排序,并打印(查找性别:女)
6).使⽤AddressBook类中定义的方法实现:根据姓名,更改联系人信息.(修改Linda的 信息: 性别:女 电话:012 年龄:25);
7).使用AddressBook类中定义的⽅法实现:按照姓名删除联系人,并展示通信录中所 有人信息.(删除联系人:Baby)
8).使用AddressBook类中定义的方法实现:删除一个分组,并展示通信录中所有人信息.(删除L组) 所有联系人信息。
9).使用AddressBook类中定义的方法实现:实现3~8之间每个功能后,输出通讯录中所有联系人
以下代码是个人编写,不是按照题目中使用的联系人;
代码:
Person.h
#import
@interface Person : NSObject
@property (nonatomic,retain)NSString *name; //姓名
@property (nonatomic,assign)NSInteger age; //年龄
@property (nonatomic,retain)NSString *gender; //性别
@property (nonatomic,retain)NSString *phoneNumber; // 电话号码
@property (nonatomic,retain)NSString *groupName; // 分组名
-(void)description;
- (instancetype)initWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
phoneNuber:(NSString *)phoneNumber;
+ (instancetype)personWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
phoneNuber:(NSString *)phoneNumber;
Person.m
#import "Person.h"
@implementation Person
// 防止内存泄漏
-(void)dealloc
{
[_name release];
[_gender release];
[_phoneNumber release];
[_groupName release];
[super dealloc];
}
// 复写的话应该和父类的方名,应该和父类一样,但是这次我直接在这个方法打印结果,所以必须使用该方法,不是直接系统调用
-(void)description
{
NSLog(@"姓名:%@ 性别:%@ 年龄:%ld 电话:%@ 分组名:%@",self.name,self.gender,self.age,self.phoneNumber,self.groupName);
}
//这里没有初始化groupName是因为
- (instancetype)initWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
phoneNuber:(NSString *)phoneNumber
{
self = [super init];
if (self) {
self.name = name;
self.gender = gender;
self.age = age;
self.phoneNumber = phoneNumber;
}
return self;
}
+ (instancetype)personWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
phoneNuber:(NSString *)phoneNumber
{
Person *p = [[Person alloc]initWithName:name age:age gender:gender phoneNuber:phoneNumber];
return [p autorelease];
}
AddressBook.h
#import
@class Person;
@interface AddressBook : NSObject
@property (nonatomic,retain)NSMutableDictionary *allPersonDic;
- (BOOL)addPerson:(Person *)aPerson;
-(Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum;
-(NSArray *)findPersonsWithSex:(NSString *)aSex;
-(BOOL)changePersonWithName:(NSString *)name phoneNum:
(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger) aAge;
- (BOOL)deletePersonWithName:(NSString*)aPersonName;
- (BOOL)deleteGroup:(NSString *)aGroupName;
-(void)showAll;
@end
AddressBook.m
#import "AddressBook.h"
#import "Person.h"
#import "NSString+Characters.h"
@implementation AddressBook
-(void)dealloc
{
[_allPersonDic release];
[super dealloc];
}
-(instancetype)init
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
self.allPersonDic = dic;
return self;
}
/**
* b.判断姓名和电话不能为空否则添加失败. c.判断是否已经存在分组,存在直接存储.不存在创建分组后再存储.(注:联系⼈人
d.添加成功返回YES,姓名为空或者有重名的添加失败返回NO. e.数据使⽤用Person存储.
*/
-(BOOL)addPerson:(Person *)aPerson
{
if (aPerson.name == NULL || aPerson.phoneNumber == NULL) {
NSLog(@"姓名和电话为空,添加失败");
return NO;
}
aPerson.groupName = [aPerson.name uppercasePinYinFirstLetter];
NSMutableArray *arr = [_allPersonDic objectForKey:aPerson.groupName];
if ([arr count] == 0) {
arr = [NSMutableArray array];
[arr addObject:aPerson];
[_allPersonDic setObject:arr forKey:aPerson.groupName];
return YES;
}else
{
[arr addObject:aPerson];
return YES;
}
}
// b:如果找到返回该联系⼈人,否则返回nil. c.忽略电话重复问题,只返回第⼀一个查找到的联系⼈人。
-(Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum
{
for (NSString *key in _allPersonDic) {
for (Person *person in _allPersonDic[key]) {
[aPhoneNum isEqualToString:person.phoneNumber];
return person;
}
}
return nil;
}
-(NSArray *)findPersonsWithSex:(NSString *)aSex
{
NSMutableArray *array = [NSMutableArray array];
for (NSString *key in _allPersonDic) {
for (Person *person in _allPersonDic[key]) {
if ([aSex isEqualToString:person.gender]) {
[array addObject:person];
}
}
}
[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Person *p1 = (Person *)obj1;
Person *p2 = (Person *)obj2;
if(p1.age > p2.age){
return NSOrderedAscending;
}else if(p1.age == p2.age){
return NSOrderedSame;
}else{
return NSOrderedDescending;
}
}];
return array;
}
-(BOOL)changePersonWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge
{
for (NSString *key in _allPersonDic) {
for (Person *person in _allPersonDic[key]) {
if ([person.name isEqualToString:name]) {
person.gender = aSex;
person.age = aAge;
person.phoneNumber = aNum;
return YES;
}
}
}
return NO;
}
-(BOOL)deletePersonWithName:(NSString *)aPersonName
{
Person *person1 = nil;
for (NSString *key in _allPersonDic) {
for (Person *person in _allPersonDic[key]) {
if ([person.name isEqualToString:aPersonName]) {
person1 = person;
break;
}
}
}
if (person1 != nil) {
NSMutableArray *array = [self.allPersonDic objectForKey:person1.groupName];
if ([array count] == 1) {
[self.allPersonDic removeObjectForKey:person1.groupName];
return YES;
}else
{
[array removeObject:person1];
return YES;
}
}else
{
NSLog(@"没有该联系人,无法删除");
return NO;
}
}
- (BOOL)deleteGroup:(NSString *)aGroupName
{
for (NSString *key in self.allPersonDic) {
if ([key isEqualToString:aGroupName]) {
[self.allPersonDic removeObjectForKey:key];
return YES;
}
}
return NO;
}
// 显示所有联系人
-(void)showAll
{
for (NSString *key in self.allPersonDic) {
for (Person *person in self.allPersonDic[key]) {
[person description];
}
}
}
@end
main.m
#import
#import "Person.h"
#import "AddressBook.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *p1 = [Person personWithName:@"张三" age:15 gender:@"男" phoneNuber:@"111"];
Person *p2 = [Person personWithName:@"乔四" age:19 gender:@"女" phoneNuber:@"222"];
Person *p3 = [Person personWithName:@"王五" age:16 gender:@"男" phoneNuber:@"555"];
Person *p4 = [Person personWithName:@"刘六" age:20 gender:@"女" phoneNuber:@"666"];
Person *p5 = [Person personWithName:@"杨七" age:20 gender:@"女" phoneNuber:@"666"];
Person *p6 = [Person personWithName:@"臧九" age:20 gender:@"男" phoneNuber:@"666"];
AddressBook *addressBook = [[AddressBook alloc]init];
NSLog(@"添加你声明的Person类的对象");
[addressBook addPerson:p1];
[addressBook addPerson:p2];
[addressBook addPerson:p3];
[addressBook addPerson:p4];
[addressBook addPerson:p5];
[addressBook addPerson:p6];
NSLog(@"添加后的所有联系人");
[addressBook showAll];
NSLog(@"显示所有的性别为男的联系人") ;
NSArray *array = [addressBook findPersonsWithSex:@"男"];
for (Person *person in array) {
[person description];
}
NSLog(@"删除联系人刘六");
[addressBook deletePersonWithName:@"刘五"];
[addressBook deletePersonWithName:@"刘六"];
[addressBook showAll];
NSLog(@"改变张三的性别,年龄,电话号码");
[addressBook changePersonWithName:@"张三" phoneNum:@"999" sex:@"女" age:66];
[addressBook showAll];
NSLog(@"删除分组名为Z的所有联系人");
[addressBook deleteGroup:@"Z"];
[addressBook showAll];
// 释放自动调用dealloc
[addressBook release];
}
return 0;
}
NSString + Characters.h
#import
@interface NSString (Characters)
//讲汉字转换为拼音
- (NSString *)pinyinOfName;
//汉字转换为拼音后,返回大写的首字母
- (NSString *)uppercasePinYinFirstLetter;
@end
NSString + Characters.m
#import "NSString+Characters.h"
@implementation NSString (Characters)
//将汉字转换为拼音
- (NSString *)pinyinOfName
{
NSMutableString * name = [[[NSMutableString alloc] initWithString:self ] autorelease];
CFRange range = CFRangeMake(0, 1);
// 汉字转换为拼音,并去除音调
if ( ! CFStringTransform((__bridge CFMutableStringRef) name, &range, kCFStringTransformMandarinLatin, NO) ||
! CFStringTransform((__bridge CFMutableStringRef) name, &range, kCFStringTransformStripDiacritics, NO)) {
return @"";
}
return name;
}
//汉字转换为拼音后,返回大写的首字母
- (NSString *)uppercasePinYinFirstLetter
{
NSMutableString * first = [[[NSMutableString alloc] initWithString:[self substringWithRange:NSMakeRange(0, 1)]] autorelease];
CFRange range = CFRangeMake(0, 1);
// 汉字转换为拼音,并去除音调
if ( ! CFStringTransform((__bridge CFMutableStringRef) first, &range, kCFStringTransformMandarinLatin, NO) ||
! CFStringTransform((__bridge CFMutableStringRef) first, &range, kCFStringTransformStripDiacritics, NO)) {
return @"";
}
NSString * result;
result = [first substringWithRange:NSMakeRange(0, 1)];
return result.uppercaseString;
}
@end