#include<iostream.h>
#include<string.h>
struct st
{float grade;
char* name;
}s[5];
class stu
{public:
stu(char*n[],float g[],int l);
stu();
~stu(){};
void in(char*n,int b,float c); //a=name,b=location,c=grade
void de(int b);
void search(int b);
void print();
private:
int length;
st *a[5];
};
stu::stu(char*n[],float g[],int l)
{for(int i=0;i<5;i++)
a[i]=&s[i];
for(i=0;i<4;i++)
{s[i].name=n[i];
s[i].grade=g[i];
}length=l;
}
void stu::in(char*n,int b,float c)
{if(length==5) cout<<"满数据."<<endl;
else{length++;s[length-1].name=n;s[length-1].grade=c;
for(int i=length;i>b;i--)
{a[i-1]=a[i-2];}
cout<<" 列入成功."<<endl;
a[b-1]=&s[length-1];}}
void stu::de(int b)
{if(length==0) cout<<"无数据."<<endl;
else if(b<1||b>length) cout<<"位置错误."<<endl;
else{cout<<a[b-1]->name<<"的数据已被删除."<<endl;
for(int i=b;i<length;i++)
{a[i-1]=a[i];
} length--;a[length]=NULL;}
}
void stu::search(int b)
{if(length==0) cout<<"无数据."<<endl;
else if(b<1||b>length) cout<<"位置错误."<<endl;
else{cout<<"第"<<b<<"位的数据为:"<<a[b-1]->name<<" "<<a[b-1]->grade<<endl;
}}
void stu::print()
{if(length==0) cout<<"无数据."<<endl;
else{for(int i=0;i<length;i++)
cout<<a[i]->name<<" "<<a[i]->grade<<endl;}}
int main()
{char*n[4]={"里","放","暗","分"};
float g[4]={80,79,81,86};
stu a(n,g,4);
int y;
float z;
char x[5];
a.print();
cout<<"输入你要列入的名字:";
cin>>x;
cout<<endl<<"分数:";
cin>>z;
cout<<endl<<"位置:";
cin>>y;
a.in(x,y,z);
a.print();
cout<<endl<<"要删除的位置:";
cin>>y;
a.de(y);
cout<<endl<<"要查找的数据的位置:";
cin>>y;
a.search(y);
cout<<"各成员数据如下:"<<endl;
a.print();
return 0;}
间接寻址方法记录成绩
最新推荐文章于 2020-12-24 03:05:01 发布