#include <iostream>
using namespace std;
using datatype =int;
struct stu
{
datatype *data=NULL;
int size=0;
int len=0;
void create(int a);
bool empty();
bool full();
bool add(datatype e);
bool bl();
bool delet_pos(int a);
datatype at(int index);
bool insert_pos(int c,int inde);
};
void stu::create(int a)
{
size = a;
data=new int[a];
}
bool stu::empty()
{
return len==0;
}
bool stu::full()
{
return len==size;
}
bool stu::add(datatype e)
{
if(full())
{
cout<<"添加失败";
return false;
}
data[len]=e;
cout<<data[len]<<endl;
len++;
cout<<"添加成功"<<endl;
return true;
}
bool stu::bl()
{
if(empty())
{
cout<<"遍历失败"<<endl;
return false;
}
for(int i =0;i<len;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
cout<<"遍历成功"<<endl;
return true;
}
bool stu::delet_pos(int a)
{
if(empty())
{
cout<<"删除失败"<<endl;
return false;
}
for(int i=a;i<len;i++)
{
data[i]=data[i+1];
}
data[len-1]=0;
len--;
cout<<"删除成功"<<endl;
return true;
}
datatype stu:: at(int index)
{
if(empty())
{
cout<<"失败"<<endl;
return -1;
}
cout<<data[index-1]<<endl;
return 0;
}
bool stu::insert_pos(int c,int inde)
{
if(empty()||full()||c>len)
{
cout<<"插入失败"<<endl;
return false;
}
len++;
for(int i=len-1;i>=c-1;i--)
{
data[i]=data[i-1];
}
data[c-1]=inde;
cout<<"插入成功"<<endl;
return true;
}
void menu()
{
cout<<"**********菜单*********"<<endl;
cout<<" 1.添加 "<<endl;
cout<<" 2.删除 "<<endl;
cout<<" 3.查看 "<<endl;
cout<<" 4.插入 "<<endl;
cout<<" 5.遍历 "<<endl;
cout<<" 6.退出 "<<endl;
cout<<"***********************"<<endl;
}
int main()
{
bool b2=0;
stu p1;
int a =0;
int input=0;
int exit=1;
cout<<"请输入创建数据表的大小";
cin>>a;
p1.create(a);
if(p1.data!=NULL)
{
cout<<"创建成功"<<endl;
}
do
{
input=0;
menu();
cout<<"请选择>>>";
cin>>input;
switch(input)
{
case 1:
{
datatype e =0;
cout<<"请输入添加成绩";
cin>>e;
b2=p1.add(e);
}break;
case 2:
{
int q =0;
cout<<"请输入删除位置>>>";
cin>>q;
p1.delet_pos(q);
}break;
case 3:
{
int index;
cout<<"请输入查看的位置>>>";
cin>>index;
p1.at(index);
}break;
case 4:
{
int c;
int inde;
cout<<"请输入成绩";
cin>>c;
cout<<"请输入插入的位置>>>";
cin>>inde;
p1.insert_pos(inde,c);
}break;
case 5:
{
p1.bl();
}break;
case 6:
{
exit=0;
}break;
default:
{
cout<<"输入错误,重新输入";
}
}
}while(exit);
delete []p1.data;
return 0;
}
Day 25 12站 渐入佳境
最新推荐文章于 2024-09-04 21:29:26 发布