#include<iostream.h>
const int MaxSize=10;
class Student
{
public:
Student(){length=0;}
Student(float a[],int n);
~Student(){}
void Insert(int i,float x);
int Length(){return length;}
int Delete(int i);
int Locate(float x);
void Print();
private:
float data[MaxSize];
int length;
};
Student::Student(float a[],int n)
{
if(n>MaxSize)throw"参数非法";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
void Student::Insert(int i,float x)
{
if(length>=MaxSize)throw"上溢";
if(i<1||i>length+1)throw"位置非法";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
int Student::Delete(int i)
{
if(length==0)throw"下溢";
if(i<1||i>length)throw"位置非法";
float x=data[i-1];
for(int j=i;j<length;j++)
data[j-1]=data[j];
length--;
return x;
}
int Student::Locate(float x)
{
for(int i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
void Student::Print()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
void main()
{
float r[5]={91,92,93,94,95};
Student L(r,5);
cout<<"一共有"<<L.Length()<<"学生成绩"<<endl;
L.Print();
cout<<"在第4位插入一个分数为100的学生"<<endl;
L.Insert(4,100);
cout<<"插入后一共有"<<L.Length()<<"学生成绩"<<endl;
L.Print();
cout<<"分数为93的学生位置为";
cout<<L.Locate(93)<<endl;
cout<<"执行删除第一个学生分数的操作,删除前数据为:"<<endl;
L.Print();
L.Delete(1);
cout<<"删除后数据为:"<<endl;
L.Print();
}
const int MaxSize=10;
class Student
{
public:
Student(){length=0;}
Student(float a[],int n);
~Student(){}
void Insert(int i,float x);
int Length(){return length;}
int Delete(int i);
int Locate(float x);
void Print();
private:
float data[MaxSize];
int length;
};
Student::Student(float a[],int n)
{
if(n>MaxSize)throw"参数非法";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
void Student::Insert(int i,float x)
{
if(length>=MaxSize)throw"上溢";
if(i<1||i>length+1)throw"位置非法";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
int Student::Delete(int i)
{
if(length==0)throw"下溢";
if(i<1||i>length)throw"位置非法";
float x=data[i-1];
for(int j=i;j<length;j++)
data[j-1]=data[j];
length--;
return x;
}
int Student::Locate(float x)
{
for(int i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
void Student::Print()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
void main()
{
float r[5]={91,92,93,94,95};
Student L(r,5);
cout<<"一共有"<<L.Length()<<"学生成绩"<<endl;
L.Print();
cout<<"在第4位插入一个分数为100的学生"<<endl;
L.Insert(4,100);
cout<<"插入后一共有"<<L.Length()<<"学生成绩"<<endl;
L.Print();
cout<<"分数为93的学生位置为";
cout<<L.Locate(93)<<endl;
cout<<"执行删除第一个学生分数的操作,删除前数据为:"<<endl;
L.Print();
L.Delete(1);
cout<<"删除后数据为:"<<endl;
L.Print();
}