通讯录项目
额,其实核心就是实现一下对 .txt 文件内容的修改,只不过主题就是以通讯录功能的形式展现出来。
这里的具体功能实现就是就是,增加联系人,删除联系人,查找单独的联系人,展示全部的联系人。如果读者感兴趣的话可以再往里面增加函数(博主写这篇的主要目的是为了增加大家的学习兴趣,熟练一下对应的函数,如有错误请多多指教Orz)。
源代码:
这里有一些查找的选项还没有往里面加,以后博主会会慢慢往里面加(巨巨勿笑)
代码参考了一位巨巨的文章->巨巨的原文;
#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include<string>
#include<iomanip>
using namespace std;
struct people
{
string name;
bool sex;///1为男
string location;
string telphone;
long postcode;
string email;
};
void init(people &m)///初始化某个节点
{
m.name="0";
m.sex=0;
m.location="0";
m.telphone="0";
m.postcode=0;
m.email="0";
}
void inser_t(people &m,ifstream &infile)///把文件单个元素插入到结构体中
{
infile>>m.name;
infile>>m.sex;
infile>>m.location;
infile>>m.telphone;
infile>>m.postcode;
infile>>m.email;
}
void write_in(people &m,ofstream &outfile)///将文件流中的数据存到结构体中
{
outfile<<'\n';
outfile<<m.name;
outfile<<" ";
outfile<<m.sex;
outfile<<" ";
outfile<<m.location;
outfile<<" ";
outfile<<m.telphone;
outfile<<" ";
outfile<<m.postcode;
outfile<<" ";
outfile<<m.email;
}
void cou_style()///输出通讯录的表头
{
cout<<endl;
cout<<setiosflags(ios::left);///左对齐
cout<<endl;
cout<<setw(10)<<"姓名";
cout<<setw(5)<<"性别";
cout<<setw(15)<<"地址";
cout<<setw(13)<<"电话号码";
cout<<setw(9)<<"邮编";
cout<<setw(25)<<"E_mail";
cout<<endl;
}
void cou_tongxunlu(people m)///显示该节点对应的数据
{
cout<<setiosflags(ios::left);
cout<<setw(10)<<m.name;
if(m.sex==1) cout<<setw(5)<<"男";
else cout<<setw(5)<<"女";
cout<<setw(15)<<m.location;
cout<<setw(13)<<m.telphone;
cout<<setw(9)<<m.postcode;
cout<<setw(25)<<m.email;
cout<<endl;
}
void add()
{
vector<people>sto;
people temp;
int i;
ifstream infile("D:\\tongxunlu.txt");
if(infile==0)
{
cout<<"打开文件失败"<<endl;
return ;
}
while(!infile.eof())///从文件中导出联系人信息到容器中。
{
init(temp);
inser_t(temp,infile);
sto.push_back(temp);
}
infile.close();
ofstream outfile("D:\\tongxunlu.txt",ios::app);///追加模式打开文件,ios::app如果没有该文件自动生成,如果有则在文件尾追加
if(outfile==0)
{
cout<<"打开文件失败"<<endl;
return ;
}
cout<<"请您按照顺序输入联系人信息(如缺失信息,请以0代替):姓名(string),性别(1:男,0:女),地址(string),电话(string),邮编(long),E_mail(string)"<<endl;
cin>>temp.name;
cin>>temp.sex;
cin>>temp.location;
cin>>temp.telphone;
cin>>temp.postcode;
cin>>temp.email;
int room=sto.size();
bool flag=0;
for( i=0; i<room; i++)
{
if(temp.telphone==sto[i].telphone)
{
flag=1;
break;
}
}
if(!flag)
{
write_in(temp,outfile);///往文件中写入temp
cout<<"成功添加"<<endl;
cou_style();
cou_tongxunlu(temp);
cout<<"你刚刚添加了这个联系人"<<endl;
return ;
}
else
{
cout<<"该电话号码已存在"<<endl;
cou_style();
cou_tongxunlu(sto[i]);
return ;
}
outfile.close();
}
void dele()
{
vector<people>sto;
people temp;
int i;
ifstream infile("D:\\tongxunlu.txt");
if(infile==0)
{
cout<<"打开文件失败"<<endl;
return ;
}
while(!infile.eof())
{
init(temp);
inser_t(temp,infile);
sto.push_back(temp);
}
infile.close();
cout<<"请输入你要删除的联系人姓名"<<endl;
string nam;
cin>>nam;
int len=sto.size(),flag=0;
for(int i=0; i<len; i++)
{
if(sto[i].name==nam)
{
flag=1;
init(sto[i]);
break;
}
}
if(!flag)
{
cout<<"找不到此联系人"<<endl;
return ;
}
else
{
cout<<"已删除此联系人"<<endl;
ofstream outfile("D:\\tongxunlu.txt");
if(!outfile)
{
cout<<"打开文件失败"<<endl;
return ;
}
int len2=sto.size();
for(int i=0; i<len2; i++)
{
if(sto[i].name=="0"&&sto[i].telphone=="0")
continue;
else
{
write_in(sto[i],outfile);
}
}
outfile.close();
}
}
void fin()
{
vector<people>sto,sto1;
people temp;
ifstream infile("D:\\tongxunlu.txt");
if(!infile)
{
cout<<"无法打开此文件"<<endl;
return ;
}
while(!infile.eof())
{
init(temp);
inser_t(temp,infile);
sto.push_back(temp);
}
infile.close();
cout<<"如果按照名字查找请输入 0 ,如果按照电话号查找请输入 1"<<endl;
int con;
cin>>con;
cout<<"输入你查找的关键字"<<endl;
string jud;
cin>>jud;
int flag=0;
if(!con)
{
int len=sto.size();
for(int i=0; i<len; i++)
{
if(sto[i].name==jud)
{
flag=1;
sto1.push_back(sto[i]);
}
}
if(flag==0)
{
cout<<"没有此联系人"<<endl;
return ;
}
else{
if(sto1.size()==1){
cou_style();
cou_tongxunlu(sto[0]);
}
else{
cout<<"姓名为"<<jud<<"的人数为:"<<sto1.size()<<endl;
cou_style();
for(int i=0;i<sto1.size();i++)
cou_tongxunlu(sto[i]);
}
}
}
if(con==1)///按照电话号码查找
{
int len=sto.size();
for(int i=0; i<len; i++)
{
if(sto[i].telphone==jud)
{
// st=i;
flag=1;
sto1.push_back(sto[i]);
//break;
}
}
if(flag==0)
{
cout<<"没有此联系人"<<endl;
return ;
}
else{
if(sto1.size()==1){
cou_style();
cou_tongxunlu(sto[0]);
}
else{
cout<<"电话为"<<jud<<"的人数为:"<<sto1.size()<<endl;
cou_style();
for(int i=0;i<sto1.size();i++)
cou_tongxunlu(sto[i]);
}
}
}
return ;
}
void show_all()
{
vector<people>sto;
people temp;
ifstream infile("D:\\tongxunlu.txt");
if(!infile)
{
cout<<"打开文件失败"<<endl;
cout<<endl;
return ;
}
while(!infile.eof())
{
init(temp);
inser_t(temp,infile);
sto.push_back(temp);
}
infile.close();
cou_style();
int len=sto.size();
for(int i=0;i<len;i++)
{
cou_tongxunlu(sto[i]);
}
return ;
}
int main()
{
int con;
while(1)
{
cout<<"请输入你的请求"<<endl;
cout<<"1: 添加联系人,2:删除通讯录中的某个联系人,3:查找联系人,4:查看全部联系人 -1:结束"<<endl;
scanf("%d",&con);///这里con是control的缩写(本人习惯而已)
if(con==1)
{
add();
cout<<endl;
}
else if(con==2)
{
dele();
cout<<endl;
}
else if(con==3)
{
fin();
cout<<endl;
}
else if(con==4)
{
show_all();
cout<<endl;
}
else if(con==-1)
{
return 0;
}
else
{
cout<<"输入的数据无效"<<endl;
cout<<endl;
}
}
return 0;
}
这里代码变量设置的有点奇怪,看不明白的话评论区评论,我会及时回复。
总结:
博主现在已经大二,由于自身的懒惰以及安心于学校的绩点。现在懊悔不已,于是报了一个补习班,引导做一下项目,希望会有用。(希望各位读者好好学习,打ACM也是一条很好的路,但是也不是只有这一条。)
告诫:
BAT,我来啦。