3道比较有用的oc练习题

//

//  main.m

//  3道比较重要的作业

//

//  Created by dllo on 15/10/30.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Contact.h"

#import "ContactManager.h"

#import "Color.h"


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

   

    

//    NSString *filePath =@"/Users/dllo/Desktop/3道比较重要的作业/3道比较重要的作业/File";

//    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

//    //NSLog(@"%@",str);

//    NSArray *arr = [str componentsSeparatedByString:@"\n"];

//    NSMutableDictionary *colorDic = [NSMutableDictionary dictionary];

//    for (NSInteger i = 0; i < arr.count; i++) {

//        NSArray * a = [[arr objectAtIndex:i] componentsSeparatedByString:@" #"];

//        [colorDic setValue:a[1] forKey:a[0]];

//    }

//    // 2、取出所有的key,升序排列。

////    for (NSInteger i = 0; i < colorDic.allKeys.count; i++) {

////        NSLog(@"%@", [colorDic.allKeys objectAtIndex:i]);

////    }

//    NSArray *keyArr = [[colorDic allKeys] sortedArrayUsingSelector:@selector(compare:)];

//   // NSLog(@"%@", keyArr);

//    

//    // 3、取出所有的value,按照排序后的key排列。

//    NSMutableArray *value = [NSMutableArray array];

//    for (NSString *key in str1) {

//        NSString *value1 = [colorDic objectForKey:key];

//        [value addObject:value1];

//    }

    //第二种方法

//    for (NSString *key in keyArr) {

//        NSLog(@"%@", [colorDic objectForKey:key]);

//    }

//   // NSLog(@"%@", value);

//     //4、使用一个新的字典管理颜色,对颜色进行分类管理,即:“A”“B”“C”…即这个字典包含多个键值对,key26个字母,value是数组,数组里存放的是Color对象(包含namecolorValue)。需要自己创建Color

//  //  --------------------------------------------------------------静态方法----------------------------------------------------------------------------

//    NSArray *arr5 = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"Q", @"W", @"E", @"R", @"T", @"Y",@"U", @"P",@"O",@"Z", @"X", @"V", nil];

//    //创建一个数组.包含26字母

//    NSMutableDictionary *ColorDic2 = [NSMutableDictionary dictionary];

//    //创建字典

//    for (NSInteger i = 0; i < arr5.count ; i++) {

//        //arr5   26  key

//        NSString *key1= [arr5 objectAtIndex:i];

//        NSMutableArray *value1 = [NSMutableArray array];

//        //arr4 所有颜色名字子

//        NSArray *arr4 = [colorDic allKeys];

//        for (NSInteger j = 0; j < arr4.count; j++) {

//            if ([[arr4 objectAtIndex:j] hasPrefix:key1]) {

//                Color *color = [[Color alloc]init];

//                [color setName:[arr4 objectAtIndex:j]];

//                [color setColorValue:[colorDic valueForKey:[arr4 objectAtIndex:j]]];

//                [value1 addObject:color];

//            }

//          [ColorDic2 setValue:value1 forKey:[arr4 objectAtIndex:i]];

//        }

//        

//    }

//    for (NSString *key1 in ColorDic2) {

//        for (Color *c in [ColorDic2 valueForKey:key1]) {

//            NSLog(@"%@ %@",[c name], [c colorValue]);

//        }

//    }

    

    //***************************************************动态方法**************************************************************************************//

//    

//    NSMutableDictionary *ColorDic2 = [NSMutableDictionary dictionary];

//    NSArray *arr4 = [colorDic allKeys];

//    for (NSInteger i = 0; i < arr4.count; i++) {

//        NSString *key1 = [[arr4 objectAtIndex:i] substringToIndex:1];

//        NSMutableArray *colorArr = nil;

//        if ( nil == [ColorDic2 valueForKey:key1] ) {

//              colorArr = [NSMutableArray array];

//            [ColorDic2 setObject:colorArr forKey:key1];

//        }else {

//            colorArr = [ColorDic2 valueForKey:key1];

//        }

//        Color *c = [[Color alloc] initWithName:[arr4 objectAtIndex:i] colorValue:[colorDic valueForKey:[arr4 objectAtIndex:i]]];

//        [colorArr addObject:c];

//        

//    }

//    for (NSString *key1 in ColorDic2) {

//                for (Color *c in [ColorDic2 valueForKey:key1]) {

//                    [ColorDic2.allKeys sortedArrayUsingSelector:@selector(compare:)];

