A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Lxy 中级黑马   /  2014-10-16 21:24  /  1458 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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
求大神指点!

3 个回复

倒序浏览
Lxy 中级黑马 2014-10-16 21:42:06
沙发
问题已解决!

#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:44
藤椅
怎么会有表情符号!:(
回复 使用道具 举报
Lxy 中级黑马 2014-10-16 21:59:48
板凳
完善版
#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;
}


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马