⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tongxunlu.cpp

📁 一个同学录
💻 CPP
字号:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
struct Inf
{
	char name[80];
	char addr[80];
	char tel[20];
	char postcode[20];
	struct Inf *next;
};
class Cperson
{
public:
	Cperson()
	{
		head=new Inf[1];
		head->next=NULL;
	}
	void Creat();
	void Insert();
	void Find();
	void Getin();
	void Print(Inf *p);
	void Printout();
	void Correct();
	void Store();
private:
	Inf *head;
};

void Cperson::Creat()     //建立通讯录
{ 
	Inf *l,*s;
	char m;
	l=new Inf[1];
	l->next=NULL;
	head->next=l;
	m='y';
	while(m!='N'&&m!='n')
	{
		cout<<"请输入通讯者的信息:"<<endl;
		cout<<"姓名:";cin>>l->name;
		cout<<"地址:";cin>>l->addr;
		cout<<"电话:";cin>>l->tel;
		cout<<"邮编:";cin>>l->postcode;
		s=new Inf[1];
		s->next=NULL;
		l->next=s;
		l=s;
		cout<<"是否继续录入下一个?(Y/y or N/n)";
		cin>>m;
	}
	l->next=NULL;
}
void Cperson::Insert()
{
	Inf *l,*s;
	l=head;	  s=new Inf;
	char m; m='y';
	while(m!='N'&&m!='n')
	{
		cout<<"请输入待插入通讯者的信息:"<<endl;
		cout<<"姓名:";cin>>s->name;		
		cout<<"地址:";cin>>s->addr;
		cout<<"电话:";cin>>s->tel;
		cout<<"邮编:";cin>>s->postcode;	     
		if(l==NULL)
		{
			l->next=s;
            s->next=NULL;
		}
		else
		{
			s->next=l->next;
			l->next=s;
		}
		cout<<"您是否继续插入?(Y/y or N/n)";
		cin>>m;
	}
}
void Cperson::Find()
{
	Inf *p;char m[80];
	char h;h='y';
	while(h!='N'&&h!='n')
	{
		p=head->next;			
		cout<<"请输入你要找的人的姓名:";
		cin>>m;	
		while(p&&strcmp(p->name,m))
			p=p->next;
		if(p==NULL)
			cout<<"通讯录里没有联系人"<<endl;
		else
		{
			if(!strcmp(p->name,m))
			{
				cout<<"你查找的人的信息如下:"<<endl;
				Print(p);
			}
			else
				cout<<"您的通讯录里不存在此人,请确认!"<<endl;
			cout<<"您是否继续?(Y/y or N/n):";cin>>h;
			p=head->next;
		}		
	}
}
void Cperson::Correct()
{	
	Inf *p,*s;char m[80];
	char h;h='y';
	while(h!='N'&&h!='n')
	{
		p=head->next;			
		cout<<"请输入你要找的人的姓名:";
		cin>>m;	
		while(p&&strcmp(p->name,m))
			p=p->next;
		if(p==NULL)
			cout<<"通讯录里没有联系人"<<endl;
		else
		{
			if(!strcmp(p->name,m))
			{
				s=new Inf[1];
				cout<<"你查找的人的信息如下:"<<endl;
				Print(p);
				cout<<"输入您要修改后的信息:  "<<endl;
				cout<<"地址:";cin>>s->addr;
				cout<<"电话:";cin>>s->tel;
				cout<<"邮编:";cin>>s->postcode;
				strcpy(p->addr,s->addr);
				strcpy(p->tel,s->tel);
				strcpy(p->postcode,s->postcode);
			}
			else
				cout<<"您的通讯录里不存在此人,请确认!"<<endl;
			cout<<"您是否继续?(Y/y or N/n):";cin>>h;
			p=head->next;
		}		
	}	
}

void Cperson::Print(Inf *p)
{	
	cout<<"  姓名: "<<p->name						
		<<"  电话: "<<p->tel
		<<"  地址: "<<p->addr
		<<"  邮编: "<<p->postcode
		<<endl;					
}
void Cperson::Printout()
{
	Inf *p;
	p=head->next;
	while(p)
	{
		Print(p);
		p=p->next;
	}
}
void Cperson::Store()
{
	fstream outfile;
	outfile.open("tongxunlu.txt",ios::out);
	if(!outfile)
	{
		cout<<"tongxunlu.txt can't open.\n";
		abort();
	}
	Inf *p;
	p=new Inf[1];
	p=head;
	int i=0;
	while(p)
	{
		outfile<<"第";outfile<<++i;outfile<<"个人的信息:";outfile<<'\n';
		outfile<<"  姓名: ";outfile<<p->name;outfile<<'\n';
		outfile<<"  电话: ";outfile<<p->tel;outfile<<'\n';
		outfile<<"  地址: ";outfile<<p->addr;outfile<<'\n';
		outfile<<"  邮编: ";outfile<<p->postcode;outfile<<'\n';
		p=p->next;
	}
	cout<<endl;
	cout<<"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*"<<endl;
	cout<<"*         存储完毕!               *"<<endl;
	cout<<"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*"<<endl;
	cout<<endl;
	outfile.close();
}

void Cperson::Getin()
{
	fstream infile;
	infile.open("tongxunlu.txt",ios::in);
	if(!infile)
	{
		cout<<"tongxunlu.txt can't open.\n";
		abort();
	}
	Inf *p;
	p=new Inf;
	while(!infile.eof())
	{
		Print(p);
		p=p->next;
	}
	infile.close();
}
void main()
{
	Cperson c;
	int x,flag;
	while(flag)
	{
		cout<<"-----------------------------------"<<endl;
		cout<<"*      欢迎使用通讯录系统         *"<<endl;
		cout<<"*=================================*"<<endl;
		cout<<"*      1.添加通讯录               *"<<endl;
		cout<<"*      2.显示通讯录               *"<<endl;
		cout<<"*      3.存储通讯录               *"<<endl;
		cout<<"*      4.装入通讯录               *"<<endl;
		cout<<"*      5.查询通讯录               *"<<endl;
		cout<<"*      6.修改通讯录               *"<<endl;
		cout<<"*      8.建立通讯录               *"<<endl;
		cout<<"*      7.退出管理系统             *"<<endl;
		cout<<"---------------------------------- "<<endl;
		cout<<"请选择您要执行的操作:";
		cin>>x;
		if(x>8)
			cout<<"注意!输入错误,重新输入!"<<endl;
		else {
			switch(x)
			{
			case(1):c.Insert();break;
			case(2):c.Printout();break;
			case(3):c.Store();break;
			case(4):c.Getin();break;
			case(5):c.Find();break;
			case(6):c.Correct();break;
			case(8):c.Creat();break;
			case(7):{flag=0;cout<<"退出成功!"<<endl;}break;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -