实现中等难度通讯录。需求: 1、定义联系人类AddressContact。实例变量:姓名(拼音,首字母大写)、性别、电话号码、住址、分组名称、年龄。方法:自定义初始化方法(姓名、电话号码)、显示联系人

http://blog.csdn.net/cuichengjie/article/details/38446291


//  1、定义联系人类AddressContact。实例变量:姓名(拼音,首字母大写)、性别、电话号码、住址、分组名称、年龄。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息  
{  
    NSString *_name;//姓名  
    NSString *_gender;//性别  
    NSInteger _phoneNumber;//电话号码  
    NSString *_location;//住址  
    NSString *_dividName;//分组名称  
    NSInteger _age;//年龄  
}  
//初始化方法  
- (id)initWithName:(NSString *)name phoneNumber:(NSInteger)phoneNumber;  
//显示联系人信息  
- (NSString *)description;  
//setter  
- (void)setDividName:(NSString *)dividName;  
//getter  
- (NSString *)name;  
- (NSInteger)phoneNumber;  
- (NSString *)dividName;  
  
@end  
@implementation AddressContact  
//初始化方法  
- (id)initWithName:(NSString *)name phoneNumber:(NSInteger)phoneNumber  
{  
    self = [super init];  
    if (self) {  
        _name = name;  
        _phoneNumber = phoneNumber;  
    }  
    return self;  
}  
//显示联系人信息  
- (NSString *)description  
{  
    return [NSString stringWithFormat:@"name:%@, gender:%@, phoneNumber:%ld, location:%@,dividName:%@, age:%ld", _name, _gender, _phoneNumber, _location, _dividName, _age];  
}  
//setter  
- (void)setDividName:(NSString *)dividName  
{  
    _dividName = dividName;  
}  
//getter  
- (NSString *)name  
{  
    return _name;  
}  
- (NSInteger)phoneNumber  
{  
    return _phoneNumber;  
}  
- (NSString *)dividName  
{  
    return _dividName;  
}  
@end<pre name="code" class="objc">int main(int argc, const char * argv[])  
{  
  
    @autoreleasepool {  
        NSArray *arr1 = @[@1,@2,@3];  
        NSArray *arr2 = @[@1,@2,@3];  
        BOOL d = [arr1 isEqualToArray:arr2];  
        NSLog(@"%d",d);  
        //创建对象  
        AddressContact *Cuichengjie = [[AddressContact alloc] initWithName:@"Cuichengjie" phoneNumber:10086];  
        AddressContact *Gengzhi = [[AddressContact alloc] initWithName:@"Gengzhi" phoneNumber:100861];  
        AddressContact *Wangjie = [[AddressContact alloc] initWithName:@"Wangjie" phoneNumber:100862];  
        AddressContact *Hongjie = [[AddressContact alloc] initWithName:@"Hongjie" phoneNumber:100863];  
        AddressContact *Xiaoguang = [[AddressContact alloc] initWithName:@"Xiaoguang" phoneNumber:100864];  
        AddressContact *Xiaoming = [[AddressContact alloc] initWithName:@"Xiaoming" phoneNumber:100865];  
        AddressContact *Chengcheng = [[AddressContact alloc] initWithName:@"Chengcheng" phoneNumber:100866];  
        AddressContact *Bug = [[AddressContact alloc] initWithName:@"Bug" phoneNumber:100867];  
        AddressContact *Bug1 = [[AddressContact alloc] initWithName:@"Bug1" phoneNumber:100868];  
        AddressContact *Baige = [[AddressContact alloc] initWithName:@"Baige" phoneNumber:100869];  
        //创建可变数组  
        NSMutableArray *arrB = [NSMutableArray arrayWithObjects:Bug, Bug1, Baige, nil];  
        NSMutableArray *arrC = [NSMutableArray arrayWithObjects:Cuichengjie, Chengcheng, nil];  
        NSMutableArray *arrG = [NSMutableArray arrayWithObjects:Gengzhi, nil];  
        NSMutableArray *arrH = [NSMutableArray arrayWithObjects:Hongjie, nil];  
        NSMutableArray *arrW = [NSMutableArray arrayWithObjects:Wangjie, nil];  
        NSMutableArray *arrX = [NSMutableArray arrayWithObjects:Xiaoguang, Xiaoming, nil];  
        NSMutableDictionary *conductMan = [NSMutableDictionary dictionaryWithObjectsAndKeys:arrB, @"B", arrC, @"C", arrG, @"G", arrH, @"H", arrW, @"W", arrX, @"X", nil];  
        NSLog(@"%@", Cuichengjie);  
          
        //添加联系人对象  
        AddressContact *Hengming = [[AddressContact alloc] initWithName:@"Hengming" phoneNumber:10087];//要添加的对象  
        NSString *subString = [[Hengming name] substringToIndex:1];//获取姓名的第一个字母  
        for (NSString *str in conductMan) {  
            if ([subString isEqualToString:nil] || [@"[Gengming phoneNumber]" isEqualToString:nil ]) {  
                NSLog(@"添加失败");  
            } else if ([str isEqualToString:subString]){  
                [[conductMan objectForKey:str] addObject:Hengming];  
            }  
            for (AddressContact *aa in [conductMan objectForKey:str]) {  
                NSLog(@"%@",aa);  
            }  
        }  
        //从通讯录中根据电话号码搜索联系人  
        int n = 0;  
        printf("请输入一个电话号码:");  
        scanf("%d", &n);  
        NSNumber *number = [NSNumber numberWithInt:n];  
        for (NSString *str in conductMan) {  
            for (AddressContact *aa in [conductMan objectForKey:str]) {  
                NSNumber *num = [NSNumber numberWithInteger:[aa phoneNumber]];                if ([number compare:num] == 0) {  
                    NSLog(@"%@", aa);  
                }  
            }  
        }  
        //根据姓名删除某个联系人  
        char a[15] = " 0";  
        printf("请输入一个姓名:");  
        scanf("%s", a);  
        NSString *string = [NSString stringWithFormat:@"%s", a];  
        for (NSString *str in conductMan) {  
            NSArray *arr = [NSArray arrayWithArray:[conductMan objectForKey:str]];  
            for (AddressContact *cui in arr) {  
                if ([string compare:[cui name]] == 0) {  
                    [[conductMan objectForKey:str] removeObject:cui];  
                }  
            }  
        }  
        //把删除之后的该数组中的联系人打印出来  
        NSString *subString1 = [string substringToIndex:1];//获取姓名的第一个字母(对象)  
        for (AddressContact *aa in [conductMan objectForKey:subString1]) {  
            NSLog(@"%@",aa);  
        }  
      
  
        //删除某个分组的全部联系人  
        char b = '0';  
        printf("请输入一个字符:");  
        getchar();  
        scanf("%c",&b);  
        NSString *string1 = [NSString stringWithFormat:@"%c", b];  
        for (NSString *str in conductMan) {  
            if ([string1 compare:str] == 0) {  
                [[conductMan objectForKey:str] removeAllObjects];  
            }  
        }  
          
    }  
    return 0;  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值