分类: C/C++
2009-09-27 11:17:10
#include
#include
#include
using namespace std;
class Student
{
public:
Student(){Maths=0;English=0;Science=0;}
Student(string n,string na):num(n),name(na){}
string GetNumber(){return num;}
string GetName(){return name;}
void entermarks(int m,int e,int s){Maths=m;English=e;Science=s;}
int getMathsMark(){return Maths;}
int getEnglishMark(){return English;}
int getScienceMark(){return Science;}
double calculateAverageMark(){return(double)(Maths+English+Science)/3;}
void Input()
{
cout<<"请输入学生资料:\n";
cout<<"学号\t姓名\t数学\t英语\t社会\n";
cin>>num>>name>>Maths>>English>>Science;
}
private:
string num;//学号
string name;//姓名
int Maths;//数学成绩
int English;//英语成绩
int Science;//科学成绩
} ;
class StudentList
{
private:
Student *list;
int total;
int count;
public:
StudentList(int t):total(t),count(0){list=new Student[total];}
~StudentList(){delete []list;}
bool add(Student *s)
{
if (isFull())
return 0;
else
{
list[count]=*s;count++;return 1;
}
return 0;
}
bool isEmpty(){return count==0?1:0;}
bool isFull(){return count>=total?1:0;}
Student getItem(int n)
{
if(n>=total)cerr<<"输入错误"<
}
int getTotal(){return total;}
int getCount(){return count;}
};
class StudentListTester:public StudentList
{
public:
StudentListTester(int n):StudentList(n){}
void Add_Record(Student &s)
{
if (add(&s)==1)cout<<"学生记录已经加入!\n\n";
else
{
if (isFull())cout<<"学生已经满了,不能增加!\n\n";
}
}
void Print_All()
{
if(getCount()==0){cout<<"**********没有学生记录**********\n\n";}
else
{
cout<<"-------------------所有学生记录------------------\n\n";
cout<<"学号\t姓名\t数学\t英语\t社会\t平均\n\n";
for(int i=0;i
Student t=getItem(i);
cout<
cout<<"-------------------------------------------------\n\n";
}
}
void Print_Under40()
{
int n=0;
for(int i=0;i
Student t=getItem(i);
if(t.calculateAverageMark()<40)n=1;
}
if(n==0){cout<<"**********没有平均分小于40的学生记录**********\n\n";}
else
{
cout<<"-------------------平均分小于40的学生记录------------------\n\n";
cout<<"学号\t姓名\t数学\t英语\t社会\t平均\n\n";
for(i=0;i
Student t=getItem(i);
if(t.calculateAverageMark()<40)
{
cout<
}
cout<<"-----------------------------------------------------------\n\n";
}
}
void Print_Zhang()
{
int n=0;
for(int i=0;i
Student t=getItem(i);
if(t.GetName().find("张")!=-1)n=1;
}
if(n==0){cout<<"**********没有姓张的学生记录**********\n\n";}
else
{
cout<<"-------------------姓张的学生记录------------------\n\n\n";
cout<<"学号\t姓名\t数学\t英语\t社会\t平均\n\n";
for(i=0;i
Student t=getItem(i);
if(t.GetName().find("张")!=-1)
{
cout<
}
cout<<"-----------------------------------------------------\n\n";
}
}
};
void main()
{
int i;
cout<<"输入学生最大数量:";
cin>>i;
StudentListTester S(i);
int n;
cout<<"[1] 增加一个学生\n"
<<"[2] 显示数组中所有的学生以及他们的成绩\n"
<<"[3] 显示数组中平均成绩<40的学生以及他们的成绩\n"
<<"[4] 显示数组中姓张的学生以及他们的成绩\n"
<<"[5] 退出\n";
cout<<"请选择操作项:[1-5] ";
cin>>n;
while(1)
{
switch(n)
{
case 1:
{
if(S.isFull()==1){cout<<"学生已经满了,不能增加!\n\n";break;}
Student a,*b=&a;
b->Input();
S.Add_Record(*b);
}
break;
case 2:
S.Print_All();break;
case 3:
S.Print_Under40();break;
case 4:
S.Print_Zhang();break;
case 5:
cout<<"**************谢谢使用****************\n\n";return;
default:
cout<<"**************无效输入****************\n\n";
}
cout<<"\n按任意键继续\n\n";
getchar();
getchar();
if(n>=1&&n<=4)
{
system("cls");
cout<<"[1] 增加一个学生\n"
<<"[2] 显示数组中所有的学生以及他们的成绩\n"
<<"[3] 显示数组中平均成绩<40的学生以及他们的成绩\n"
<<"[4] 显示数组中姓张的学生以及他们的成绩\n"
<<"[5] 退出\n";
cout<<"请选择操作项:[1-5] ";
}
cin>>n;
}
}