//                    NSLog(@"%@ %@",[c name], [c colorValue]);

//                }

//            }


    

    

    

    

    

    

    

    


//    //--------------------------------第一题答案------------------------------------//

//    Contact *str1 = [[Contact alloc]initWithName:@"yuhao" sex:@"" phone:@"15702445035" address:@"hhh" group:@"friend" age:23];

//     Contact *str2 = [[Contact alloc]initWithName:@"yw" sex:@"" phone:@"15002445035" address:@"hhh" group:@"friend" age:22];

//     Contact *str3 = [[Contact alloc]initWithName:@"ww" sex:@"" phone:@"15802445035" address:@"hhh" group:@"friend" age:23];

//    ContactManager *p = [[ContactManager alloc]init];

//    [p addContact:str1];

//    [p addContact:str2];

//    [p addContact:str3];

//    

     //NSMutableArray *b =[p getSortedNameOfkey:@"y"];

    //NSMutableArray *b = [p serchContactWithPhone:@"15802445035"];

    

//    NSMutableArray *b = [p getSortedContactOfAge];

    //[p deleteByName:@"yw"];

   // [p deleteByKey:@"y"];

    //for (Contact *c in b) {

      //  NSLog(@"%@ %@ %@ %@ %@ %ld", c.name, c.sex, c.phone, c.adress, c.group, c.age);

    //}

    //[p info];

    //    实现中等难度通讯录。需求:

    //    1、定义联系人类Contact。实例变量:姓名(拼音,首字母大写)、性别、电话号码、住址、分组名称、年龄。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息

    //    2、在main.m中定义字典,分组管理所有联系人。分组名为26个大写的英文字母。

    //    3、可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。

    //    4、获取某个分组名称下所有联系人,并且按照姓名升序排列。

    //    5、从通讯录中根据电话号码搜索联系人。

    //    6、获取所有女性的联系人,并且按照年龄的降序排列。

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

    //    8、删除某个分组的全部联系人。

    //    处理工程文件crayons.txt中的文本信息,文本内容是关于颜色的,每行都是一个颜色的信息,例如:Almond #EED9C4,前一个字符串是颜色的名称,后一个字符串是颜色的16进制色值,处理文本完成如下需求

    //    1、使用字典管理所有的颜色,即字典中存储的是多个键值对,颜色名称为key16进制颜色值(不带#)是value

    //    2、取出所有的key,升序排列。

    //    3、取出所有的value,按照排序后的key排列。

    //    4、使用一个新的字典管理颜色,对颜色进行分类管理,即:“A”“B”“C”…即这个字典包含多个键值对,key26个字母,value是数组,数组里存放的是Color对象(包含namecolorValue)。需要自己创建Color

    return 0;

}

//

//  Color.h

//  3道比较重要的作业

//

//  Created by dllo on 15/10/31.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Color : NSObject

@property (nonatomic, copy)NSString *name;

@property (nonatomic, copy)NSString *colorValue;

- (id)initWithName:(NSString *)name colorValue:(NSString *)colorValue;

@end


//

//  Color.m

//  3道比较重要的作业

//

//  Created by dllo on 15/10/31.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "Color.h"


@implementation Color

- (id)initWithName:(NSString *)name colorValue:(NSString *)colorValue

{

    self = [super init];

    if (self) {

        [self setName:name];

        [self setColorValue:colorValue];

      


    }

    return self;

}

@end


//

//  ContactManager.h

//  3道比较重要的作业

//

//  Created by dllo on 15/10/30.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>

@class Contact;

@interface ContactManager : NSObject

@property (nonatomic, retain) NSMutableDictionary *dataDic;

- (id)init;

- (void)info;

//可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。

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

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

- (NSMutableArray *)getSortedNameOfkey:(NSString *)key;

 //5、从通讯录中根据电话号码搜索联系人。

- (NSMutableArray *)serchContactWithPhone:(NSString *)phone;

//、获取所有女性的联系人,并且按照年龄的降序排列

- (NSMutableArray *)getSortedContactOfAge;

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

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

// 8、删除某个分组的全部联系人。

- (void)deleteByKey:(NSString *)key;


@end


//

//  ContactManager.m

//  3道比较重要的作业

//

//  Created by dllo on 15/10/30.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "ContactManager.h"

#import "Contact.h"

@implementation ContactManager

- (id)init

