C++学生信息处理

#include
#include
using namespace std;
template
int getArrayLen(T& array) //使用模板定义一个函数getArrayLen,该函数将返回数组array的长度
{
return (sizeof(array) / sizeof(array[0]));
}

class Student{
public:
char name[20];
int id;
int score;
public:
Student(int i = 0, char* c="", int s= 0)
{
id = i;
strcpy(name,c);
score = s;
}
Student& operator = (const Student& rhs )
{
id = rhs.id;
strcpy(name,rhs.name);
score = rhs.score;
return *this;
}
void gets()
{
cin>>name>>id>>score;
}
void print()
{
cout<<"name:\t"<<name<<"\tid:\t"<<id<<"\tscore:\t"<<score<<endl;
}
} ;

void rank(int n,Student a[])//排序函数
{
int i, j; Student t;
for(i = 0 ; i < n ; i++ )
for(j = i + 1; j < n -1 ; j ++)
{
if(a[i].score < a[j].score){t = a[i]; a[i] = a[j] ;a[j] = t;}
}
}

int average(Student a[],int n)//求平均数
{
int sum = 0,average;
for(int i = 0 ; i < n ; i++)
sum+=a[i].score;
average = sum / n;
return average;
}
void input(Student s[],int n)//输入信息的函数
for(int i = 0 ; i < n ; i++)
   s[i].gets();
}
void output(Student s[],int n)//输出信息的函数
for(int i = 0 ; i < n ; i++)
      s[i].print();
}

//主函数
int main()
{
Student class1[10];//十个人的信息
cout<<"******************************************"<<endl;
cout<<"please enter name id score:"<<endl;
input(class1,10);
cout<<"name              id             score  :"<<endl;
cout<<"******************************************"<<endl;
output(class1,10);
cout<<"******************************************"<<endl;
cout<<"this class average score is:"<<average(class1,4)<<endl;
cout<<"******************************************"<<endl;
    rank(10,class1);
output(class1,4);//输出前四的信息
return 0;
}
posted @ 2014-11-05 19:43  whieenz  阅读(386)  评论(0编辑  收藏  举报