leoe_ 2016-04-18 03:04 采纳率: 26.7%
浏览 6840
已采纳

关于c++将文件内容读取到链表中

图片说明图片说明图片说明

  • 写回答

10条回答

  • 小灸舞 2016-04-19 04:51
    关注
     #include<string>
    #include<iostream>
    #include<fstream>
    #include <stdlib.h>
    using namespace std;
    
    class Person
    {
    protected:
        string name;
        int age;
        string sex;
    };
    
    class Doctor:public Person
    {
    public:
        void show();
        void del();
        void add();
        void refer();
        void save();
        void open();
        Doctor *get();
        Doctor *find_d(int card);
    protected:
        string spe;
        double fee;
        int son_d;
    //private:
    public:
        Doctor *next,*head,*prece;
    };
    
    void Doctor::open()
    {
        FILE *fp;
        Doctor *p1,*p2;
        head=new Doctor;
        head->prece=NULL;
        p1=head;
        ifstream infile("file.txt",ios::in|ios::binary);
        p2=new Doctor;
        while(infile.read((char*)p2,sizeof(Doctor)))
        {
            cout<<p2->age;
            p1->next=p2;
            p2->prece=p1;
            p1=p2;
            cout<<"aaaa"<<endl;
            p2=new Doctor;
        }
        p1->next = NULL;
        infile.close();
    }
    void Doctor::save()
    {
        Doctor *p;
        p=head->next;
        fstream outfile("file.txt",ios::out|ios::binary);
        if(!outfile)
        {
            cout<<"打开文件失败!"<<endl;
        }
        else
        {
            while(p)
            {
                outfile.write((char *)p,sizeof(Doctor));
                p=p->next;
                cout << "haha" << endl;
            }
        }
        outfile.close();
    }
    Doctor* Doctor::find_d(int card)
    {
        int card2;
        Doctor *p;
        p=head->next;
        while(p)
        {
            card2=p->son_d;
            if(card2==card)
            {
                return p;
            }
            else
            {
                p=p->next;
            }
            if(!p)
            {
                return p=NULL;
            }
        }
    
    }
    Doctor* Doctor::get()
    {
        Doctor *p=new Doctor;
    
        cout<<" 姓名:";
        cin>>p->name;
        cout<<" 医生编号:";
        cin>>p->son_d;
        cout<<" 年龄:";
        cin>>p->age;
        cout<<" 性别:";
        cin>>p->sex;
        cout<<" 专业:";
        cin>>p->spe;
        cout<<" 诊费:";
        cin>>p->fee;
    
        return p;
    }
    
    void Doctor::add()
    {
        Doctor *p1,*p2;
        char y;
        p1=head;
        cout<<"请输入要添加的医生信息:"<<endl;
        p2=get();
    
        if(p1->next!=NULL)
            p1->next->prece=p2;
        if(p1->next==NULL)
            p2->next=NULL;
        p2->prece=p1;
        p2->next=p1->next;
        p1->next=p2;
    
    
        cout<<"医生信息添加成功!"<<endl;
        cout<<"是否继续添加医生信息?(y/n)"<<endl;
        cin>>y;
    
        if(y=='y'||y=='Y')
        {
            add();
        }
        else
        {
            return;
        }
    }
    
    
    
    void Doctor::del()
    {
        int card;
        Doctor *p;
        cout<<"请输入要删除医生信息的医生编号:";
        cin>>card;
        p=find_d(card);
    
        if(p==NULL)
        {
            cout<<"对不起!查无此人。"<<endl;
        }
        else
        {
            p->prece->next=p->next;
            if(p->next!=NULL)
                p->next->prece=p->prece;
            delete p;
            cout<<"删除成功!"<<endl;
        }
    }
    
    
    void Doctor::refer()
    {
        Doctor *p;
        cout<<"输入要查询的医生信息的医生编号:";
        int card;
        cin>>card;
        p=find_d(card);
        if(p==NULL)
        {
            cout<<"对不起!查无此人。"<<endl;
        }
        else
        {
            cout<<"姓名:";
            cout<<p->name;
            cout<<"    医生编号:";
            cout<<p->son_d;
            cout<<"    年龄:";
            cout<<p->age;
            cout<<"    性别:";
            cout<<p->sex;
            cout<<"    专业:";
            cout<<p->spe;
            cout<<"    诊费:";
            cout<<p->fee;
        }
    }
    
    
    void Doctor::show()
    {
        Doctor *p;
    
        p=head->next;
    
        while(p)
        {
            cout<<" 姓名:";
            cout<<p->name;
            cout<<"    医生编号:";
            cout<<p->son_d;
            cout<<"    年龄:";
            cout<<p->age;
            cout<<"    性别:";
            cout<<p->sex;
            cout<<"    专业:";
            cout<<p->spe;
            cout<<"    诊费:";
            cout<<p->fee;
    
            p=p->next;
            cout<<endl;
        }
    }
    
    int main()
    {
        Doctor d;
        d.head = new Doctor;
        d.head->prece = NULL;
        d.head->next = NULL;
        //d.add();
        d.open();
        d.show();
        //d.save();
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