C++学生信息处理
2016-03-02 17:29
351 查看
#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;
}
#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;
}
相关文章推荐
- 【C++函数重载】求3个数中最大的数…
- 【c++】用c++编写的求任意区间的素…
- 构造函数
- #include<bits/stdc++.h>
- C++中ShallowCopy和DeepCopy的区别
- VC++中窄字节、宽字节、多字节
- 项目开发中值得推荐的C/C++框架和库总结
- 第1章 软件工程基本原理和C++类
- C++调用lib或dll文件
- 【C语言】输入一组整数,求出这组数字子序列和中最大值
- 黑马程序员-C语言回顾-指针
- 类(Classes)的定义与实现
- Effective C++ 笔记:4设计与声明
- C语言学习
- C++中字符串字面值和字符相加
- c语言:用递归方法求n的阶乘
- C语言表达式和运算符(三)
- Effective c++ 读书笔记
- Find the Kth number c++ version
- C++中在容器Vector中使用结构体Struct