黑马程序员技术交流社区
标题: OC编程问题 [打印本页]
作者: Lxy 时间: 2014-10-16 21:24
标题: OC编程问题
5.利用前面的成绩类,重新设计一个学生类
1> 属性
* 姓名
* 学号
* 成绩(包括3科成绩)
2> 行为
* 比较C语言成绩:跟另外一个学生比较C语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较OC成绩:跟另外一个学生比较OC语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较iOS成绩:跟另外一个学生比较iOS语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较总分:跟另外一个学生比较总分,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较平均分:跟另外一个学生比较平均分,返回成绩差(自己的成绩 - 其他人的成绩)
*/
#import <Foundation/Foundation.h>
@interface Score : NSObject
{
@public
int CScore;
int OCScore;
int IOSScore;
}
@end
@implementation Score
@end
@interface Student : NSObject
{
@public
char* name;
char* number;
Score* score;
}
- (int) compareCScoreWithOther:(Student*) student;
@end
@implementation Student
- (int) compareCScoreWithOther:(Student*) student
{
return score->CScore - student->score->CScore;
}
@end
int main()
{
Student * stu1 = [Student new];
Student * stu2 = [Student new];
stu1->score->CScore = 100;
stu2->score->CScore = 200;
int a = [stu1 compareCScoreWithOther:stu2];
NSLog(@"%d",a);
return 0;
}
这是我写的,编译连接可以,但是运行时提示:Segmentation fault: 11
求大神指点!
作者: Lxy 时间: 2014-10-16 21:42
问题已解决!
#import <Foundation/Foundation.h>
@interface Score : NSObject
{
@public
int CScore;
int OCScore;
int IOSScore;
}
- (int) compareCScoreWithOther:(Score *) score;
@end
@implementation Score
- (int) compareCScoreWithOther:(Score *) score
{
return CScore - score->CScore;
}
@end
@interface Student : NSObject
{
@public
char* name;
char* number;
Score* score;
}
- (int) compareCScoreWithOther:(Student*) student;
@end
@implementation Student
- (int) compareCScoreWithOther:(Student*) student
{
return [score compareCScoreWithOther:student->score];
}
@end
int main()
{
Student * stu1 = [Student new];
Student * stu2 = [Student new];
Score * score1 = [Score new];
Score * score2 = [Score new];
score1->CScore = 100;
score2->CScore = 200;
stu1->score = score1;
stu2->score = score2;
//stu1->score->CScore = 100;//第二层是结构体可以,如果是对象,好像只能间接赋值,如上四句
//stu2->score->CScore = 200;
int a = [stu1 compareCScoreWithOther:stu2];
NSLog(@"%d",a);
return 0;
}
作者: Lxy 时间: 2014-10-16 21:43
怎么会有表情符号!:(
作者: Lxy 时间: 2014-10-16 21:59
完善版#import <Foundation/Foundation.h>
@interface Score : NSObject
{
@public
int CScore;
int OCScore;
int IOSScore;
}
- (int) compareCScoreWithOther:(Score *) score;
- (int) compareSumScoreWithOther:(Score *)score;
@end
@implementation Score
- (int) compareCScoreWithOther:(Score *) score
{
return CScore - score->CScore;
}
- (int) compareSumScoreWithOther:(Score *)score
{
int sum1 = CScore + OCScore + IOSScore;
int sum2 = score->CScore + score->OCScore + score->IOSScore;
return sum1 - sum2;
}
@end
@interface Student : NSObject
{
@public
char* name;
char* number;
Score* score;
}
- (int) compareCScoreWithOther:(Student*) student;
- (int) compareSumScoreWithOther:(Student*) student;
@end
@implementation Student
- (int) compareCScoreWithOther:(Student*) student
{
return [score compareCScoreWithOther:student->score];
}
- (int) compareSumScoreWithOther:(Student*) student
{
return [score compareSumScoreWithOther:student->score];
}
@end
int main()
{
Student * stu1 = [Student new];
Student * stu2 = [Student new];
Score * score1 = [Score new];
Score * score2 = [Score new];
score1->CScore = 100;
score2->CScore = 200;
stu1->score = score1;
stu2->score = score2;
//stu1->score->CScore = 100;//第二层是结构体可以,如果是对象,好像只能间接赋值,如上
//stu2->score->CScore = 200;
int a = [stu1 compareCScoreWithOther:stu2];
int b = [stu1 compareSumScoreWithOther:stu2];
NSLog(@"%d",a);
return 0;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |