#include<iostream.h>
class stu
{ private:
int data[4];
int length;
public:
stu (int a[],int n);
void insert(int i,int x);
int Delete(int i);
int locate(int x);
void print();
};
stu::stu(int a[],int l)
{ for(int i=0;i<l;i++)
data[i]=a[i];
length=l;}
void stu::insert(int i,int x) //i为插入位置 x为插入的数值
{if (i<1||i>4) {cout<<"wrong adress"<<endl;return;}
if (length>=4) {cout<<"max"<<endl;return;}
for (int a=length;a>=i;a--)
data[a]=data[a-1];
data[i-1]=x;
length++;
}
int stu::Delete(int i) //删除第i个 返回删除的值x
{int x;
if (length<1)
{cout<<"empty"<<endl;return 0;}
x=data[i-1];
for (int a=i;a<length;a++)
data[a-1]=data[a];
length--;
return x;}
int stu::locate(int x) //输入值x 返回其位置
{for(int a=0;a<length;a++)
if (data[a]==x) return a+1;
else {cout<<"没有找到该值"<<endl;}
return 0;
}
void stu::print() //输出全部值
{for(int a=0;a<length;a++)
cout<<data[a]<<" ";
cout<<endl;}
int main()
{int a[4],p,q;
cout<<"请输入若干个数"<<endl;
for(int i=0;i<4;i++)
cin>>a[i];
stu L(a,4);
do{cout<<endl;
cout<<"选择要进行的操作"<<endl;
cout<<"1--------插入"<<endl<<"2--------查询"<<endl<<"3---------删除"<<endl<<"4---------显示"<<endl<<"5----------退出"<<endl;
cin>>i;
switch(i)
{case 1:cout<<"插入位置:";cin>>p;cout<<endl;cout<<"插入数值:";cin>>q; L.insert(p,q);break;
case 2:cout<<"查找位置的数值:";cin>>p;cout<<endl;cout<<"其位置为:"<<L.locate(p);break;
case 3:cout<<"要删除的数值所在位置:";cin>>p;cout<<endl;cout<<"所删除的数值为:"<<L.Delete(p);break;
case 4:L.print();break;
case 5:break;
default:cout<<"wrong choice!chose again!";break;}
}while(i!=5);
return 0;
}
class stu
{ private:
int data[4];
int length;
public:
stu (int a[],int n);
void insert(int i,int x);
int Delete(int i);
int locate(int x);
void print();
};
stu::stu(int a[],int l)
{ for(int i=0;i<l;i++)
data[i]=a[i];
length=l;}
void stu::insert(int i,int x) //i为插入位置 x为插入的数值
{if (i<1||i>4) {cout<<"wrong adress"<<endl;return;}
if (length>=4) {cout<<"max"<<endl;return;}
for (int a=length;a>=i;a--)
data[a]=data[a-1];
data[i-1]=x;
length++;
}
int stu::Delete(int i) //删除第i个 返回删除的值x
{int x;
if (length<1)
{cout<<"empty"<<endl;return 0;}
x=data[i-1];
for (int a=i;a<length;a++)
data[a-1]=data[a];
length--;
return x;}
int stu::locate(int x) //输入值x 返回其位置
{for(int a=0;a<length;a++)
if (data[a]==x) return a+1;
else {cout<<"没有找到该值"<<endl;}
return 0;
}
void stu::print() //输出全部值
{for(int a=0;a<length;a++)
cout<<data[a]<<" ";
cout<<endl;}
int main()
{int a[4],p,q;
cout<<"请输入若干个数"<<endl;
for(int i=0;i<4;i++)
cin>>a[i];
stu L(a,4);
do{cout<<endl;
cout<<"选择要进行的操作"<<endl;
cout<<"1--------插入"<<endl<<"2--------查询"<<endl<<"3---------删除"<<endl<<"4---------显示"<<endl<<"5----------退出"<<endl;
cin>>i;
switch(i)
{case 1:cout<<"插入位置:";cin>>p;cout<<endl;cout<<"插入数值:";cin>>q; L.insert(p,q);break;
case 2:cout<<"查找位置的数值:";cin>>p;cout<<endl;cout<<"其位置为:"<<L.locate(p);break;
case 3:cout<<"要删除的数值所在位置:";cin>>p;cout<<endl;cout<<"所删除的数值为:"<<L.Delete(p);break;
case 4:L.print();break;
case 5:break;
default:cout<<"wrong choice!chose again!";break;}
}while(i!=5);
return 0;
}