C++控制台通迅录系统,实现文件同步更新


//定义全局变量

char num;

//定义全局函数

void choice(char);

void add_record();

void del_record();

void rep_record();

void fin_record();

void dis_record();

void exi_record();

class Person

{

public:

Person(){}

Person(string a,string b,string c,Person*d=NULL):name(a),sex(b),tel(c),next(d){}

public:

string name;

string sex;

string tel;

Person* next;

}*Linkist;

struct str{

string temp;

str*next;

}*getstr;

int main()

{

A:

cout<<"                  欢迎使用通讯录系统!!!"<<endl;

cout<<"-------------------------------------------------------------"<<endl;

cout<<"1.添加记录"<<"\t"<<"2.删除记录"<<"\t"<<"3.修改记录"<<endl;

cout<<"4.查询记录"<<"\t"<<"5.列出记录"<<"\t"<<"6.退出程序"<<endl;

cout<<"-------------------------------------------------------------"<<endl;

choice(num);

goto A;

return 0;

}

void choice(char num)

{

cout<<"请输入的所要执行操作的数字编号:";

cin>>num;

switch(num)

{

case '1':

add_record();

break;

case '2':

del_record();

break;

case '3':

rep_record();

break;

case '4':

fin_record();

break;

case '5':

dis_record();

break;

case '6':

exi_record();

break;

default:

break;

}

}

void add_record()

{

Linkist=new Person;

    Linkist->next=NULL;

    Person *q,*p;

    p=Linkist;

cout<<"请输入你所要添加的记录数量:";

int num1;

cin>>num1;

cout<<"请输入姓名、姓别、号码:"<<endl;

    for(int i=0;i<num1;i++){

q=new Person;

cin>>q->name>>q->sex>>q->tel;

       q->next=NULL;

       p->next=q; 

  p=q;

    }

cout<<"是否保存以上刚输入信息?y/n:";

char ch;

cin>>ch;

if(ch=='y'||ch=='Y')

{

ofstream outfile("person.dat",ios::app);

for(int i=0;i<num1;i++){

outfile<<Linkist->next->name<<'\t'<<Linkist->next->sex<<'\t'<<Linkist->next->tel<<endl;

Linkist=Linkist->next; 

    }

outfile.close();

}

delete Linkist,q;

}

void dis_record()

{

string temp;

ifstream infile("person.dat",ios::in);

if(!infile)

{

cerr<<"open error!"<<endl;

exit(1);

}

cout<<"姓名\t姓别\t号码"<<endl;

while(!infile.eof())

{

infile.seekg(0,ios::cur);

getline(infile,temp);

cout<<'\n'<<temp<<endl;

}

}

void del_record()

{

ifstream infile("person.dat",ios::in);

if(!infile)

{

cerr<<"open error!"<<endl;

exit(1);

}

getstr=new str;

getstr->next=NULL;

str *p,*q;

p=getstr;

while(!infile.eof())

{

q=new str;

infile.seekg(0,ios::cur);

getline(infile,q->temp);

q->next=NULL;

p->next=q;

p=q;

}

infile.close();

ofstream outfile("person.dat",ios::out);

char finstr[20];

cout<<"请输入你将要删除的人的姓名:";

cin>>finstr;

while(getstr->next->next!=NULL)

{

char sc='y';

if(!(getstr->next->temp).find(finstr))

{

cout<<getstr->next->temp<<endl;

cout<<"你确定要删除此条记录吗?y/n:";

cin>>sc;

}

if(sc=='y'||sc=='Y')

if((getstr->next->temp).find(finstr))

outfile<<getstr->next->temp<<endl;

getstr=getstr->next;

}

outfile.close();

delete getstr,q;

}

void rep_record()

{

ifstream infile("person.dat",ios::in);

if(!infile)

{

cerr<<"open error!"<<endl;

exit(1);

}

getstr=new str;

getstr->next=NULL;

str *p,*q;

p=getstr;

while(!infile.eof())

{

q=new str;

infile.seekg(0,ios::cur);

getline(infile,q->temp);

q->next=NULL;

p->next=q;

p=q;

}

infile.close();

ofstream outfile("person.dat",ios::out);

char finstr[20];

cout<<"请输入你将要修改的人的姓名:";

cin>>finstr;

Person *p1=new Person;

int xg=0;

while(getstr->next->next!=NULL)

{

char sc,xz;

if(!(getstr->next->temp).find(finstr))

{

xg=1;

cout<<getstr->next->temp<<endl;

cout<<"你确定要修改此条记录吗?y/n:";

cin>>sc;

}

if(sc=='y'||sc=='Y'){

cout<<"请为记录输入新的值:"<<endl;

cin>>p1->name>>p1->sex>>p1->tel;

cout<<"确定要保存新值吗?y/n:";

sc=' ';

cin>>xz;

}

if(xz=='y'||xz=='Y')

{

outfile<<p1->name<<'\t'<<p1->sex<<'\t'<<p1->tel<<endl;

xz=' ';

}

if(xz=='n'||xz=='n')

{

outfile<<getstr->next->temp<<endl;

xz=' ';

}

if((getstr->next->temp).find(finstr))

outfile<<getstr->next->temp<<endl;

getstr=getstr->next;

}

if(xg==0)

cout<<"找不到你所要修改的数据!"<<endl;

outfile.close();

delete getstr,q,p1;

}

void fin_record()

{

ifstream infile("person.dat",ios::in);

if(!infile)

{

cerr<<"open error!"<<endl;

exit(1);

}

getstr=new str;

getstr->next=NULL;

str *p,*q;

p=getstr;

while(!infile.eof())

{

q=new str;

infile.seekg(0,ios::cur);

getline(infile,q->temp);

q->next=NULL;

p->next=q;

p=q;

}

infile.close();

string finstr;

cout<<"请输入你将要查找的人的姓名:";

cin>>finstr;

int j;

while(getstr->next->next!=NULL)

{

j=0;

if(!(getstr->next->temp).find(finstr))

{

cout<<getstr->next->temp<<endl;

break;

}

else

++j;

getstr=getstr->next;

}

if(j>0)

cout<<"无法找到你所查询的记录!"<<endl;

delete getstr,q;

}

void exi_record()

{

exit(0);

}


你可能感兴趣的:(C++,文件流,通讯录,控制台)