间接寻址-学生成绩


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

猜你喜欢

转载自blog.csdn.net/ab111996/article/details/80706445