开发者社区> 问答> 正文

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

screenshot
![screenshot](https://oss-cn-
hangzhou.aliyuncs.com/yqfiles/4f10b8140953765233b86cf833cf418a760d6eaf.png)
screenshot

展开
收起
a123456678 2016-03-09 14:23:17 3609 0
1 条回答
写回答
取消 提交回答
  •  #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;
    }
    2019-07-17 18:56:05
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载