C++ 数构 关于班级学生信息处理的问题
程序是为了实现以下目的:输入:
先输入学生的个数N。接着N行代表N个学生的相关信息:num、sex、name、come_from,代表学号、性别、姓名、来的地方。接下来的3行分别代表要查找学生的学号、要删除学生的学号、要插入学生的相关信息。假设学生信息是按学号从小到大排列。
输出:
第一行为查找出的学生信息。接着的几行代表最终处理后班上所有学生的相关信息。
以下是我写的代码:
#include <iostream.h>
class student
{
public:
int num;
char sex;
char name[10];
char come_from[20];
};
template <class T>
class LinearList
{
public:
virtual bool IsEmpty() const = 0;
virtual int Length() const = 0;
virtual void Search(T x) = 0;
virtual bool Insert(int i,T x) = 0;
virtual bool Insert(T x) = 0;
virtual bool Delete(T x) = 0;
virtual void Output(ostream& out) const = 0;
protected:
int n;
};
template <class T>
class SeqList:public LinearList<T>
{
public:
SeqList(int mSize);
~SeqList();
bool IsEmpty() const;
int Length() const;
void Search(T x);
bool Insert(int i,T x);
bool Insert(T x);
bool Delete(T x);
void Output(ostream& out) const;
void Paixu(student a[],int n)
{
int t;
for(int i=0;i<=n-2;i++)
{
for(int j=n-1;j>=i+1;j--)
if(a[j].num<a[j-1].num)
{
t=a[j].num;
a[j].num=a[j-1].num;
a[j-1].num=t;
}
}
}
private:
int maxLength;
T *elements;
};
template <class T>
SeqList<T>::SeqList(int mSize)
{
maxLength = mSize;
elements = new T[maxLength];
n = 0;
}
template <class T>
SeqList<T>::~SeqList()
{
delete []elements;
}
template <class T>
bool SeqList<T>::IsEmpty() const
{
return (n == 0);
}
template <class T>
int SeqList<T>::Length() const
{
return n;
}
template <class T>
void SeqList<T>::Search(T x)
{
int s;
for(int j = 0;j < n;j++)
{
if(elements[j].num == x.num)
{
s=1;
break;}
}
if(s!=1)
{
cout<<"Can'T find"<<endl;
}
else
cout<<elements[j].num<<" "<<elements[j].sex<<" "<<elements[j].name<<" "<<elements[j].come_from<<endl;
}
template <class T>
bool SeqList<T>:: Insert(int i,T x)
{
if(i<-1||i>n - 1)
{
return false;
}
if(n==maxLength)
{
return false;
}
for(int j=n-1;j>i;j--)
{
elements[j+1]=elements[j];
}
elements[i+1]=x;
n++;
return true;
}
template <class T>
bool SeqList<T>::Insert(T x)
{
if(!n)
{
//cout<<"UnderFlow"<<endl;
return false;
}
for(int i=0;i<n;i++)
{
if(elements[i].num<=x.num)
{
i++;
break;
}
}
if(i < -1 || i > n - 1)
{
//cout<<"Out of Bounds"<<endl;
return false;
}
if(n == maxLength)
{
//cout<<"OverFlow"<<endl;
return false;
}
for(int j = n - 1;j > i;j--)
{
elements[j + 1] = elements[j];
}
elements[i + 1] = x;
n++;
return true;
}
template <class T>
bool SeqList<T>::Delete(T x)
{
if(!n)
{
//cout<<"UnderFlow"<<endl;
return false;
}
for(int i=0;i<n;i++)
{
if(elements[i].num==x.num)
{
break;
}
}
if(i < 0 || i > n-1)
{
//cout<<"Out of Bounds"<<endl;
return false;
}
for(int j = i+1;j < n;j++)
{
elements[j - 1] = elements[j];
n--;
return true;
}
}
template <class T>
void SeqList<T>::Output(ostream& out) const
{
for(int i = 0;i < n;i++)
{
out<<elements[i].num<<" "<<elements[i].sex<<" "<<elements[i].name<<" "<<elements[i].come_from<<endl;
}
}
const int SIZE = 20;
void main()
{
SeqList<student> LA(SIZE);
student s[SIZE],p;
int n;
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>s[i].num>>s[i].sex>>s[i].name>>s[i].come_from;
}
LA.Paixu(s,n);
for(i=0;i<n;i++)
{
LA.Insert(i-1,s[i]);
}
cin>>p.num;
LA.Search(p);
cin>>p.num;
LA.Delete(p);
cin>>p.num>>p.sex>>p.name>>
LA.Insert(p);
LA.Output(cout);
}
}
调试程序时输入:
5
101 m zhang fuzou
103 f li shanghai
105 m wu sanming
107 f wang zhejian
109 f chen zhangzhou
105
103
106 f ling xiamen
执行结果是:
5
101 m zhang fuzou
103 f li shanghai
105 m wu sanming
107 f wang zhejian
109 f chen zhangzhou
105
105 m wu sanming
103
106 f ling xiamenSample Output
101 m zhang fuzou
105 m wu sanming
106 f ling xiamen
105 m wu sanming
107 f wang zhejian
输出最终处理后班上所有学生的相关信息结果时,排序好像有些问题,请大家帮忙看下该怎么改,谢谢啦!!