标签:objective-c 对象 类 黑马考试题 冒泡排序
题目:定义一个学生类,需要有姓名,年龄,考试成绩三个成员属性,创建5个对象,属性可以任意值。(Objective-C)
1) 不使用@property,手动编写他们的访问器方法(getter和setter),注意内存管理(手动管理内存)
2) 增加一个便利构造器(快速构造器)
3) 使用NSLog输出学生对象时,输出信息格式为:My Name Is XXX Age Is XXX Score Is XXX
4) 对5个学生对象按照成绩—》年龄—》姓名优先级排序(成绩相同按照年龄排序,成绩年龄相同按照姓名排序(5个学生的属性值自己随便设定,姓名不考虑中文,按26个大小字母排序))
这是一道黑马入学考试题,我做了3个小时,终于做好了这道题,涉及到了很多OC语言基础的知识点,由于使用的是xocde6.3.2,没有办法进行手动内存管理,所以省略了这一部分,供大家参考
代码:
#import <Foundation/Foundation.h> @interface Student : NSObject { //姓名 NSString* _name; //年龄 int _age; //成绩 int _score; } -(void)setName:(NSString*)name; -(void)setAge:(int)age; -(void)setScore:(int)score; -(NSString*)getName; -(int)getAge; -(int)getScore; +(BOOL)isNameHigher:(NSString *)n1 and:(NSString *)n2; //比较两个学生的排名,是否第一个学生比第二个学生排名靠前 +(BOOL)isHigher:(Student*) student1 and:(Student*)student2; -(id)initWithName:(NSString*)name andAge:(int)age andScore:(int)score; @end
#import "Student.h" @implementation Student -(void)setName:(NSString *)name{ _name = name; } -(void)setAge:(int)age{ _age = age; } -(void)setScore:(int)score{ _score = score; } -(NSString *)getName{ return _name; } -(int)getAge{ return _age; } -(int)getScore{ return _score; } -(id)initWithName:(NSString *)name andAge:(int)age andScore:(int)score{ if (self = [super init]) { _name = name; _age = age; _score = score; } return self; } -(NSString *)description{ // return [NSString stringWithFormat:@"%@ %d %d",_name,_age,_score]; return [NSString stringWithFormat:@"My name is %@ age is %d score is %d",_name,_age,_score]; } +(BOOL)isNameHigher:(NSString *)n1 and:(NSString *)n2{ unsigned long time; if ([n1 length]>[n2 length]) { time = [n2 length]; }else{ time = [n1 length]; } for (int i = 0; i<time; i++) { if ([n1 characterAtIndex:i]<[n2 characterAtIndex:i]) { return YES; }else if ([n1 characterAtIndex:i]>[n2 characterAtIndex:i]){ return NO; } } NSLog(@"所有条件都相同,根据姓名长度比较"); return [n1 length]<[n2 length]; } +(BOOL)isHigher:(Student*) student1 and:(Student*)student2{ //比较两个学生的分数,分数高的返回 if ([student1 getScore] > [student2 getScore]) { return YES; }else if ([student1 getScore] < [student2 getScore]){ return NO; }else {//如果两个学生的分数相同,比较两个学生的年龄,年龄低的返回 if ([student1 getAge] < [student2 getAge]) { return YES; }else if ([student1 getAge] > [student2 getAge]){ return NO; }else{ //如果两个学生的分数和年龄都相同,根据姓名(限制为英文)前端字母排序 NSString* n1 = [[student1 getName] lowercaseString]; NSString* n2 = [[student2 getName] lowercaseString]; if ([self isNameHigher:n1 and:n2]){ return YES; }else{ return NO; } } } } @end
#import <Foundation/Foundation.h> #import "Student.h" int main(){ Student* s1 = [[Student alloc]initWithName:@"Tim" andAge:11 andScore:79]; Student* s2 = [[Student alloc]initWithName:@"Calo" andAge:12 andScore:86]; Student* s3 = [[Student alloc]initWithName:@"QaJin" andAge:14 andScore:95]; Student* s4 = [[Student alloc]initWithName:@"Jaren" andAge:11 andScore:95]; Student* s5 = [[Student alloc]initWithName:@"Jare" andAge:11 andScore:95]; NSMutableArray* students = [NSMutableArray arrayWithObjects:s1,s2,s3,s4,s5, nil]; NSUInteger count = [students count]; //使用冒泡排序对可变集合(学生集合)进行排序 for (int i = 0; i<count; i++) { for (int j = 0; j<count; j++) { if (j<count-1 && ![Student isHigher:students[j] and:students[j+1]]) { Student* s =students[j]; [students setObject:students[j+1] atIndexedSubscript:j]; [students setObject:s atIndexedSubscript:j+1]; } } } NSLog(@"%@",students); return 0; }
【OC复合题】之定义一个学生类,需要有姓名,年龄,考试成绩三个成员属性,创建5个对象,属性可以任意值。(Objective-C)
标签:objective-c 对象 类 黑马考试题 冒泡排序
原文地址:http://blog.csdn.net/u011349387/article/details/46008287