源代码
- #include
- using namespace std;
- const int MaxSize=100;
- template
- class Score
- {
- public:
- Score(){ length=0; } //建立空的顺序表
- Score(DataType a[],int n); //建立长度为n的顺序表
- ~Score(){} //建立析构函数
- int Length(){ return length;} //求线性表的长度
- DataType Get( int i); //按位置查找
- int Locate(DataType x); //按值查找
- void Insert(int i,DataType x); //在位置i插入x
- DataType Delete(int i); //删除
- void Print(); //输出
- private:
- DataType data[MaxSize]; //存放数据元素的数组
- int length; //线性表的长度
- };
- template
- Score::Score(DataType a[],int n)
- {
- int i;
- if(n>MaxSize)
- throw "非法参数";
- for(i=0;i //按位查找
- DataType Score::Get( int i)
- {
- if(i<1||i>length)
- throw"查找位置非法";
- else return data[i-1];
- }
- template //按值查找
- int Score::Locate(DataType x)
- {
- int i;
- for(i=0;i
- void Score::Insert(int i,DataType 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++;
- }
- template
- DataType Score::Delete(int i)
- {
- int x,j;
- if(length==0)
- throw"下溢";
- if(i<1||i>length)
- //throw"位置异常";
- x=data[i-1];
- for(j=i;j
- void Score::Print()
- {
- int i;
- for(i=0;is(a,10);
- while(flag==0)
- {
- cout<<"please input the command(1~8):"<>t;
- switch(t)
- {
- case 1:
- l=s.Length();
- cout<<"the length is:"<>i;
- x=s.Get(i);
- cout<<"the number is:"<>x;
- i=s.Locate(x);
- cout<<"the number's location is:"<<i>>i;
- cout<<"please input the insert number:"<>x;
- s.Insert(i,x);
- cout<<"insert successfully!"<>i;
- s.Delete(i);
- cout<<"delete successfully!"<</
运行结果
求长度
按位置查找成绩
按值查找位置
插入成绩