#include<iostream.h>
const int MaxSize=10;
class Student
{
public:
Student(){}
Student(int a[],int n);
~Student(){}
void Insert(int i,int x);
int Delete(int i);
int Get(int i);
int Locate(int x);
void Print();
private:
int stu[MaxSize];
int length;
};
Student::Student(int a[],int n)
{
int i;
if(n>MaxSize) throw"参数非法";
for(i=0;i<n;i++)
stu[i]=a[i];
length=n;
}
void Student::Insert(int i,int x)
{
if(length>=MaxSize) throw"上溢";
if(i<1||i>length+1) throw"位置非法";
for(int j=length;j>=i;j--)
stu[j]=stu[j-1];
stu[i-1]=x;
length++;
}
int Student::Delete(int i)
{
int j,x;
if(length==0) throw"下溢";
if(i<1||i>length) throw"位置非法";
x=stu[i-1];
for(j=i;j<length;j++)
stu[j-1]=stu[j];
length--;
return x;
}
int Student::Get(int i)
{
if(i<1&&i>length) throw"查找位置非法";
else return stu[i-1];
}
int Student::Locate(int x)
{
int i;
for(i=0;i<length;i++)
if(stu[i]==x) return i+1;
return 0;
}
void Student::Print()
{
int i;
for(i=0;i<length;i++)
cout<<stu[i]<<",";
}
void main()
{
int score[5]={60,65,70,80,85};
Student S(score,5);
cout<<"学生的体育成绩:"<<endl;
S.Print();
cout<<endl<<endl<<"在位置3插入成绩75,结果如下:"<<endl;
S.Insert(3,75);
S.Print();
cout<<endl<<endl<<"在位置6删除成绩为:"<<S.Delete(6)<<endl;
cout<<"删除后结果如下:"<<endl;
S.Print();
cout<<endl<<endl<<"位置4的成绩为:"<<S.Get(4)<<endl;
cout<<endl<<endl<<"成绩65所在的位置为:"<<S.Locate(65)<<endl;
}