//
#include "stdafx.h"
#include "StudentData.h"
int _tmain(int argc, _TCHAR* argv[])
{
StudentDataSet pStudent ;
pStudent.Input( pStudent.getStudents(), N);
pStudent.Output( pStudent.getStudents() ,N);
pStudent.SortByScore(pStudent.getStudents() ,N);
printf("按成绩降序排列:\n");
pStudent.Output(pStudent.getStudents() ,N);
pStudent.PrintVale(pStudent.getStudents() ,N,Vale);
return 0;
}
#ifndef STUDENTDATA_H_
#define STUDENTDATA_H_
#include <stdio.h>
#define N 2
#define Vale 90
struct STUDENT
{
int num;
char name[20];
char sex;
float score;
};
class StudentDataSet
{
public:
StudentDataSet();
~StudentDataSet();
public:
void Input(STUDENT *s, int n);
void Output(STUDENT *s, int n);
void SortByScore( STUDENT *s, int n);
void PrintVale(STUDENT *s,int n,int m);
void Swap( STUDENT& a, STUDENT& b );
void Swap( int* x, int* y );
void Swap( int& x, int& y );
STUDENT* getStudents();
private:
STUDENT s[N];
};
inline STUDENT* StudentDataSet::getStudents()
{
return s;
}
#endif
#include "StudentData.h"
StudentDataSet::StudentDataSet()
{
}
StudentDataSet::~StudentDataSet()
{
}
void StudentDataSet::Input( STUDENT* s, int n )
{
printf("请输入%d个学生\n",n);
for(int i = 0;i < n; i++ )
{
scanf("%d %s %c %f", &s[i].num,s[i].name,&s[i].sex,&s[i].score);
}
}
void StudentDataSet::Output( STUDENT* s, int n )
{
for(int i=0;i < n;i++ )
printf(" %d \t%s\t%c\t %4.2f\n",s[i].num,s[i].name, s[i].sex,s[i].score);
}
void StudentDataSet::Swap( STUDENT& a, STUDENT& b )
{
STUDENT temp;
temp = a;
a = b;
b = temp;
}
void StudentDataSet::Swap( int* x, int* y )
{
int temp = (*x);
(*x) = (*y);
*y = temp;
}
void StudentDataSet::Swap( int& x, int& y )
{
int temp = x;
x = y;
y = temp;
}
void StudentDataSet::SortByScore( STUDENT *s, int n )
{
bool isSorted = false;
for(int i=0;i < n && !isSorted;i++)
{
isSorted = true ;
for(int j = 0;j < n-i -1;j++ )
{
if(s[j].score < s[j+1].score)
{
isSorted = false;
Swap(s[j],s[j+1]);
}
}
}
}
void StudentDataSet::PrintVale( STUDENT *s,int n,int m )
{
for (int i = 0; i < n; i++ )
{
if (s[i].score > m)
{
printf("%d\t%s\t%c\t%f\n",s[i].num,s[i].name,s[i].sex,s[i].score);
}
}
}