学生信息 类 结构

// Student.cpp : 定义控制台应用程序的入口点。
//


#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);
}
}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值