【OC複合題】之定義一個學生類,需要有姓名,年齡,考試成績三個成員屬性,建立5個物件,屬性可以任意值。(Objective-C)
阿新 • • 發佈:2018-12-25
題目:
定義一個學生類,需要有姓名,年齡,考試成績三個成員屬性,建立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;
}