#ifndef FANGWU_H

#define FANGWU_H

#include<string>

using namespace std;

struct fangWu

int bianhao;

string zuLinZhe;

fangWu* next;

 

};

 

#endif

 

 

 

 

#ifndef FANGWUGL_H

#define FANGWUGL_H

 

#include"fangWu.h"

class fangWuGL

{

private:

fangWu* top;

fangWu* last;

fangWu* item;

public:

fangWuGL();

void addInfo(int,string);

void show();

void show(int);

void readFile();

void save();

~fangWuGL();

 

};

 

#endif

 

 

 

 

 

 

 

 

 

#include<iostream>

#include<fstream>

#include"fangWuGL.h"

using namespace std;

fangWuGL::fangWuGL()

{

top=NULL;

item=NULL;

last=NULL;

}

 

fangWuGL::~fangWuGL()

{

item=top;

fangWu* temp;

while(item!=NULL)

{

temp=item->next;

delete item;

item=temp;

}

}

 

void fangWuGL::addInfo(int b,string r)

{

item=new fangWu();

item->bianhao=b;

item->zuLinZhe=r;

item->next=NULL;

if(top==NULL)

{

top=item;

last=item;

}

else

last->next=item;

last=item;

}

 

void fangWuGL::show()

{

item=top;

while(item!=NULL)

{

cout<<item->bianhao<<"\t"<<item->zuLinZhe<<";\n";

item=item->next;

}

}

 

void fangWuGL::show(int b)

{

item=top;

while(item!=NULL)

{

if(item->bianhao==b)

{

cout<<item->bianhao<<"\t"<<item->zuLinZhe<<endl;

return;

}

item=item->next;

}

cout<<"没有此编号的房子的信息!!!!!!!!!!"<<endl;

}

 

void fangWuGL::readFile()

{

    fstream f;

f.clear();

f.open("a.dat",std::ios::in|ios::binary);

fangWu temp;

if(f.is_open())

{

while(f.read((char*)&temp,sizeof temp))

{

if(f.eof())

break;

item=new fangWu;

item->bianhao=temp.bianhao;

item->zuLinZhe=temp.zuLinZhe;

item->next=NULL;

if(top==NULL)

top=last=item;

else

last->next=item;

   last=item;

cout<<item->bianhao<<"\t"<<item->zuLinZhe<<endl;

}

}

f.close();

if(top==NULL)

cout<<"目前没有任何录入信息,暂不能执行查询操作"<<endl;

}

 

void fangWuGL::save()

ofstream f;

f.open("a.dat",std::ios::out|std::ios::binary);

item=top;

fangWu temp;

while(item!=NULL)

{

temp.bianhao=item->bianhao;

temp.zuLinZhe=item->zuLinZhe;

f.write((char*)&temp,sizeof temp);

item=item->next;

}

f.close();

}

 

 

 

 

#include"fangWu.h"

#include"fangWuGL.h"

#include<iostream>

int main()

{

cout<<"欢迎进入房屋租赁管理系统,您可以执行以下操作:"<<endl

<<"1输入type以录入新的租赁信息;"<<endl

<<"2输入search以查询已出租的房屋:"<<endl

<<"3输入save以保存已经录入的信息:"<<endl

<<"4输入quit以退出:"<<endl;

string jueDing;

string type="type";

string search="search";

string save="save";

string quit="quit";

fangWuGL test;

while(jueDing!=quit)

{

cin>>jueDing;

cin.get();

if(jueDing==type)

{

cout<<"请输入房间编号,输入0结束: ";

       char tempstr[40];

       int tempint;

       cin>>tempint;

       while(tempint!=0)

       {

cout<<"请输入租赁者信息: ";

       cin>>tempstr;

           test.addInfo(tempint,tempstr);

       test.show();

                cout<<"请输入房间编号,输入0结束: ";

       cin>>tempint;

}

}

else if(jueDing==search)

{   

int temp;

cout<<"请输入您想查询的房子的编号,输入0结束: ";

            cin>>temp;

while(temp!=0)

{

cout<<"请输入您想查询的房子的编号,输入0结束: ";

                cin>>temp;

   test.show(temp);

}

}

else if(jueDing==save)

{

test.save();

test.readFile();

}

else if(jueDing==quit)

{

break;

}

else

{

cout<<"您输入的信息有误,请重新输入:"

<<"1输入type以录入新的租赁信息;"<<endl

<<"2输入search以查询已出租的房屋:"<<endl

<<"3输入save以保存已经录入的信息:"<<endl

<<"4输入quit以退出:";

}

cout<<"欢迎进入房屋租赁管理系统,您可以执行以下操作:"<<endl

<<"1输入type以录入新的租赁信息;"<<endl

<<"2输入search以查询已出租的房屋:"<<endl

<<"3输入save以保存已经录入的信息:"<<endl

<<"4输入quit以退出:"<<endl;

 

}

 

return 0;

}

 

 

为了回答一个网友的问题,我设计了这个程序,可是出现了两个问题我怎么也解决不了,还希望知道的朋友可以帮忙,多谢.

1.就是运行这个程序读取文件,如果程序不关闭的话就没有问题,如果重新启动程序再读取文件就会出错.

2.进行search的时候,第一次输入的数字无效.

qq:914196158

tel:18680321436

eamil:914196158@qq.com