{

    self = [super init];

    if (self) {

        self.dataDic = [NSMutableDictionary dictionary];

    }

    return self;

}

//可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。

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

{

    if (0 == contact.name.length ) {

        

        return;

    }

    NSString *key = [contact.name substringToIndex:1] ;

    NSMutableArray *arr =  nil;

    if ([self.dataDic objectForKey:key] == nil) {

     //  arr = [NSMutableArray array];

        arr = [NSMutableArray array];

        [self.dataDic setObject:arr forKey:key];

        

        

    } else {

        arr = [self.dataDic objectForKey:key];

    }

    [arr addObject:contact];

    

}



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

- (NSMutableArray *)getSortedNameOfkey:(NSString *)key

{

    //判断参数有效性

    if (key.length == 0) {

        return nil;

    }

    NSMutableArray *arr = [self.dataDic objectForKey:key];

    [arr sortUsingSelector:@selector(compareByNameOfContact:)];

    return arr;

    

}

//根据电话号码查找联系人

- (NSMutableArray *)serchContactWithPhone:(NSString *)phone

{

    if (phone.length == 0) {

        return nil;

    }

    NSMutableArray *arr = [NSMutableArray array];

    for (NSString *key in self.dataDic) {

        for (Contact *contact in [self.dataDic objectForKey:key]) {

            if ([contact.phone isEqualToString:phone]) {

                //若电话匹配,则添加进数组中去

                //防止不同联系人得电话相同

                [arr addObject:contact];

            }

        }

    }

    return arr;

}

//、获取所有女性的联系人,并且按照年龄的降序排列

- (NSMutableArray *)getSortedContactOfAge

{

    

    NSMutableArray *arr = [NSMutableArray array];

    for (NSString *key in self.dataDic) {

        for (Contact *contact in [self.dataDic objectForKey:key]) {

            if ([contact.sex isEqualToString:@""]) {

                [arr addObject:contact];

            }

        }

    }

    [arr sortUsingSelector:@selector(compareByAgeOfContact:)];

    return arr;

}

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

-(void)deleteByName:(NSString *)name

{

    if (name.length == 0) {

        return ;

    }

   //根据名字获取key

    NSString *key = [name substringToIndex:1];

    NSMutableArray  *arr = [self.dataDic objectForKey:key];

    for (NSInteger i = 0; i < arr.count; i++) {

        if ([[[arr objectAtIndex:i] name] isEqualToString:name]) {

            [arr removeObjectAtIndex:i];

            i--;

        }

    }

}

// 8、删除某个分组的全部联系人。

- (void)deleteByKey:(NSString *)key

{

    if (key.length == 0) {

        return;

    }

    [self.dataDic removeObjectForKey:key];

}


//输出

- (void)info

{

    for (NSString *key in self.dataDic) {

        for (Contact *contact in  [self.dataDic objectForKey:key]) {

            NSLog(@"%@ %@ %@ %@ %@ %ld", contact.name, contact.sex, contact.phone,contact.adress, contact.group, contact.age);

        }

    }

}

@end


//

//  ContactManager.m

//  3道比较重要的作业

//

//  Created by dllo on 15/10/30.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "ContactManager.h"

#import "Contact.h"

@implementation ContactManager

- (id)init

{

    self = [super init];

    if (self) {

        self.dataDic = [NSMutableDictionary dictionary];

    }

    return self;

}

//可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。

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

{

    if (0 == contact.name.length ) {

        

        return;

    }

    NSString *key = [contact.name substringToIndex:1] ;

    NSMutableArray *arr =  nil;

    if ([self.dataDic objectForKey:key] == nil) {

     //  arr = [NSMutableArray array];

        arr = [NSMutableArray array];

        [self.dataDic setObject:arr forKey:key];

        

        

    } else {

        arr = [self.dataDic objectForKey:key];

    }

    [arr addObject:contact];

    

}



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

- (NSMutableArray *)getSortedNameOfkey:(NSString *)key

{

    //判断参数有效性

    if (key.length == 0) {

        return nil;

    }

    NSMutableArray *arr = [self.dataDic objectForKey:key];

    [arr sortUsingSelector:@selector(compareByNameOfContact:)];

    return arr;

    

}

//根据电话号码查找联系人

- (NSMutableArray *)serchContactWithPhone:(NSString *)phone

