C++Vector在输出的时候没有反应,调了好久没结果,请大家帮我看一下
程序运行结果:
--------构造两个向量--------
请输入向量La的结点元素(输入'0'结束):
a
v
x
'0'
构造的向量La为:
长度:4
向量元素为:? ? ? ?-------------------------这里没有输出向量?
请输入向量Lb的结点元素(输入'0'结束):
template <class T>
#define DefaultSize 100
class Vector
{
private:
T* elements;
int ArraySize, VectorLength; // 总长,实长
void GetArray(void);
public:
Vector(int ArraySize = DefaultSize); //构造函数
~Vector(void){delete[] elements;}
int GetLength(void)const {return VectorLength;}
T Getnode(int i)
{
return (i<0||i>=VectorLength)?NULL:elements[i];
}
int Find(T& x)
{
for (int i=0; i<VectorLength; i++)
if (elements[i]==x)return i;
return -1;
}
bool Insert(T& x, int i);
bool Remove(int i);
};
template <class T>
void Vector<T>::GetArray(void)
{
elements = new T[ArraySize];
if(elements==NULL)
cerr << "Memory Allocation Error" << endl;
}
template <class T>
Vector <T>::Vector(int sz)
{
if(sz<=0)
cerr << "Invalid Array Size" << endl;
else
{
ArraySize = sz;
VectorLength = 0;
GetArray();
}
}
template <class T>
bool Vector<T>::Insert(T& x, int i)
{
if(VectorLength == ArraySize)
{
cerr << "overflow" << endl;
return false;
}
else if(i<0 || i>VectorLength)
{
cerr << "position error" << endl;
return false;
}
else
{
for (int j =VectorLength-1; j>=i; j--)
elements[i] = x;
VectorLength++;
return true;
}
}
template <class T>
bool Vector<T>::Remove(int i)
{
if(VectorLength == 0)
{
cerr << "Vector is empty" << endl;
return false;
}
else if(i<0 || i>VectorLength-1)
{
cerr << "position error" << endl;
return false;
}
else
{
for (int j=i; j<VectorLength-1; j++)
elements[j] = elements[j+1];
VectorLength--;
return true;
}
}
template <class T>
void Union(Vector<T>&Va, Vector<T>&Vb)
{
int n = Va.GetLength();
int m = Vb.GetLength();
for (int i=0; i<m; i++)
{
T x = Vb.Getnode(i);
int k = Va.Find(x);
if (k==-1)
{
Va.Insert(x, n);
n++;
}
}
}
template <class T>
void Intersection(Vector<T>&Va, Vector<T>& Vb)
{
int n=Va.GetLength();
int m=Vb.GetLength();
int i =0;
while(i < m)
{
T x = Vb.Getnode(i);
int k = Va.Find(x);
if (k==-1)
{
Vb.Remove(i);
m--;
}
else i++;
}
}
void Josephus(Vector<int>&P, int n, int s, int m)
{
int k =1;
for (int i=0; i<n; i++)
{
P.Insert(k, i);
k++;
}
int s1 =s;
for (int j=n; j>=1; j--)
{
s1 = (s1 + m -1)%j;
if (s1==0)
s1 = j;
int w = P.Getnode(s1-1);
P.Remove(s1-1);
P.Insert(w, n-1);
}
}
#include<iostream.h>
#include"Vector.h"
void main()
{ cout<<"--------构造两个向量--------"<<endl;
Vector<char> La(10); //构造一长度为10的向量
Vector<char> Lb(10); //构造一长度为10的空表
int i=0,l=0,j=0;
char ch;
bool t=true;
cout<<"请输入向量La的结点元素(输入'0'结束):"<<endl;
while (t=true)
{ cin>>ch;
if (ch=='0') break;
t=La.Insert(ch,i);
i++;
}
l=La.GetLength();
cout<<"构造的向量La为:"<<endl;
cout<<"长度:"<<l<<endl;
cout<<"向量元素为:";
for(j=1;j<=l;j++)
{ ch=La.Getnode(j-1);
cout<<ch<<" ";
}
cout<<endl<<endl;
i=0;
cout<<"请输入向量Lb的结点元素(输入'0'结束):"<<endl;
while (t=true)
{ cin>>ch;
if (ch=='0') break;
t=Lb.Insert(ch,i);
i++;
}
l=Lb.GetLength();
cout<<"构造的向量Lb为:"<<endl;
cout<<"长度:"<<l<<endl;
cout<<"向量元素为:";
for(j=1;j<=l;j++)
{ ch=Lb.Getnode(j-1);
cout<<ch<<" ";
}
cout<<endl<<endl;
///////// //两个向量之间的交运算
l=Lb.GetLength();
Vector<char> Lc(20);
for(j=1;j<=l;j++)
{ ch=Lb.Getnode(j-1);
Lc.Insert(ch,j-1);
}
Intersection(La,Lc);
cout<<"两向量的交集为:";
l=Lc.GetLength();
for(i=1;i<=l;i++)
{
cout<<Lc.Getnode(i-1)<<" ";
}
cout<<endl;
/////////// //将两个向量合并成一个向量
Union(La, Lb);
cout<<"两向量的并集为:";
l=La.GetLength();
for(i=1;i<=l;i++)
{
cout<<La.Getnode(i-1)<<" ";
}
}
程序运行结果:
--------构造两个向量--------
请输入向量La的结点元素(输入'0'结束):
a
v
x
'0'
构造的向量La为:
长度:4
向量元素为:? ? ? ?-------------------------这里没有输出向量?
请输入向量Lb的结点元素(输入'0'结束):