| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 490 人关注过本帖
标题:哪位仁兄帮忙看一下!谢谢啦
只看楼主 加入收藏
qlc00
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:157
专家分:540
注 册:2007-11-26
结帖率:50%
收藏
已结贴  问题点数:30 回复次数:4 
哪位仁兄帮忙看一下!谢谢啦
#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 编辑 ]
搜索更多相关主题的帖子: 仁兄 
2009-10-29 18:15
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:30 
enum是一个特殊的整数的集合。
要实现你要求的那个功能还是用数组吧。

你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-10-29 20:38
qlc00
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:157
专家分:540
注 册:2007-11-26
收藏
得分:0 
强制转化类型难道不行吗?

Anything is possible!
2009-10-29 21:01
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:0 
我觉得enum的作用是:
1.给本身无意义的整数值赋上撰写期信息(就是指定一个特定意义),增加可读性
2.在类中定义常量

enum Sex{male,female};
相当于定义了int male = 0;int female = 1;
所以我认为强制转化也是行不通的

你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-10-29 22:49
qlc00
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:157
专家分:540
注 册:2007-11-26
收藏
得分:0 
回复 4楼 flyingcloude
现在已经弄出来了,是用if语句来实现的,给你看下源代码!但是有些繁琐!
#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)
{
    if(s.sex==1)
        out<<s.id<<"  "<<s.name<<"  "<<"female"<<"  "<<s.birth_day.year<<"/"
            <<s.birth_day.month<<"/"<<s.birth_day.day<<"  "<<s.birth_place<<"  ";
    if(s.sex==0)
        out<<s.id<<"  "<<s.name<<"  "<<"male"<<"  "<<s.birth_day.year<<"/"
            <<s.birth_day.month<<"/"<<s.birth_day.day<<"  "<<s.birth_place<<"  ";
    switch(s.major)
    {
    case 0: out<<"MATHEMTIC";
        break;
    case 1: out<<"QHYSICS";
        break;
    case 2: out<<"CHEMISTRY";
    break;
    case 3: out<<"COMPUTER";
        break;
    case 4: out<<"GEOGRAPHY";
        break;
    case 5: out<<"ASTRONOMY";
        break;
    case 6: out<<"ENGLISH";
        break;
    case 7: out<<"CHINESE";
        break;
    case 8: out<<"PHILOSOPHY";
        break;
    default:
        cout<<"没有这个专业!\n";
    }
    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;
    do
    {
        cout<<"0:男性 1:女性\n";
        cin>>i;
    }while(i>1||i<0);
    switch(i)
    {
    case 0: s.sex=male;
        break;
    case 1: s.sex=female;
        break;
    default:
        cout<<"输入错误!";
    }
    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<<"没有相匹配的专业!";
    }
    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;
}

Anything is possible!
2009-10-30 00:12
快速回复:哪位仁兄帮忙看一下!谢谢啦
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012578 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved