学生有10个(num,name,score[5])等信息,用类来编写程序,要求
能进行学生信息的初始化;
能实现10个学生数据的输入;
能自动计算学生的平均成绩[s, av];
可打印学生的上述信息;
找到成绩挂科的学生并输出其信息;
能求出每门课程的平均分数(静态数组sum[5],ave[5],count计数)
排序
删除3科以上不及格的学生;
添加
分析:
1.定义一个学生类,其中有num. name .score[5]. av. s. mc作为私有成员;
2.设计构造函数来初始化;
3.设计一个输入函数(输入num. name ,score[5])并计算静态数组sum[5]求和 和计算count 每个学生的s. av
4.打印函数,作为公有成员函数;
5.设计一个计算学生每门课平均分的函数,作为公有成员函数;
6.设计一个排列函数。作为公有成员函数;
7.设计一个函数来判断一个学生呢个是否挂科;
8.设计一个函数来判断一个学生呢个是否有3门以上成绩挂科
样本 :(在源程序上修改)
#include<iostream>
using namespace std;
class Student
{
public:
char xuehao[5];
char name[10];
int chengji;
int N;
int addr[30];
Student();
~Student();
Student(char * xuehao,char * name,int chengji);
bool AddStudent(char * xuehao,char * name,int chengji);
void DispAll();
Student * stu;
};
Student::Student()
{
xuehao[0]='0';
name[0]='0';
chengji=0;for(int i=0;i<30;i++)
{
addr=0;
}
N=0;
}
Student::Student(char * xuehao1,char * name1,int chengji1)
{
strcpy(xuehao,xuehao1);
strcpy(name,name1);
chengji=chengji1;
}
Student::~Student()
{
}
bool Student::AddStudent(char * xuehao,char * name,int chengji)
{
stu=new Student(xuehao,name,chengji);
addr[N]=(int)stu;
N++;
cout<<"学生成绩添加成功!"<<endl;
return true;
}
void Student::DispAll()
{
cout<<"学号"<<"\t姓名"<<"\t成绩"<<endl;
for(int i=0;i<N;i++)
{
stu=(Student*)addr;
cout<<stu->xuehao<<'\t'<<stu->name<<'\t'<<stu->chengji<<endl;
}
}
void main()
{
Student stu;
int select;
select=1;
char xuehao[5];
char name[10];
int chengji;
cout<<endl<<endl;
cout<<" -------------------------------------------------"<<endl;
cout<<" * *"<<endl;
cout<<" 欢迎进入学生成绩管理系统 "<<endl;
cout<<" * *"<<endl;
cout<<" -------------------------------------------------"<<endl;
while(select)
{
cout<<endl<<endl;
cout<<" 请选择您的操作:"<<endl<<endl;
cout<<" 1.录入学生成绩信息;"<<endl;
cout<<" 2.显示学生成绩信息;"<<endl;
cout<<" 0.退出;"<<endl;
cout<<" 请输入数字选项(0~2): ";
cin>>select;
if(select>=0&&select<=2)
{
switch(select)
{
case 1:
cout<<" 请输入学号:";
cin>>xuehao;
cout<<endl;
cout<<" 请输入学生姓名:";
cin>>name;
cout<<endl;
cout<<" 请输入成绩:";
cin>>chengji;
cout<<endl;
stu.AddStudent(xuehao,name,chengji);
break;
case 2:
cout<<" 所有成绩信息如下:"<<endl;
stu.DispAll();
break;
case 0:
break;
}
}
else
{
cout<<"输入错误,请重新输入!"<<endl;
break;
}
}
}
(这个可能用得到)
#include<iostream>using namespace std;int sum[3];float ave[3];class Student{ private: static int count; int num,score[3]; char name[10]; public: Student(int num,char*name,int score1,int score2,int score3); void printid(); void printCA(); int GetScore(int n); /* int Getsum(); int Getave();*/};int Student::count=0;int Student::GetScore(int n){return score[n];}Student::Student(int pnum,char*pname,int score1,int score2,int score3){ score[0]=score1; score[1]=score2; score[2]=score3; this->num=pnum; strcpy(name,pname); count++; sum[0]+=this->score[0]; sum[1]+=this->score[1]; sum[2]+=this->score[2]; ave[0]=sum[0]/count; ave[1]=sum[1]/count; ave[2]=sum[2]/count;}/*int Student::Getsum() { return sum[0]+sum[1]+sum[2]; }int Student::Getave() { return ave[0]+ave[1]+sum[2]; }*/void Student::printid(){ cout<<"学号:"<<num<<" "; cout<<"姓名:"<<name<<" "; cout<<"成绩1:"<<score[0]<<" "; cout<<"成绩2:"<<score[1]<<" "; cout<<"成绩3:"<<score[2]<<endl;}void Student::printCA(){for(int i=0;i<3;i++){cout<<"课程"<<i+1<<"总分:"<<sum<<endl;}for(int i=0;i<3;i++){cout<<"课程"<<i+1<<"平均分:"<<ave<<endl;}cout<<"学生数:"<<count<<endl;}int main(){Student stu[4]={Student(1001,"陈红",89,67,77),Student(1002,"张大山",78,85,76) ,Student(2001,"赵志勇",97,90,88),Student(2002,"李明全",91,94,89)};for(int i=0;i<4;i++)stu.printid();stu[0].printCA();int s1=0,s2=0,s3=0;int n1=0,n2=0,n3=0;/* for(i=0;i<4;i++){if(s1<stu.GetScore(0)){s1=stu.GetScore(0);n1=i;}if(s2<stu.GetScore(1)){s1=stu.GetScore(1);n2=i;}if(s3<stu.GetScore(2)){s3=stu.GetScore(2);n3=i;}}*/for(int i=0;i<4;i++){ if(s1<stu.GetScore(0)+stu.GetScore(1)+stu.GetScore(2)) { s1=stu.GetScore(0)+stu.GetScore(1)+stu.GetScore(2);n1=i; }}cout<<"三个科目成绩总和最高的学生是:"<<endl;stu[n1].printid();cout<<"总和:"<<s1<<endl;/*stu[n2].printid();stu[n3].printid();*/}