哪位仁兄帮忙看一下!谢谢啦
#include <iostream.h>#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>
class student;
ostream & operator<<(ostream &out,student &s);
istream & operator>>(istream &in,student &s);
enum Sex{male,female};
struct Data
{
int year;
int month;
int day;
};
enum Major{MATHEMTICS,QHYSICS,CHEMISTRY,COMPUTER,GEOGRAPHY,ASTRONOMY,
ENGLISH,CHINESE,PHILOSOPHY};
class student
{
char id[11];
char name[9];
Sex sex;
Data birth_day;
char birth_place[40];
Major major;
bool initialized;
public:
friend ostream & operator<<(ostream &out,student &s);
friend istream & operator>>(istream &in,student &s);
bool data_is_ok(){return initialized;}
};
ostream & operator<<(ostream &out,student &s)
{
out<<s.id<<" "<<s.name<<" "<<s.sex<<" "<<s.birth_day.year<<"/"
<<s.birth_day.month<<"/"<<s.birth_day.day<<" "<<s.birth_place<<" "<<s.major;
return out;
}
istream & operator>>(istream &in,student &s)
{
if(&in==&cin)
cout<<"请输入学号、姓名、性别、出生日期、出生地和专业(以学号'E'结束):\n";
in>>setw(11)>>s.id;
if(in.eof()||s.id[0]=='E')
{
s.initialized=false;
return in;
}
in>>setw(9)>>s.name;
int i;
in>>i;
if (i ==1||i==0)
s.sex=Sex(i);
in>>s.birth_day.year>>s.birth_day.month>>s.birth_day.day>>s.birth_place;
int m;
do
{
cout<<"0:数学1:物理2:化学3:计算机4:生物5:微电子6:英语7:中文8:哲学"<<endl;
cin>>m;
}while(m<0||m>8);
switch(m)
{
case 0: s.major=MATHEMTICS;;
break;
case 1: s.major=QHYSICS;
break;
case 2: s.major=CHEMISTRY;
break;
case 3: s.major=COMPUTER;
break;
case 4: s.major=GEOGRAPHY;
break;
case 5: s.major=ASTRONOMY;
break;
case 6: s.major=ENGLISH;
break;
case 7: s.major=CHINESE;
break;
case 8: s.major=PHILOSOPHY;
break;
default:
cout<<"没有匹配的专业!\n";
}
s.initialized=true;
return in;
}
int main()
{
ofstream out_file("E:\\students.dat",ios::out|ios::binary);
if(!out_file)
{
cerr<<"打开文件失败!\n";
return -1;
}
student st;
cin>>st;
while(st.data_is_ok())
{
out_file<<st<<endl;
cin>>st;
}
out_file.close();
return 0;
}
如果在输出的txt文本里enum类型的数据不用数字来输出而是直接用enum里面的数据输出,比如是性别,输出的时候不是输出1或者0而是输出male或者female,同理Major里面的也一样显示不是数字而是具体的那门课程!
[ 本帖最后由 qlc00 于 2009-10-29 18:17 编辑 ]