班级通讯录管理软件
建立一个简易通讯录
包含姓名,手机号码,qq号码,地址等信息,实现基本的添删改查的操作,不少于20条通讯录信息。
功能要求:
1、添加一个联系人
2、查找一个联系人,可以根据姓名,手机号码,qq号码进行查询
3、打印功能,列表显示全部通讯录信息,一屏10条记录,可以自动实现分屏
4、删除一个联系人
5、更新一个联系人的信息
6、信息保存到文件中
######欢迎使用班级通讯录管理软件 开发者:班级姓名###############
请选择要进行的操作**
系统流程图
程序源代码
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
#define MAX 1000
void showMenu()
{
cout<<"###### 欢迎使用班级管理系统 ######"<<endl;
cout<<"###### 开发者:123.wl & ######"<<endl;
cout<<"********* 请选择要进行的操作 *********"<<endl;
cout<<"********* 1、添加联系人 *********"<<endl;
cout<<"********* 2、显示联系人 *********"<<endl;
cout<<"********* 3、删除联系人 *********"<<endl;
cout<<"********* 4、查找联系人 *********"<<endl;
cout<<"********* 5、修改联系人 *********"<<endl;
cout<<"********* 6、清空联系人 *********"<<endl;
cout<<"********* 0、退出通讯录 *********"<<endl;
cout<<"*******************************************"<<endl;
}
int empNum();
struct Person1
{
string m_Name1;
int m_Sex1;
int m_Age1;
string m_Phone1;
string m_QQnumber1;
string m_Addr1;
};
struct ifsAddressbooks
{
struct Person1 peopleArray[MAX];
};
Person1 peopleArray[MAX];
void Initial(ifsAddressbooks*ifsabs)
{
string Name;
int Sex;
int Age;
string Phone;
string QQnumber;
string Addr;
ifstream ifs;
ifs.open("Save.txt",ios::in);
if(!ifs)
{
cout<<"文件打开失败"<<endl;
}
if(ifs.eof())
{
cout<<"文件为空!"<<endl;
ifs.close();
system("pause");
system("cls");
return;
}
int num=empNum();
for(int i=0;i<num;i++)
{
ifs>>Name;
ifsabs->peopleArray[i].m_Name1=Name;
ifs>>Sex;
ifsabs->peopleArray[i].m_Sex1=Sex;
ifs>>Age;
ifsabs->peopleArray[i].m_Age1=Age;
ifs>>Phone;
ifsabs->peopleArray[i].m_Phone1=Phone;
ifs>>QQnumber;
ifsabs->peopleArray[i].m_QQnumber1=QQnumber;
ifs>>Addr;
ifsabs->peopleArray[i].m_Addr1=Addr;
}
ifs.close();
return;
}
int empNum()
{
string Name;
string Phone;
string QQnumber;
string Addr;
int Sex;
int Age;
ifstream fp;
fp.open("Save.txt",ios::in);
int num=0;
while(fp>>Name>>Sex>>Age>>Phone>>QQnumber>>Addr)
{
num=num+1;
}
return num;
fp.close();
}
//1.添加联系人
void addPerson()
{
int num=empNum();
if(num==MAX)
{
cout<<"通讯录已满,无法添加!"<<endl;
return;
}
else
{
ofstream ofs;
ofs.open("Save.txt",ios::out|ios::app);
string name;
cout<<"请输入姓名:"<<endl;
cin>>name;
ofs<<name<<endl;
cout<<"请输入性别:"<<endl;
cout<<"1-男"<<endl;
cout<<"2-女"<<endl;
int sex=0;
while(true)
{
cin>>sex;
if(sex==1||sex==2)
{
ofs<<sex<<endl;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"请输入年龄:"<<endl;
int age;
while(true)
{
cin>>age;
if(age>0&&age<200)
{
ofs<<age<<endl;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"请输入手机号码:"<<endl;
string phone;
while(true)
{
cin>>phone;
if(phone.length()==11)
{
ofs<<phone<<endl;
break;
}
else
{
cout<<"输入有误,请重新输入"<<endl;
}
}
cout<<"请输入QQ号:"<<endl;
string qqnumber;
while(true)
{
cin>>qqnumber;
if(qqnumber.length()==9||qqnumber.length()==10||qqnumber.length()==11)
{
ofs<<qqnumber<<endl;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"请输入家庭住址:"<<endl;
string address;
cin>>address;
ofs<<address<<endl;
cout<<"添加成功"<<endl;
ofs.close();
system("pause");
system("cls");
}
}
//2.显示联系人
void showPerson()
{
int num=empNum();
string Name;
char Phone[20];
char QQnumber[20];
string Addr;
int Sex;
int Age;
ifstream fp;
fp.open("Save.txt",ios::in);
if(!fp.is_open())
{
cout<<"打开文件失败!!\n";
return;
}
if(num==0)
{
cout<<"文件为空!"<<endl;
system("pause");
system("cls");
return;
}
else
{
for(int i=0;i<num;i++)
{
fp>>Name>>Sex>>Age>>Phone>>QQnumber>>Addr;
if((i+1)%10!=0)
{
cout<<"姓名:"<<Name<<"\t"
<<"性别:"<<(Sex==1?"男":"女")<<"\t"
<<"年龄:"<<Age<<"\t"
<<"电话:"<<Phone<<"\t"
<<"QQ号码:"<<QQnumber<<"\t"
<<"地址:"<<Addr<<"\t";
cout<<endl;
}
else
{
cout<<"姓名:"<<Name<<"\t"
<<"性别:"<<(Sex==1?"男":"女")<<"\t"
<<"年龄:"<<Age<<"\t"
<<"电话:"<<Phone<<"\t"
<<"QQ号码:"<<QQnumber<<"\t"
<<"地址:"<<Addr<<"\t";
cout<<endl;
system("pause");
system("cls");
showMenu();
}
}
}
fp.close();
system("pause");
system("cls");
return ;
}
//2.1检测在新建的数组中是否有当前新建的数组 (姓名)
int peopleArrayisExist1(ifsAddressbooks*ifsabs,string name)
{
int num=empNum();
int flag=0;
for(int i=0;i<num;i=i+1)
{
string str1=ifsabs->peopleArray[i].m_Name1;
string str2=name;
if(str1.compare(str2)==0)
{
flag=1;
return i;
}
}
if(flag==0)
return -1;
}
//2.2检测在新建的数组中是否有当前新建的数组 (手机号)
int peopleArrayisExist2(ifsAddressbooks*ifsabs,string phone)
{
int num=empNum();
int flag=0;
for(int i=0;i<num;i=i+1)
{
string str1=ifsabs->peopleArray[i].m_Phone1;
string str2=phone;
if(str1.compare(str2)==0)
{
flag=1;
return i;
}
}
if(flag==0)
return -1;
}
//2.3检测在新建的数组中是否有当前新建的数组(QQ号码)
int peopleArrayisExist3(ifsAddressbooks*ifsabs,string qqnumber)
{
int num=empNum();
int flag=0;
for(int i=0;i<num;i=i+1)
{
string str1=ifsabs->peopleArray[i].m_QQnumber1;
string str2=qqnumber;
if(str1.compare(str2)==0)
{
flag=1;
return i;
}
}
if(flag==0)
return -1;
}
//3.删除指定的联系人
void deletePerson(ifsAddressbooks*ifsabs)
{
cout<<"请输入您要删除的联系人"<<endl;
string name;
cin>>name;
Initial(ifsabs);
int ret=peopleArrayisExist1(ifsabs,name);
int num=empNum();
if(ret!=-1)
{
ofstream ofs("Save.txt");
for(int i=ret;i<num;i++)
{
ifsabs->peopleArray[i]=ifsabs->peopleArray[i+1];
}
num--;
for(int i=0;i<num;i++)
{
ofs<<ifsabs->peopleArray[i].m_Name1<<" "
<<ifsabs->peopleArray[i].m_Sex1<<" "
<<ifsabs->peopleArray[i].m_Age1<<" "
<<ifsabs->peopleArray[i].m_Phone1<<" "
<<ifsabs->peopleArray[i].m_QQnumber1<<" "
<<ifsabs->peopleArray[i].m_Addr1<<endl;
}
ofs.close();
cout<<"删除成功"<<endl;
}
else
{
cout<<"查无此人"<<endl;
}
system("pause");
system("cls");
}
//4.查找指定联系人信息
void findPerson(ifsAddressbooks*ifsabs)
{
cout<<"请输入您要查找的联系人"<<endl;
cout<<"1.根据姓名查找"<<endl;
cout<<"2.根据手机号查找"<<endl;
cout<<"3.根据QQ号码查找"<<endl;
int select;
cin>>select;
while(true)
{
switch(select)
{
case 1:
{
cout<<"请输入姓名:"<<endl;
string name;
cin>>name;
int num=empNum();
Initial(ifsabs);
int ret=peopleArrayisExist1(ifsabs,name);
if(ret!=-1)
{
cout<<"姓名:"<<ifsabs->peopleArray[ret].m_Name1<<" "
<<"性别:"<<ifsabs->peopleArray[ret].m_Sex1<<" "
<<"年龄:"<<ifsabs->peopleArray[ret].m_Age1<<" "
<<"手机号码:"<<ifsabs->peopleArray[ret].m_Phone1<<" "
<<"QQ号码:"<<ifsabs->peopleArray[ret].m_QQnumber1<<" "
<<"地址:"<<ifsabs->peopleArray[ret].m_Addr1<<" "<<endl;
cout<<"查找成功"<<endl;
}
else
{
cout<<"查无此人"<<endl;
}
}
break;
case 2:
{
cout<<"请输入手机号" <<endl;
string phone;
cin>>phone;
int num=empNum();
Initial(ifsabs);
int ret=peopleArrayisExist2(ifsabs,phone);
if(ret!=-1)
{
cout<<"姓名:"<<ifsabs->peopleArray[ret].m_Name1<<" "
<<"性别:"<<ifsabs->peopleArray[ret].m_Sex1<<" "
<<"年龄:"<<ifsabs->peopleArray[ret].m_Age1<<" "
<<"手机号码:"<<ifsabs->peopleArray[ret].m_Phone1<<" "
<<"QQ号码:"<<ifsabs->peopleArray[ret].m_QQnumber1<<" "
<<"地址:"<<ifsabs->peopleArray[ret].m_Addr1<<" "<<endl;
cout<<"查找成功"<<endl;
}
else
{
cout<<"查无此人"<<endl;
}
break;
}
case 3:
{
cout<<"请输入一串QQ号码"<<endl;
string qqnumber;
cin>>qqnumber;
int num=empNum();
Initial(ifsabs);
int ret=peopleArrayisExist3(ifsabs,qqnumber);
if(ret!=-1)
{
cout<<"姓名:"<<ifsabs->peopleArray[ret].m_Name1<<" "
<<"性别:"<<ifsabs->peopleArray[ret].m_Sex1<<" "
<<"年龄:"<<ifsabs->peopleArray[ret].m_Age1<<" "
<<"手机号码:"<<ifsabs->peopleArray[ret].m_Phone1<<" "
<<"QQ号码:"<<ifsabs->peopleArray[ret].m_QQnumber1<<" "
<<"地址:"<<ifsabs->peopleArray[ret].m_Addr1<<" "<<endl;
cout<<"查找成功"<<endl;
}
else
{
cout<<"查无此人"<<endl;
}
break;
}
default:
{
cout<<"输入错误请重新查找"<<endl;
}
}
break;
}
system("pause");
system("cls");
}
//5.修改指定联系人信息
void modifyPerson(ifsAddressbooks*ifsabs)
{
cout<<"请输入您要修改的联系人"<<endl;
string name;
cin>>name;
Initial(ifsabs);
int ret=peopleArrayisExist1(ifsabs,name);
int num=empNum();
if(ret!=-1)
{
ofstream ofs;
ofs.open("Save.txt",ios::out|ios::ate);
string name;
cout<<"请输入姓名:"<<endl;
cin>>name;
ifsabs->peopleArray[ret].m_Name1=name;
cout<<"请输入性别:"<<endl;
cout<<"1-男"<<endl;
cout<<"2-女"<<endl;
int sex=0;
while(true)
{
cin>>sex;
if(sex==1||sex==2)
{
ifsabs->peopleArray[ret].m_Sex1=sex;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"请输入年龄:"<<endl;
int age;
while(true)
{
cin>>age;
if(age>0&&age<200)
{
ifsabs->peopleArray[ret].m_Age1=age;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"请输入手机号码:"<<endl;
string phone;
while(true)
{
cin>>phone;
if(phone.length()==11)
{
ifsabs->peopleArray[ret].m_Phone1=phone;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"请输入QQ号:"<<endl;
string qqnumber;
while(true)
{
cin>>qqnumber;
if(qqnumber.length()==9||qqnumber.length()==10||qqnumber.length()==11)
{
ifsabs->peopleArray[ret].m_QQnumber1=qqnumber;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"请输入家庭住址:"<<endl;
string address;
cin>>address;
ifsabs->peopleArray[ret].m_Addr1=address;
for(int i=0;i<num;i++)
{
ofs<<ifsabs->peopleArray[i].m_Name1<<" "
<<ifsabs->peopleArray[i].m_Sex1<<" "
<<ifsabs->peopleArray[i].m_Age1<<" "
<<ifsabs->peopleArray[i].m_Phone1<<" "
<<ifsabs->peopleArray[i].m_QQnumber1<<" "
<<ifsabs->peopleArray[i].m_Addr1<<endl;
}
cout<<"修改成功"<<endl;
}
else
cout<<"查无此人"<<endl;
system("pause");
system("cls");
}
//6.清空所有联系人
void cleanPerson()
{
int number;
cout<<"是否确定清除所有联系人?"<<endl;
cout<<"确定清除请按1"<<endl;
cout<<"放弃清除请按2"<<endl;
cin>>number;
while(true)
{
if(number==1||number==2)
{
if(number==1)
{
ofstream ofs;
ofs.open("Save.txt",ios::trunc);
ofs.close();
cout<<"通讯录已清空"<<endl;
}
else
{
cout<<"已放弃清除"<<endl;
}
break;
}
else
{
cout<<"输入错误,请重新输入"<<endl;
}
}
system("pause");
system("cls");
}
int main()
{
ifsAddressbooks ifsabs;
int select=0;
while(true)
{
showMenu();
cin>>select;
switch(select)
{
case 1://1、添加联系人
addPerson();
break;
case 2://2、显示联系人
showPerson();
break;
case 3://3、删除联系人
deletePerson(&ifsabs);
break;
case 4://4、查找联系人
findPerson(&ifsabs);
break;
case 5://5、修改联系人
modifyPerson(&ifsabs);
break;
case 6://6、清空联系人
cleanPerson();
break;
case 0://0、退出通讯录
cout<<"欢迎下次使用"<<endl;
system("pause");
return 0;
break;
default:
cout<<"输入错误,请重新输入"<<endl;
system("pause");
system("cls");
break;
}
}
system("pause");
return 0;
}