[关闭]
@fiy-fish 2015-07-18T19:16:29.000000Z 字数 3285 阅读 1643

day06-02-电话本

Objective-c


  1. // day06-02-电话本
  2. //
  3. // Created by Aaron on 15/7/8.
  4. // Copyright (c) 2015年 Aaron. All rights reserved.
  5. //
  6. #import <Foundation/Foundation.h>
  7. #import "PhoneBook.h"
  8. int main(int argc, const char * argv[]) {
  9. @autoreleasepool {
  10. PhoneBook *book = [[PhoneBook alloc] init];
  11. for(int i = 0; i < 5; i++)
  12. {
  13. if(i%2)
  14. {
  15. Person *p = [[Person alloc] initWithName:@"laowang" withAddr:@"..." withCompany:@"company" withPhone:@"110"];
  16. [book addPerson:p];
  17. }
  18. else
  19. {
  20. Person *p = [[Person alloc] initWithName:@"gebi" withAddr:@"..." withCompany:@"company" withPhone:@"110"];
  21. [book addPerson:p];
  22. }
  23. }
  24. [book showInfo];
  25. //NSLog(@"%@",[book searchByName:@"gebi"]);
  26. }
  27. return 0;
  28. }

  1. #import <Foundation/Foundation.h>
  2. /*
  3. 设计一个电话本,人物信息包含姓名,地址,电话,公司
  4. 需要实现功能:按姓名自动排序
  5. 指定姓名查找
  6. 指定公司查找
  7. 人物可重名
  8. 显示所有人物信息
  9. //姓名模糊查询
  10. */
  11. @interface Person : NSObject
  12. {
  13. NSString *_name;
  14. NSString *_addr;
  15. NSString *_company;
  16. NSString *_phone;
  17. }
  18. -(instancetype)initWithName:(NSString *)name withAddr:(NSString *)addr withCompany:(NSString *)company withPhone:(NSString *)phone;
  19. -(NSString *)name;
  20. -(NSString *)addr;
  21. -(NSString *)company;
  22. -(NSString *)phone;
  23. @end

  1. #import "Person.h"
  2. @implementation Person
  3. -(instancetype)initWithName:(NSString *)name withAddr:(NSString *)addr withCompany:(NSString *)company withPhone:(NSString *)phone
  4. {
  5. if(self = [super init])
  6. {
  7. _name = name;
  8. _addr = addr;
  9. _company = company;
  10. _phone = phone;
  11. }
  12. return self;
  13. }
  14. -(NSString *)name
  15. {
  16. return _name;
  17. }
  18. -(NSString *)addr
  19. {
  20. return _addr;
  21. }
  22. -(NSString *)company
  23. {
  24. return _company;
  25. }
  26. -(NSString *)phone
  27. {
  28. return _phone;
  29. }
  30. @end

  1. /*
  2. 设计一个电话本,人物信息包含姓名,地址,电话,公司
  3. 需要实现功能:按姓名自动排序
  4. 指定姓名查找
  5. 指定公司查找
  6. 人物可重名
  7. 显示所有人物信息
  8. //姓名模糊查询
  9. */
  10. #import "Person.h"
  11. @interface PhoneBook : NSObject
  12. {
  13. //姓名和id关联
  14. NSMutableDictionary *_nameAndIdDic;
  15. //人和id关联
  16. NSMutableDictionary *_personAndIdDic;
  17. //公司和id关联
  18. NSMutableDictionary *_companyAndIdDic;
  19. //记录最大的id
  20. NSInteger _maxId;
  21. //添加一个数组
  22. NSMutableArray *_personArray;
  23. }
  24. //添加人
  25. -(void)addPerson:(Person *)p;
  26. //按姓名查找
  27. -(NSArray *)searchByName:(NSString *)name;
  28. //按公司查找
  29. -(NSArray *)searchByCompany:(NSString *)company;
  30. //测试
  31. -(void)showInfo;
  32. @end

  1. #import "PhoneBook.h"
  2. @implementation PhoneBook
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. _nameAndIdDic = [NSMutableDictionary dictionary];
  8. _personAndIdDic = [NSMutableDictionary dictionary];
  9. _companyAndIdDic = [NSMutableDictionary dictionary];
  10. _personArray = [NSMutableArray array];
  11. _maxId = 0;
  12. }
  13. return self;
  14. }
  15. -(void)addPerson:(Person *)p
  16. {
  17. //计算ID
  18. NSNumber *currentID = [NSNumber numberWithInteger:_maxId++];
  19. //将名字和id关联
  20. [_nameAndIdDic setObject:[p name] forKey:currentID];
  21. //将人和id关联
  22. [_personAndIdDic setObject:p forKey:currentID];
  23. //将公司和id关联
  24. [_companyAndIdDic setObject:[p company] forKey:currentID];
  25. //将人添加到数组
  26. [self insertPerson:p];
  27. }
  28. -(void)insertPerson:(Person *)p
  29. {
  30. if([_personArray count] == 0)
  31. {
  32. [_personArray addObject:p];
  33. }
  34. else
  35. {
  36. for(int i = 0; i < [_personArray count]; i++)
  37. {
  38. Person *obj1 = _personArray[i];
  39. if([[p name] compare:[obj1 name]] == NSOrderedAscending)
  40. {
  41. [_personArray insertObject:p atIndex:i];
  42. break;
  43. }
  44. if(i == [_personArray count]-1)
  45. {
  46. [_personArray addObject:p];
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. -(NSArray *)searchByCompany:(NSString *)company
  53. {
  54. return [self searchByString:company inDic:_companyAndIdDic];
  55. }
  56. -(NSArray *)searchByName:(NSString *)name
  57. {
  58. return [self searchByString:name inDic:_nameAndIdDic];
  59. }
  60. -(NSArray *)searchByString:(NSString *)str inDic:(NSDictionary *)dic
  61. {
  62. NSMutableArray *array = [NSMutableArray array];
  63. //找名字对应的所有id
  64. NSArray *allIds = [dic allKeysForObject:str];
  65. for(id obj in allIds)
  66. {
  67. Person *p = [_personAndIdDic objectForKey:obj];
  68. [array addObject:p];
  69. }
  70. return array;
  71. }
  72. -(void)showInfo
  73. {
  74. for(Person *obj in _personArray)
  75. {
  76. //value = 字典[key]
  77. NSLog(@"%@",[obj name]);
  78. }
  79. }
  80. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注