/*
题目:
学生成绩排序:成绩从低到高,姓名字母从低到高(区分大小写),年龄从低到高排序
输入:
3
abc 20 99
bcd 19 97
bed 20 97
输出:
bcd 19 97
bed 20 97
abc 20 99
易错点:
1对于字符指针,scanf("%s",ps[i].sName)不需要取地址符
2对于名字,要指定大小,否则内存访问非法,char sName[128];而不是定义char* sName;
3int strcmp(const char* string1,const char* string2); <0:前<后
得分:0
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct Student
{
//Student(char* name,int age,int grade):sName(name),iAge(age),iGrade(grade){}
//char* sName;//就是这里出错了,没有具体制定多大
//char* sName;
//法二,用重载操作符operator<的方法做
bool operator < (const Student& stu) const
{
if(iGrade != stu.iGrade)
{
return iGrade < stu.iGrade;
}
else
{
if(sName != stu.sName)
{
return strcmp(sName,stu.sName) < 0 ? true:false;
}
else
{
return iAge < stu.iAge;
}
}
}
char sName[128];
int iAge;
int iGrade;
}
机试算法讲解:第2题 结构体之快速排序
最新推荐文章于 2023-10-13 12:20:59 发布