#include<iostream.h>
const int MaxSize=10;
class Student
{
public:
Student(){length=0;}
Student(int g[],int n);
~Student(){}
void insert(int i,int a);
int get(int i);
int Delete(int i);
void print();
private:
int grade[MaxSize];
int length;
};
Student::Student(int g[],int n)
{
if(n>MaxSize) throw"参数非法";
for(int i=0;i<=n;i++)
grade[i]=g[i];
length=n;
}
void Student::insert(int i,int a)
{
if(length>=10)throw"上溢";
if(i<1||i>length+1)throw"位置非法";
for(int j=length;j>=i;j--)
grade[j]=grade[j-1];
grade[i-1]=a;
length++;
cout<<"信息输入成功"<<endl;
}
int Student::get(int i)
{
if(i<1&&i>length) throw"查找位置非法";
else
return grade[i-1];
}
int Student::Delete(int i)
{
int a,j;
if(length==0)throw"下溢";
if(i<1||i>length)throw"位置非法";
a=grade[i-1];
for(j=i;j<length;j++)
{
grade[j-1]=grade[j];
}
length--;
return a;
}
void Student::print()
{
int i;
for(i=1;i<length;i++)
{
cout<<"学生"<<i<<"的成绩"<<endl;
cout<<grade[i-1]<<endl;
}
}
void main()
{
int g[5]={99,95};
Student S(g,5);
cout<<"学生的成绩为:"<<endl;
S.print();
try
{
S.insert(2,92);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"在位置2插入学生成绩92:"<<endl;
S.print();
cout<<"查找第二个学生的信息:";
cout<<S.get(2)<<endl;
cout<<"执行删除第一个学生信息操作,删除前数据为:"<<endl;
S.print();
try
{
S.Delete(1);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"删除后数据为:"<<endl;
S.print();
}
实验三 顺序表
最新推荐文章于 2021-07-19 19:13:27 发布