{

    if (phone.length == 0) {

        return nil;

    }

    NSMutableArray *arr = [NSMutableArray array];

    for (NSString *key in self.dataDic) {

        for (Contact *contact in [self.dataDic objectForKey:key]) {

            if ([contact.phone isEqualToString:phone]) {

                //若电话匹配,则添加进数组中去

                //防止不同联系人得电话相同

                [arr addObject:contact];

            }

        }

    }

    return arr;

}

//、获取所有女性的联系人,并且按照年龄的降序排列

- (NSMutableArray *)getSortedContactOfAge

{

    

    NSMutableArray *arr = [NSMutableArray array];

    for (NSString *key in self.dataDic) {

        for (Contact *contact in [self.dataDic objectForKey:key]) {

            if ([contact.sex isEqualToString:@""]) {

                [arr addObject:contact];

            }

        }

    }

    [arr sortUsingSelector:@selector(compareByAgeOfContact:)];

    return arr;

}

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

-(void)deleteByName:(NSString *)name

{

    if (name.length == 0) {

        return ;

    }

   //根据名字获取key

    NSString *key = [name substringToIndex:1];

    NSMutableArray  *arr = [self.dataDic objectForKey:key];

    for (NSInteger i = 0; i < arr.count; i++) {

        if ([[[arr objectAtIndex:i] name] isEqualToString:name]) {

            [arr removeObjectAtIndex:i];

            i--;

        }

    }

}

// 8、删除某个分组的全部联系人。

- (void)deleteByKey:(NSString *)key

{

    if (key.length == 0) {

        return;

    }

    [self.dataDic removeObjectForKey:key];

}


//输出

- (void)info

{

    for (NSString *key in self.dataDic) {

        for (Contact *contact in  [self.dataDic objectForKey:key]) {

            NSLog(@"%@ %@ %@ %@ %@ %ld", contact.name, contact.sex, contact.phone,contact.adress, contact.group, contact.age);

        }

    }

}

@end


//

//  Contact.h

//  3道比较重要的作业

//

//  Created by dllo on 15/10/30.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>

@class ContactManager;

@interface Contact : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *sex;

@property (nonatomic, copy) NSString *phone;

@property (nonatomic, copy) NSString *adress;

@property (nonatomic, copy) NSString *group;

@property (nonatomic, assign)NSInteger age;

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

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

