#include<iostream>
using namespace std;
const int MaxSize=100;
class Student
{
public:
Student(){length=0;}
Student(float a[],int n);
~Student(){}
int Length(){return length;}
void Get(int i);
int Locate(float x);
void Insert(int i,float x);
float Delete(int i);
void Print();
private:
float data[MaxSize];
int length;
};
Student::Student(float a[],int n)
{
int i;
if(n>MaxSize)throw"参数非法";
for(i=0;i<n;i++)
data[i]=a[i];
length=n;
}
void Student::Get(int i)
{
if(i<1&&i>length) throw"查找位置非法";
else cout<<data[i-1];
}
int Student::Locate(float x)
{
int i;
for(i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
void Student::Insert(int i,float x)
{
int j;
if(length>=MaxSize)throw"上溢";
if(i<1||i>length+1)throw"位置";
for(j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
float Student::Delete(int i)
{
int j;
float x;
if(length==0)throw"下溢";
if(i<1||i>length)throw"位置";
x=data[i-1];
for(j=i;j<length;j++)
data[j-i]=data[j];
length--;
return x;
}
void Student::Print()
{
int i;
for(i=0;i<length;i++)
cout<<data[i]<<endl;
}
int main()
{
float a[3]={68,45.5,86};
Student stu(a,3);
cout<<"学生的成绩为:";
stu.Print();
cout<<"查找位置3的成绩为:";
stu.Get(3);
stu.Insert(2,78);
cout<<endl<<"在位置2插入成绩78后的结果为:";
stu.Print();
stu.Delete(1);
cout<<endl<<"删除位置1的成绩后,结果为:";
stu.Print();
return 0;
}
using namespace std;
const int MaxSize=100;
class Student
{
public:
Student(){length=0;}
Student(float a[],int n);
~Student(){}
int Length(){return length;}
void Get(int i);
int Locate(float x);
void Insert(int i,float x);
float Delete(int i);
void Print();
private:
float data[MaxSize];
int length;
};
Student::Student(float a[],int n)
{
int i;
if(n>MaxSize)throw"参数非法";
for(i=0;i<n;i++)
data[i]=a[i];
length=n;
}
void Student::Get(int i)
{
if(i<1&&i>length) throw"查找位置非法";
else cout<<data[i-1];
}
int Student::Locate(float x)
{
int i;
for(i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
void Student::Insert(int i,float x)
{
int j;
if(length>=MaxSize)throw"上溢";
if(i<1||i>length+1)throw"位置";
for(j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
float Student::Delete(int i)
{
int j;
float x;
if(length==0)throw"下溢";
if(i<1||i>length)throw"位置";
x=data[i-1];
for(j=i;j<length;j++)
data[j-i]=data[j];
length--;
return x;
}
void Student::Print()
{
int i;
for(i=0;i<length;i++)
cout<<data[i]<<endl;
}
int main()
{
float a[3]={68,45.5,86};
Student stu(a,3);
cout<<"学生的成绩为:";
stu.Print();
cout<<"查找位置3的成绩为:";
stu.Get(3);
stu.Insert(2,78);
cout<<endl<<"在位置2插入成绩78后的结果为:";
stu.Print();
stu.Delete(1);
cout<<endl<<"删除位置1的成绩后,结果为:";
stu.Print();
return 0;
}