#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
static int n=0;
int comp(char*,char*);
class student
{
public:
void getdata()
{
tag='#';
cout<<"(姓名 身份证号码 班级 成绩):";
cin>>name>>number>>Class>>score;
}
char gettag()
{
return tag;
}
void disp()
{
if(tag=='#')
cout<<setiosflags(ios::left)
<<setw(10)<<name<<setw(18)<<number<<setw(20)<<Class<<setw(20)<<score<<endl;
}
char *getname()
{
return name;
}
private:
char tag;
char name[12];
char number[18];
char Class[20];
char score[6];
};
class teacher
{
public:
void getdata()
{
tag='#';
cout<<"(姓名 身份证号码 职称 学院):";
cin>>name>>number>>zhichen>>xueyuan;
}
char gettag()
{
return tag;
}
void disp()
{
if(tag=='#')
cout<<setiosflags(ios::left)
<<setw(10)<<name<<setw(18)<<number<<setw(20)<<zhichen<<setw(20)<<xueyuan<<endl;
}
char *getname()
{
return name;
}
private:
char tag;
char name[12];
char number[18];
char zhichen[20];
char xueyuan[30];
};
class studentteacher
{
public:
void getdata()
{
tag='#';
cout<<"(姓名 身份证号码 班级 成绩 职称 学院):";
cin>>name>>number>>Class>>score>>zhichen>>xueyuan;
}
char gettag()
{
return tag;
}
void disp()
{
if(tag=='#')
cout<<setiosflags(ios::left)
<<setw(10)<<name<<setw(18)<<number<<setw(20)<<Class<<setw(20)<<score<<setw(20)<<zhichen<<setw(20)<<xueyuan<<endl;
}
char *getname()
{
return name;
}
private:
char tag;
char name[12];
char number[18];
char Class[20];
double score;
char zhichen[20];
char xueyuan[30];
};
void func1()
{
ofstream output("MyFrd.dat");
student s;
cout<<"输入数据"<<endl;
cout<<"学生人数:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"第"<<i+1<<"个学生";
s.getdata();
output.write((char *)&s,sizeof(s));
};
output.close();
}
void func2()
{
ifstream input("MyFrd.dat");
student s;
cout<<"输出数据"<<endl;
cout<<setiosflags(ios::left)
<<setw(10)<<"姓名"<<setw(18)<<"身份证号码"<<setw(20)<<"班级"<<setw(6)<<"成绩"<<endl;
input.read((char *)&s,sizeof(s));
while(input)
{
s.disp();
input.read((char*)&s,sizeof(s));
};
input.close();
}
void func3()
{
char sname[10];
ifstream file("MyFrd.dat");
student one;
file.seekg(0);
cout<<"输入要查询的姓名(可只输入姓氏):";
cin>>sname;
cout<<"输出查询结果:"<<endl;
cout<<setiosflags(ios::left)
<<setw(10)<<"姓名"<<setw(18)<<"身份证号码"<<setw(20)<<"班级"<<setw(6)<<"成绩"<<endl;
file.read((char*)&one,sizeof(one));
while(file)
{
if(comp(one.getname(),sname)==1)
one.disp();
file.read((char*)&one,sizeof(one));
};
file.close();
}
void func4()
{
fstream outapp("MyFrd.dat",ios::app);
student one;
cout<<"添加数据:";
one.getdata();
outapp.write((char*)&one,sizeof(one));
outapp.close();
}
void func5()
{
char sname[12];
int i=0;
fstream outdel("MyFrd.dat",ios::in|ios::out);
student one;
outdel.seekg(0);
cout<<"输入要删除的姓名(可只输入姓氏):";
cin>>sname;
while(!outdel.eof())
{
outdel.seekp(sizeof(student)*i);
outdel.read((char *)&one,sizeof(one));
if(comp(one.getname(),sname)==1&&one.gettag()=='#')
{
outdel.seekp(sizeof(student) *i);
outdel.put('*');
}
i++;
}
outdel.close();
}
void func6()
{
fstream outdel("student.dat",ios::in);
fstream temp("temp",ios::out|ios::trunc);
student s;
while(!outdel.eof())
{
outdel.read((char *)&s,sizeof(student));
if(s.gettag()=='#')
temp.write((char *)&s,sizeof(student));
}
outdel.close();
temp.close();
fstream outdel1("MyFrd.dat",ios::out|ios::trunc);
fstream temp1("temp",ios::in);
while(!temp1.eof())
{
temp1.read((char *)&s,sizeof(student));
outdel1.write((char *)&s,sizeof(student));
}
outdel1.close();
temp1.close();
cout<<"此记录已物理删除"<<endl;
}