1.Contact.h
#import <Foundation/Foundation.h>
@interface Contact : NSObject
/**
*? 姓名
*/
@property(nonatomic, copy) NSString *name;
/**
*? 電話號(hào)碼
*/
@property(nonatomic, copy) NSString *phoneNumber;
/**
*? 分組名稱
*/
@property(nonatomic, copy) NSString *groupName;
- (instancetype)initWithName:(NSString *)name PhoneNumber:(NSString *)phoneNumber;
+ (instancetype)contactWithName:(NSString *)name PhoneNumber:(NSString *)phoneNumber;
- (void)showInfo;
? ?@end
2.Contact.m
#import "Contact.h"
@implementation Contact
#pragma mark 重寫dealloc
- (void)dealloc
{
[_name release];
[_phoneNumber release];
[_groupName release];
[super dealloc];
NSLog(@"contact對(duì)象已釋放!");
}
#pragma mark 初始化方法
- (instancetype)initWithName:(NSString *)name PhoneNumber:(NSString *)phoneNumber
{
self = [super init];
if (self)
{
//初始化過程中直接將姓名首字母大寫,規(guī)范格式
_name = [name capitalizedString];
_phoneNumber = phoneNumber;
//直接將分組名設(shè)置為首字母
_groupName? = [_name substringToIndex:1];
}
return self;
}
#pragma mark 遍歷構(gòu)造器
+ (instancetype)contactWithName:(NSString *)name PhoneNumber:(NSString *)phoneNumber
{
return [[[Contact alloc] initWithName:name PhoneNumber:phoneNumber] autorelease];
}
#pragma mark 顯示聯(lián)系人信息
- (void)showInfo
{
NSLog(@"%@-%@", self.name, self.phoneNumber);
}
@end
3.ContactBook.h
#import#import "Contact.h"
@interface ContactBook : NSObject
/**
*? 聯(lián)系人字典
*/
@property(nonatomic, retain) NSMutableDictionary *contactDic;
- (void)showContactInfo;
- (void)addNewContact;
- (void)removeContactByName;
- (void)removeContactByPhoneNumber;
- (void)removeAllGroup;
- (void)searchByName;
- (void)searchByPhoneNumber;
- (void)sortByNameAscending;
@end
4.Contact.m
#import "ContactBook.h"
@implementation ContactBook
#pragma mark 重寫init
- (instancetype)init
{
self = [super init];
if (self)
{
//在init中進(jìn)行字典的初始化過程,可以讓程序更好的封裝
//1.定義可變數(shù)組allKey接收26個(gè)大寫英文字母
NSMutableArray *allKey = [NSMutableArray array];
//2.定義可變數(shù)組allValue接收26個(gè)存儲(chǔ)在對(duì)應(yīng)key下的存儲(chǔ)聯(lián)系人的數(shù)組
NSMutableArray *allValue = [NSMutableArray array];
for (NSInteger i = 65; i < 91; i++)
{
//此處通過format這個(gè)萬能公式將長(zhǎng)整形i轉(zhuǎn)化為字符類型,將26個(gè)字母存入allKey數(shù)組(此處操作會(huì)出現(xiàn)編譯警告,暫時(shí)沒想出其他的方法)
[allKey addObject:[NSString stringWithFormat:@"%c",i]];
//此處將26個(gè)空數(shù)組放入allValue
[allValue addObject:[NSMutableArray array]];
}
//通過allKey和allValue為存儲(chǔ)字典賦值
_contactDic = [NSMutableDictionary dictionaryWithObjects:allValue forKeys:allKey];
}
//用來檢測(cè)字典是否創(chuàng)建成功,正式運(yùn)行時(shí)可注釋
NSLog(@"%@", _contactDic);
return self;
}
#pragma mark 顯示通訊錄聯(lián)系人信息
- (void)showContactInfo
{
//用來進(jìn)行計(jì)數(shù)
NSInteger count = 0;
//對(duì)字典進(jìn)行循環(huán)
for (NSString *str in self.contactDic)
{
//對(duì)每個(gè)key對(duì)應(yīng)的數(shù)組進(jìn)行遍歷,輸出聯(lián)系人
for (Contact *contact in [self.contactDic objectForKey:str])
{
NSLog(@"%@-%@", contact.name, contact.phoneNumber);
count ++;
}
}
//如果flag等于0則說明26個(gè)數(shù)組全為空
if (count == 0)
{
//輸出通訊錄為空
printf("通訊錄為空!\n");
}
else
{
NSLog(@"輸出結(jié)束,總共%ld個(gè)聯(lián)系人!", count);
}
}
#pragma mark 添加聯(lián)系人
- (void)addNewContact
{
char name[30];
printf("請(qǐng)輸入聯(lián)系人姓名:");
scanf("%s", name);
char phoneNumber[30];
printf("請(qǐng)輸入聯(lián)系人電話號(hào)碼:");
scanf("%s", phoneNumber);
//通過NSString的方法將用戶輸入字符串轉(zhuǎn)換為對(duì)象
NSString *s_name? ? ? ? = [NSString stringWithUTF8String:name];
NSString *s_phoneNumber = [NSString stringWithUTF8String:phoneNumber];
//用s_name和s_phoneNumber新建聯(lián)系人
Contact *newContact = [Contact contactWithName:s_name PhoneNumber:s_phoneNumber];
//通過分組名稱找到對(duì)應(yīng)數(shù)組將聯(lián)系人添加進(jìn)數(shù)組
[[self.contactDic objectForKey:[newContact groupName]] addObject:newContact];
printf("添加成功!\n");
NSLog(@"%@", self.contactDic);
}
#pragma mark 通過姓名刪除聯(lián)系人
- (void)removeContactByName
{
NSInteger count = 0;
char name[30];
printf("請(qǐng)輸入要?jiǎng)h除的聯(lián)系人姓名:");
scanf("%s", name);
NSString *s_name = [[NSString stringWithUTF8String:name] capitalizedString];
NSString *s_groupName = [s_name substringToIndex:1];
//接收groupName對(duì)應(yīng)的目標(biāo)數(shù)組
NSMutableArray *targetArray = [self.contactDic objectForKey:s_groupName];
for (Contact *contact in targetArray)
{
if ([contact.name isEqualToString: s_name])
{
[targetArray removeObject:contact];
count ++;
printf("刪除成功!\n");
}
}
//如果沒有刪除到聯(lián)系人則顯示無此聯(lián)系人
if (count == 0)
{
printf("無此聯(lián)系人!\n");
}
}
#pragma mark 通過電話號(hào)碼刪除聯(lián)系人
- (void)removeContactByPhoneNumber
{
NSInteger count = 0;
char phoneNumber[30];
printf("請(qǐng)輸入聯(lián)系人電話號(hào)碼:");
scanf("%s", phoneNumber);
NSString *s_phoneNumber = [NSString stringWithUTF8String:phoneNumber];
for (NSString *str in self.contactDic)
{
NSMutableArray *targetArray = [self.contactDic objectForKey:str];
for (Contact *contact in targetArray)
{
if ([contact.phoneNumber isEqualToString:s_phoneNumber])
{
[targetArray removeObject:contact];
printf("刪除成功!\n");
count ++;
}
}
[targetArray release];
}
//如果沒有刪除到聯(lián)系人則顯示無此聯(lián)系人
if (count == 0)
{
printf("此電話號(hào)碼不在通訊錄中!\n");
}
}
#pragma mark 刪除分組
- (void)removeAllGroup
{
char groupName[30];
printf("請(qǐng)輸入分組字母:");
scanf("%s", groupName);
NSString *s_groupName = [[NSString stringWithUTF8String:groupName] capitalizedString];
//如果輸入字符過多則返回輸入有誤
if (s_groupName.length > 1)
{
printf("輸入有誤!");
}
else if ((groupName[0] >= 'a' && groupName[0] <= 'z') || (groupName[0] >= 'A' && groupName[0] <= 'Z'))
{
[self.contactDic removeObjectForKey:s_groupName];
printf("刪除成功!\n");
}
}
#pragma mark 按照姓名查找
- (void)searchByName
{
NSInteger count = 0;
char name[30];
printf("請(qǐng)輸入要?jiǎng)h除的聯(lián)系人姓名:");
scanf("%s", name);
NSString *s_name = [[NSString stringWithUTF8String:name] capitalizedString];
NSString *s_groupName = [s_name substringToIndex:1];
//接收groupName對(duì)應(yīng)的目標(biāo)數(shù)組
NSMutableArray *targetArray = [self.contactDic objectForKey:s_groupName];
for (Contact *contact in targetArray)
{
if ([contact.name isEqualToString: s_name])
{
[contact showInfo];
}
}
//如果沒有刪除到聯(lián)系人則顯示無此聯(lián)系人
if (count == 0)
{
printf("無此聯(lián)系人!\n");
}
[s_groupName release];
[targetArray release];
}
#pragma mark 按電話號(hào)碼查找
- (void)searchByPhoneNumber
{
NSInteger count = 0;
char phoneNumber[30];
printf("請(qǐng)輸入聯(lián)系人電話號(hào)碼:");
scanf("%s", phoneNumber);
NSString *s_phoneNumber = [NSString stringWithUTF8String:phoneNumber];
for (NSString *str in self.contactDic)
{
NSMutableArray *targetArray = [self.contactDic objectForKey:str];
for (Contact *contact in targetArray)
{
if ([contact.phoneNumber isEqualToString:s_phoneNumber])
{
[contact showInfo];
}
}
[targetArray release];
}
//如果沒有刪除到聯(lián)系人則顯示無此聯(lián)系人
if (count == 0)
{
printf("此電話號(hào)碼不在通訊錄中!\n");
}
}
#pragma mark 按照姓名升序排序
- (void)sortByNameAscending
{
NSSortDescriptor *desName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:1];
for (NSString *str in self.contactDic)
{
//進(jìn)行排序
NSMutableArray *targetArr = [self.contactDic objectForKey:str];
[targetArr sortUsingDescriptors:@[desName]];
//輸出排序結(jié)果
for (Contact *contact in targetArr)
{
[contact showInfo];
}
[targetArr release];
}
[desName release];
}
@end
5.main.m
#import <Foundation/Foundation.h>
#import "Contact.h"
#import "ContactBook.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
ContactBook *myContactBook = [[ContactBook alloc] init];
while (YES)
{
printf("-----------------------------\n");
printf("------------通訊錄------------\n");
printf("------1.查看通訊全部聯(lián)系人------\n");
printf("------2.添加聯(lián)系人-------------\n");
printf("------3.刪除聯(lián)系人-------------\n");
printf("------4.查找聯(lián)系人-------------\n");
printf("------5.對(duì)聯(lián)系人進(jìn)行排序--------\n");
printf("------6.退出------------------\n");
printf("-----------------------------\n");
//提示用戶輸入操作代碼
NSInteger userNumber = 0;
printf("請(qǐng)輸入您要進(jìn)行的操作:");
scanf("%ld", &userNumber);
//運(yùn)用switch語句進(jìn)行分支功能的執(zhí)行
switch (userNumber)
{
case 1:
{
[myContactBook showContactInfo];
}
break;
case 2:
{
[myContactBook addNewContact];
}
break;
case 3:
{
BOOL b = YES;
while (b)
{
printf("-----------------------------\n");
printf("--------1.通過姓名刪除---------\n");
printf("--------2.通過號(hào)碼刪除---------\n");
printf("--------3.刪除整個(gè)分組---------\n");
printf("--------4.返回上級(jí)菜單---------\n");
printf("-----------------------------\n");
NSInteger Num = 0;
printf("請(qǐng)輸入操作代碼:");
scanf("%ld", &Num);
switch (Num) {
case 1:
{
[myContactBook removeContactByName];
}
break;
case 2:
{
[myContactBook removeContactByPhoneNumber];
}
break;
case 3:
{
[myContactBook removeAllGroup];
}
break;
case 4:
{
b = NO;
break;
}
break;
default:
{
printf("輸入有誤!\n");
break;
}
break;
}
}
}
break;
case 4:
{
BOOL b = YES;
while (b)
{
printf("-----------------------------\n");
printf("--------1.通過姓名查找---------\n");
printf("--------2.通過號(hào)碼查找---------\n");
printf("--------3.返回上級(jí)菜單---------\n");
printf("-----------------------------\n");
NSInteger Num = 0;
printf("請(qǐng)輸入操作代碼:");
scanf("%ld", &Num);
switch (Num) {
case 1:
{
[myContactBook searchByName];
}
break;
case 2:
{
[myContactBook searchByPhoneNumber];
}
break;
case 3:
{
b = NO;
break;
}
break;
default:
{
printf("輸入有誤!\n");
break;
}
break;
}
}
}
break;
case 5:
{
[myContactBook sortByNameAscending];
}
break;
case 6:
{
return 0;
}
break;
default:
break;
}
}
}
return 0;
}