- (id)initWithName: (NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address group:(NSString *)group age:(NSInteger)age;

+(id)contactWithName: (NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address group:(NSString *)group age:(NSInteger)age;

@end


//

//  Contact.m

//  3道比较重要的作业

//

//  Created by dllo on 15/10/30.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "Contact.h"


@implementation Contact

- (NSComparisonResult)compareByNameOfContact:(Contact *)contact

{

    return [self.name compare:contact.name];

}

- (NSComparisonResult)compareByAgeOfContact:(Contact *)contact

{

    if (self.age < contact.age) {

        return NSOrderedDescending;

        }

    if (self.age > contact.age) {

        return NSOrderedAscending;

    }

    return  NSOrderedSame;

}

- (id)initWithName: (NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address group:(NSString *)group age:(NSInteger)age

{

    self = [super init];

    if (self) {

        [self setName:name];

        [self setSex:sex];

        [self setPhone:phone];

        [self setAdress:address];

        [self setGroup:group];

        [self setAge:age];

    }

    return self;

}

+(id)contactWithName: (NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address group:(NSString *)group age:(NSInteger)age

{

    Contact *p = [[Contact alloc]initWithName:name sex:sex phone:phone address:address group:group age:age];

    return p;

}

@end


//

//  Contact.m

//  3道比较重要的作业

//

//  Created by dllo on 15/10/30.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "Contact.h"


@implementation Contact

- (NSComparisonResult)compareByNameOfContact:(Contact *)contact

{

    return [self.name compare:contact.name];

}

- (NSComparisonResult)compareByAgeOfContact:(Contact *)contact

{

    if (self.age < contact.age) {

        return NSOrderedDescending;

        }

    if (self.age > contact.age) {

        return NSOrderedAscending;

    }

    return  NSOrderedSame;

}

- (id)initWithName: (NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address group:(NSString *)group age:(NSInteger)age

{

    self = [super init];

    if (self) {

        [self setName:name];

        [self setSex:sex];

        [self setPhone:phone];

        [self setAdress:address];

        [self setGroup:group];

        [self setAge:age];

    }

    return self;

}

+(id)contactWithName: (NSString *)name sex:(NSString *)sex phone:(NSString *)phone address:(NSString *)address group:(NSString *)group age:(NSInteger)age

{

    Contact *p = [[Contact alloc]initWithName:name sex:sex phone:phone address:address group:group age:age];

    return p;

}

@end


Almond #EED9C4

Antique Brass #C88A65

Apricot #FDD5B1

Aquamarine #71D9E2

Asparagus #7BA05B

Atomic Tangerine #FF9966

Banana Mania #FBE7B2

Beaver #926F5B

Bittersweet #FE6F5E

Black #000000

Blizzard Blue #A3E3ED

Blue #0066FF

Blue Bell #9999CC

Blue Green #0095B6

Blue Violet #6456B7

Brick Red #C62D42

Brink Pink #FB607F

Brown #AF593E

Burnt Orange #FF7034

Burnt Sienna #E97451

Cadet Blue #A9B2C3

Canary #FFFF99

Caribbean Green #00CC99

Carnation Pink #FFA6C9

Cerise #DA3287

Cerulean #02A4D3

Chartreuse #FF9966

Chestnut #B94E48

Copper #DA8A67

Cornflower #93CCEA

Cotton Candy #FFB7D5

Cranberry #DB5079

Dandelion #FED85D

Denim #1560BD

Desert Sand #EDC9AF

Eggplant #614051

Electric Lime #CCFF00

Fern #63B76C

Flesh #FFCBA4

Forest Green #5FA777

Fuchsia #C154C1

Fuzzy Wuzzy Brown #C45655

Gold #E6BE8A

Goldenrod #FCD667

Granny Smith Apple #9DE093

Gray #8B8680

Green #01A368

Green Yellow #F1E788

Happy Ever After #6CDA37

Hot Magenta #FF00CC

Inch Worm #B0E313

Indian Red #B94E48

Indigo #4F69C6

Jazzberry Jam #A50B5E

Jungle Green #29AB87

Laser Lemon #FFFF66

Lavender #FBAED2

Macaroni And Cheese #FFB97B

Magenta #F653A6

Magic Mint #AAF0D1

Mahogany #CA3435

Manatee #8D90A1

Mango Tango #E77200

Maroon #C32148

Mauvelous #F091A9

Melon #FEBAAD

Midnight Blue #003366

Mountain Meadow #1AB385

Mulberry #C54B8C

Navy Blue #0066CC

Neon Carrot #FF9933

Olive Green #B5B35C

Orange #FF681F

Orchid #E29CD2

Outer Space #2D383A

Outrageous Orange #FF6037

Pacific Blue #009DC4

Peach #FFCBA4

Periwinkle #C3CDE6

Pig Pink #FDD7E4

Pine Green #01796F

Pink Flamingo #FF66FF

Plum #843179

Prussian Blue #003366

Purple Heart #652DC1

Purple Mountain's Majesty #9678B6

Purple Pizzazz #FF00CC

Radical Red #FF355E

Raw Sienna #D27D46

Razzle Dazzle Rose #FF33CC

Razzmatazz #E30B5C

Red #ED0A3F

Red Orange #FF3F34

Red Violet #BB3385

Robin's Egg Blue #00CCCC

Royal Purple #6B3FA0

Salmon #FF91A4

Scarlet #FD0E35

Screamin' Green #66FF66

Sea Green #93DFB8

Sepia #9E5B40

Shadow #837050

Shamrock #33CC99

Shocking Pink #FF6FFF

Silver #C9C0BB

Sky Blue #76D7EA

Spring Green #ECEBBD

Sunglow #FFCC33

Sunset Orange #FE4C40

Tan #FA9D5A

Tickle Me Pink #FC80A5

Timberwolf #D9D6CF

Torch Red #FD0E35

Tropical Rain Forest #00755E

Tumbleweed #DEA681

Turquoise Blue #6CDAE7

Ultra Green #66FF66

Ultra Orange #FF6037

Ultra Pink #FF6FFF

Ultra Red #FD5B78

Ultra Yellow #FFFF66

Unmellow Yellow #FFFF66

Violet (purple) #8359A3

Violet Red #F7468A

Vivid Tangerine #FF9980

Vivid Violet #803790

White #FFFFFF

Wild Blue Yonder #7A89B8

Wild Strawberry #FF3399

Wild Watermelon #FD5B78

Wisteria #C9A0DC

Yellow #FBE870

Yellow Green #C5E17A

Yellow Orange #FFAE42


posted @ 2015-11-08 14:15  挽月细数风流  阅读(258)  评论(0编辑  收藏  举报