c对txt的添加 修改 和 条件查询。。。
#include <iostream>#include <fstream>
#include <string>
using namespace std;
struct guest
{
string id;
string name;
string address;
string date;
string ps;
guest(string i,string p,string a,string d,string s):id(i),name(p),address(a),date(d),ps(s) {next = NULL;}
guest* next;};
class guest_mgr{
private:
guest* head;
public:
guest_mgr();
bool add_people(string id,string name,string address,string date,string ps);
bool alt_people(string id,string name,string address,string date,string ps);
bool look_file(string filename);
void print_people();
~guest_mgr();
}
bool guest_mgr::add_people(string id,string name,string address,string date,string ps){
if(NULL==head)
return false;
guest* p = head;
while(NULL!=p->next){
p = p->next;
}
guest* newguest = new guest(id,name,address,date,ps);
p->next = newguest;
newguest->next = NULL;
return true;
}
bool guest_mgr::look_file(string filename)
{
if stream inobj(filename.c_str());
if(!inobj){
cout<<"open file failed.\n";
return false;
}
guest *t,*p;
string sid,sname,saddress,sdate,sps;
p = head;
while(!inobj.eof()){
inobj>>sid>>sname>>saddress>>sdate>>sps;
t = new guest(sid,sname,saddress,sdate,sps);
p->next = t;
p = t;
}
t->next = NULL;
return true;
}
bool guest_mgr::chk_people(string id){
if(NULL==head)
return false;
guest *p = head;
while(NULL!=p->next&&id!=p->id){
p = p->next;
}
if(NULL!=p->next){
return true;
}
else if(NULL==p->next&&id==p->id)
return true;
return false;
}
bool guest_mgr::chk_people(string id,string name,string address,string date,string ps){
if(NULL==head)
return false;
guest *p = head;
while(NULL!=p->next&&id!=p->id){
p = p->next;
}
if(NULL!=p->next){
if(name==p->name)
return true;
}
else if(NULL==p->next&&id==p->id)
if(name==p->name)
return true;
return false;
}
void guest_mgr::print_people(){
if(NULL==head){
cout<<"no guest in the list.\n";
return;
}
guest* p = head;
while(NULL!=p){
cout<<p->id<<" "<<p->name<<" "<<p->address" "<<p->date" "p->ps<<"\n";
p=p->next;
}
cout<<endl;
}
guest_mgr* mylist;//在菜单函数中要用到,不要移动它
string fn = "data.txt";//同上
void add(){
cout<<"\n-------------------Add Guest----------------------\n";
string id,name,address,date,contents,ps;
cout<<"ID:";
cin>>id;
cout<<"name:";
cin>>name;
cout<<"Address:";
cin>>address;
cout<<"Date:";
cin>>date;
cout<<"Contents:";
cin>>contents;
cout<<"Ps:";
cin>>ps;
if(mylist->add_people(id,name,address,date,contents,ps)){
cout<<"\nadd guest successful.\n";
}
else{
cout<<"sorry,the system is wrong.\n";
}
cout<<"\n-------------------Add Guest----------------------\n";
}
void menu(){
int c;
cout<<"\n****************************Main Menu****************************\n";
cout<<"1.添加信息\t2.修改信息\t3.查询信息\t4.退出";
cout<<"\n****************************Main Menu****************************\n";
cin>>c;
switch(c){
case 1:add();break;
case 2:look();break;
case 3:look();break;
default:mylist->write_file(fn);exit(0);
}
}
void main(){
mylist = new guest_mgr();
mylist->look_file(fn);
while(1)
menu();
cout<<endl;
}
我想实现 c对txt的添加 修改 和 条件查询。。。。
怎么改?