//实现一个学生信息管理系统
//能够新增学生信息,查询学生信息,删除学生信息
//以及按照学号排序学生信息等功能。
#include<iostream>
#include<string>
using namespace std;
static int cnt=0;
struct stu_struct{
string number;
string gender;
string name;
};
class stu_system{
public:
void myinput();
void mysearch();
void mydelete();
void mysort();
void myoutput();
private:
stu_struct stu[100];
string inf;
string infdel;
};
void stu_system::myoutput(){
for(int i=0;i<cnt;i++){
cout<<"name:"<<stu[i].name<<endl;
cout<<"number:"<<stu[i].number<<endl;
cout<<"gender:"<<stu[i].gender<<endl;
if(i!=cnt-1) cout<<endl;
}
}
void stu_system::mysort(){
for(int i=0;i<cnt;i++){
for(int j=0;j<cnt-i-1;j++){
if(stu[j].number>stu[j+1].number){
stu_struct t2=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t2;
}
}
}
cout<<"The information has been sorted!"<<endl;
}
void stu_system::myinput(){
cout<<"Enter your name:";
cin>>stu[cnt].name;
cout<<"Enter your number:";
cin>>stu[cnt].number;
cout<<"Enter your gender:";
cin>>stu[cnt].gender;
cnt++;
}
void stu_system::mydelete(){
cout<<"Enter the information you want to delete(name or number):";
cin>>infdel;
int val;
int ret;
for(int i=0;i<cnt;i++){
if(infdel==stu[i].name||infdel==stu[i].number){
ret=1;
val=i;
}
}
if(ret){
stu[val].name="";
stu[val].gender="";
stu[val].number="";
for(int i=val;i<cnt-1;i++){
stu[i]=stu[i+1];
}
stu[cnt-1].name="";
stu[cnt-1].gender="";
stu[cnt-1].number="";
cnt--;
cout<<"The information has been deleted!"<<endl;
}else cout<<"Can't find the information!"<<endl;
}
void stu_system::mysearch(){
cout<<"Enter the information(name or number):";
cin>>inf;
int ret=0;
int val;
for(int i=0;i<cnt;i++){
if(inf==stu[i].name||inf==stu[i].number){
ret=1;
val=i;
}
}
if(ret==0) cout<<"Can't find the information!"<<endl;
else {
cout<<"Here is the information:"<<endl;
cout<<"name:"<<stu[val].name<<endl;
cout<<"number:"<<stu[val].number<<endl;
cout<<"gender:"<<stu[val].gender<<endl;
}
}
int main()
{
string a;
stu_system student;
cout<<"What do you wang to do?";
cout<<" (Please enter 'search' or 'input' or 'delete' or 'sort' or 'output')";
cout<<endl;
cout<<"(If you want to end, please enter 'End'.)";
cout<<endl;
cin>>a;
while(a!="End"){
if(a=="search")
student.mysearch();
else if(a=="input")
student.myinput();
else if(a=="delete")
student.mydelete();
else if(a=="sort")
student.mysort();
else if(a=="output")
student.myoutput();
else cout<<"Please Enter again."<<endl;
cin>>a;
}
return 0;
}
运行结果: