急!!!结构体 qsort

#include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
int score ;
int age;
}s[100];

//按照x从小到大排序,当x相等时按照y从大到小排序
void main()
{
student A,B,C,D,E;
A.score=100;
B.score=93;
C.score=98;
D.score=95;
E.score=96;

A.age=20;
B.age=23;
C.age=19;
D.age=21;
E.age=22;
qsort(s,100,sizeof(s[0]),cmp);

}

int cmp( const void *a , const void *b )
{
struct student *c=(student*)a;
struct student *d = (student*)b;
if(c->score != d->score) return c->score - d->score;
else return d->age - c->age;
}

#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
int score ;
int age;
}s[100];

int cmp( const void* a,const void *b);
//按照x从小到大排序,当x相等时按照y从大到小排序
void main()
{
student A,B,C,D,E;
A.score=100;
B.score=93;
C.score=98;
D.score=95;
E.score=96;

A.age=20;
B.age=23;
C.age=19;
D.age=21;
E.age=22;
qsort(s,100,sizeof(s[0]),cmp);

}

int cmp( const void *a , const void *b )
{
struct student *c=(student*)a;
struct student *d = (student*)b;
if(c->score != d->score) return c->score - d->score;
else return d->age - c->age;
}
不知道你问的是什么。。。既然是c++放着sort不用用qsort,实在是有意思。
温馨提示:答案为网友推荐,仅供参考
相似回答