OC强化考试编程题答案

第一题

给NSString 增加个分类,获取字符串http://www.itcast.cn中的itcast
开始下标与结束下标的位置(字符串:)
NSString+GetIndex.h

#ifndef NSString_GetIndex_h
#define NSString_GetIndex_h
#import <Foundation/Foundation.h>

@interface NSString(GetIndex)
+(int)GetITCastStart;
+(int)GetITCastEnd;
@end

#endif /* NSString_GetIndex_h */

NSString+GetIndex.m

#import <Foundation/Foundation.h>
#import "NSString+GetIndex.h"

@implementation NSString(GetIndex)

+(int)GetITCastStart
{
    NSString *fullStr = @"http://www.itcast.cn";
    NSString *itcastStr = @"itcast";
    NSRange range = [fullStr rangeOfString:itcastStr];
    return (int)range.location;
}

+(int)GetITCastEnd
{
    NSString *fullStr = @"http://www.itcast.cn";
    NSString *itcastStr = @"itcast";
    NSRange range = [fullStr rangeOfString:itcastStr];
    return (int)(range.location+range.length-1);
}

@end

main.m

#import <Foundation/Foundation.h>
#import "NSString+GetIndex.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...

        int start = [NSString GetITCastStart];
        int end = [NSString GetITCastEnd];
         NSLog(@"itcast start=%d,end=%d",start,end);
    }
    return 0;
}

第2题

设计一个形状类,提供(计算、显示)面积和周长的方法,设计一个长方形类和正方形类 继承形状类,并给长方形类提供长度和宽度成员属性,给正方形类提供边长属性,程序运行 输出长方形和正方形的面积和周长。
ShapeWithProtocol.h

#ifndef ShapeWithProtocol_h
#define ShapeWithProtocol_h

#import <Foundation/Foundation.h>
@protocol ShapeProtocol<NSObject>

//默认是必须实现的
//必须实现
@required
- (int)GetArea;
- (int)getCircle;
@end

#endif /* ShapeWithProtocol_h */

rectangle_h

#ifndef rectangle_h
#define rectangle_h

#import <Foundation/Foundation.h>

#import "ShapeWithProtocol.h"

@interface CZRectangle:NSObject<ShapeProtocol>

@property(nonatomic,assign) int length;

@property(nonatomic,assign) int weight;

-(NSString *)description;

@end

#endif /* rectangle_h */

rectangle.m

#import <Foundation/Foundation.h>
#import "rectangle.h"

@implementation CZRectangle


#pragma mark - ShapeWithProtocol

- (int)GetArea
{
    return _weight*_length;
}

-(int)getCircle
{
    return (self.weight+self.length)*2;
}

-(NSString *)description
{
    int area = [self GetArea];
    int circle = [self getCircle];

    NSString *str = [NSString
                     stringWithFormat:
                     @"CZRectangle area is %d,circle is %d",
                     area,
                     circle];
    return str;
}

@end

square.h

#ifndef square_h
#define square_h

#import <Foundation/Foundation.h>

#import "ShapeWithProtocol.h"

@interface CZSquare:NSObject<ShapeProtocol>

@property(nonatomic,assign) int length;

-(NSString *)description;

@end

#endif /* square_h */

square.m

#import <Foundation/Foundation.h>
#import "square.h"

@implementation CZSquare

#pragma mark - ShapeWithProtocol
- (int)GetArea
{
    return _length*_length;
}

-(int)getCircle
{
    return self.length*4;
}

-(NSString *)description
{
    int area = [self GetArea];
    int circle = [self getCircle];

    NSString *str = [NSString
                     stringWithFormat:
                     @"CZRectangle area is %d,circle is %d",
                     area,
                     circle];
    return str;
}


@end

main.m

#import <Foundation/Foundation.h>
#import "ShapeWithProtocol.h"
#import "rectangle.h"
#import "square.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...

        CZRectangle *rectangle = [[CZRectangle alloc]init];
        rectangle.length = 2;
        rectangle.weight = 1;
        NSLog(@"%@",rectangle);

        CZSquare *square =[[CZSquare alloc]init];
        square.length = 2;
         NSLog(@"%@",square);

    }
    return 0;
}

第三题

CZDate.h

#ifndef CZDate_h
#define CZDate_h

#import <Foundation/Foundation.h>
//获取当前日期是星期几
@interface CZDate : NSObject

- (NSString *) GetCurDate;

@end

#endif /* CZDate_h */

CZDate.m


#import <Foundation/Foundation.h>
#import "CZDate.h"

@implementation CZDate

- (NSString *) GetCurDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //      获取日期每一个部分
    NSDate *date = [NSDate date];
    NSDateComponents  *cmps = [calendar components:NSCalendarUnitWeekday fromDate:date];
    NSInteger nowWeek = cmps.weekday-1;
    NSString *dateStr = [NSString stringWithFormat:@"今天是星期%d",(int)nowWeek];
    return dateStr;
}
@end

main.m

#import <Foundation/Foundation.h>
#import "CZDate.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        CZDate *pDate = [[CZDate alloc] init];
        NSString *str = [pDate GetCurDate];
        NSLog(@"%@",str);
        return 0;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值