第5个回答 2012-06-04
我给你!!!!
记得加分!!!!!
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#include<fstream.h>
//学生类
class Student{
public:
Student(int stu_no1=-1,char* name1=" ",char* sex1=" ");
~Student();
int stu_no;
char* name;
char* sex;
};
Student::Student(int stu_no1,char* name1,char* sex1)
{
stu_no=stu_no1;
name=new char[strlen(name1)+1];
strcpy(name,name1);
sex=new char[strlen(sex1)+1];
strcpy(sex,sex1);
}
Student::~Student()
{
delete []name;
delete []sex;
}
//兴趣类
class Intrest:public Student{
public:
Intrest(int stu_no1,char* name1,char* sex1,int num1=-1,char* style1=" ");
~Intrest();
int num;
char* style;
};
Intrest::Intrest(int stu_no1,char* name1,char* sex1,int num1,char* style1):Student(stu_no1,name1,sex1)
{
num=num1;
style=new char[strlen(style1)+1];
strcpy(style,style1);
}
Intrest::~Intrest()
{
delete []style;
}
//兴趣类型类
class Kind:public Intrest{
public:
Kind(int stu_no1=-1,char* name1=" ",char* sex1=" ",int num1=-1,char* style1=" "):Intrest(stu_no1,name1,sex1,num1,style1)
{}
void insert();
void modify();
void del();
void show();
void setstu(int stu_no1)
{
stu_no=stu_no1;
}
void setname(char* name1)
{
name=new char[strlen(name1)+1];
strcpy(name,name1);
}
void setsex(char* sex1)
{
sex=new char[strlen(sex1)+1];
strcpy(sex,sex1);
}
void setnum(int num1)
{
num=num1;
}
void setstyle(char* style1)
{
style=new char[strlen(style1)+1];
strcpy(style,style1);
}
static int count;
}stu[100];
int Kind::count=0;
void Kind::insert()
{
cout<<"学生学号:"<<endl;
cin>>stu_no;
cout<<"学生姓名:"<<endl;
cin>>name;
cout<<"学生性别:"<<endl;
cin>>sex;
cout<<"兴趣编号:"<<endl;
cin>>num;
cout<<"兴趣类型:"<<endl;
cin>>style;
count++;
}
void Kind::modify()
{
cout<<"+++++请选择要进行的修改:"<<endl;
cout<<"1...修改学号"<<endl;
cout<<"2...修改姓名"<<endl;
cout<<"3...修改性别"<<endl;
cout<<"4...修改编号"<<endl;
cout<<"5...修改类型"<<endl;
cout<<"其它数字键..退出"<<endl;
int a;
cin>>a;
switch(a)
{
case 1:
cout<<"输入新的学号:"<<endl;
cin>>stu_no;
break;
case 2:
cout<<"输入新的姓名:"<<endl;
cin>>name;
break;
case 3:
cout<<"输入新的性别:"<<endl;
cin>>sex;
break;
case 4:
cout<<"输入新的编号:"<<endl;
cin>>num;
break;
case 5:
cout<<"输入新的类型:"<<endl;
cin>>style;
break;
default:
return;
}
}
void Kind::del()
{
stu_no=-1;
name=" ";
sex=" ";
num=-1;
style=" ";
count--;
}
void search()
{
cout<<"输入要查询的学生的学号:"<<endl;
int r,o=1;
cin>>r;
for(int i=0;i<Kind::count;i++)
{
if(stu[i].stu_no==r)
{
cout<<setiosflags(ios::left)<<setw(15)<<"学号"<<setiosflags(ios::left)<<setw(10)<<"姓名"<<setiosflags(ios::left)<<setw(10)\
<<setiosflags(ios::left)<<setw(10)<<"性别"<<setiosflags(ios::left)<<setw(10)<<"兴趣编号"<<setiosflags(ios::left)\
<<setw(10)<<"兴趣类型"<<endl;
stu[i].show();
o=0;
}
}
if(o==1)
cout<<"没有该学号,查询失败"<<endl;
}
void Kind::show()
{
cout<<setiosflags(ios::left)<<setw(15)<<stu_no<<setiosflags(ios::left)<<setw(10)<<name<<setiosflags(ios::left)<<setw(10)\
<<setiosflags(ios::left)<<setw(10)<<sex<<setiosflags(ios::left)<<setw(10)<<num<<setiosflags(ios::left)\
<<setw(10)<<style<<endl;
}
void pri()
{
ofstream fout("学生兴趣小组管理.txt");
if(!fout)
{
cout<<"can not open output file"<<endl;
}
fout<<setiosflags(ios::left)<<setw(15)<<"学号"<<setiosflags(ios::left)<<setw(10)<<"姓名"<<setiosflags(ios::left)<<setw(10)\
<<setiosflags(ios::left)<<setw(10)<<"性别"<<setiosflags(ios::left)<<setw(10)<<"兴趣编号"<<setiosflags(ios::left)\
<<setw(10)<<"兴趣类型"<<endl;
for(int i=0;i<Kind::count;i++)
{
fout<<setiosflags(ios::left)<<setw(15)<<stu[i].stu_no<<setiosflags(ios::left)<<setw(10)<<stu[i].name<<setiosflags(ios::left)<<setw(10)\
<<setiosflags(ios::left)<<setw(10)<<stu[i].sex<<setiosflags(ios::left)<<setw(10)<<stu[i].num<<setiosflags(ios::left)\
<<setw(10)<<stu[i].style<<endl;
}
fout.close();
cout<<"保存成功!"<<endl;
return ;
}
void menu()
{
cout<<"===========学生兴趣小组管理============"<<endl;
cout<<"1---添加学生兴趣信息"<<endl;
cout<<"2---修改学生兴趣信息"<<endl;
cout<<"3---删除学生兴趣信息"<<endl;
cout<<"4---查询学生兴趣信息"<<endl;
cout<<"5---显示学生兴趣信息"<<endl;
cout<<"6---保存数据!!!"<<endl;
cout<<"其它数字键------退出"<<endl;
}
void main()
{
for(;;)
{
menu();
cout<<"请选择您要进行的处理:"<<endl;
int i;
cin>>i;
switch(i)
{
case 1:
cout<<"请输入要添加的学生的人数:"<<endl;
int i,n;
cin>>n;
for(i=0;i<n;i++)
{
stu[Kind::count].insert();
}
if(n==0)
cout<<"无输入!!!"<<endl;
else
cout<<"输入完毕!!!"<<endl;
break;
case 2:
cout<<"输入要修改的学生的学号:"<<endl;
int p;
cin>>p;
for(i=0;i<Kind::count;i++)
{
if(stu[i].stu_no==p)
{
stu[i].modify();
cout<<"修改成功!!!"<<endl;
}
}
break;
case 3:
cout<<"输入要删除的学生的学号:"<<endl;
int q;
cin>>q;
for(i=0;i<Kind::count;i++)
{
if(stu[i].stu_no==q)
{
stu[i].del();
}
}
for(;i<Kind::count;i++)
{
Kind temp;
temp=stu[i];
stu[i]=stu[i+1];
stu[i+1]=stu[i];
}
break;
case 4:
search();
break;
case 5:
cout<<setiosflags(ios::left)<<setw(15)<<"学号"<<setiosflags(ios::left)<<setw(10)<<"姓名"<<setiosflags(ios::left)<<setw(10)\
<<setiosflags(ios::left)<<setw(10)<<"性别"<<setiosflags(ios::left)<<setw(10)<<"兴趣编号"<<setiosflags(ios::left)\
<<setw(10)<<"兴趣类型"<<endl;
for(i=0;i<Kind::count;i++)
stu[i].show();
break;
case 6:
pri();
break;
default:
return;
}
}
}本回答被提问者采纳