[c]代码库
//类实现
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
//创建联系人类
class Tongxunlu
{
public:
string id;//编号
string name; //姓名
string sex; //性别
string call; //联系方式
string address; //通讯地址
string email;//邮箱地址
}
txl[50];
//创建管理类
class Guanli
{
public:
Tongxunlu txl[50];
public:
//联系人
void Tongxunluadd(); //添加联系人信息
void Tongxunlusearch(); //查询联系人信息
//管理
void searchsid(); //按编号查询信息
void searchname(); //按名字查询信息
void searchaddress(); //按通讯地址查询信息
void searchemail(); //按邮箱地址查询信息
void write(int n); //向文件中写入数据
int read(); //从文件中读数据
void alter(); //修改信息
void del(); //删除信息
void total(); //统计信息
void display(); //显示信息
};
//函数实现
// 向文件中写入数据具体代码
/*
void Guanli::write(int i)
{
std::fstream Tongxunlucourse;
Tongxunlucourse.open("Tongxunlu.txt",ios::app| ios::binary);
if(!Tongxunlucourse)
{
cout<<"该文件不能打开!"<<endl;
abort();
}
Tongxunlucourse<< txl[i];
Tongxunlucourse.close();
}
//从文件中读数据具体代码
int Guanli::read()
{
fstream Tongxunlucourse;
Tongxunlucourse.open("Tongxunlu.txt",ios::in|ios::binary);
if(!Tongxunlucourse)
{
cout<<"该文件不能打开!"<<endl;
abort();
}
int count=0,i=0;
while(Tongxunlucourse.peek()!= EOF)
{
Tongxunlucourse.read(reinterpret_cast<char *>(&txl[i]),sizeof(txl[i]));
cout<<endl;
i++;
count++;
}
Tongxunlucourse.close();
return count;
*/
int Guanli::read()
{
int top1 = 0;
system("cls");
ifstream inflie("Tongxunlu.txt", ios::in);
if (!inflie)
{
cout << "打开失败!" << endl;
//return;
}
int i = 0;
while (inflie >> txl[i].id >> txl[i].name >> txl[i].sex >> txl[i].call >> txl[i].address >> txl[i].email )
{
i++;
top1 = i;
}
return top1;
inflie.close();
}
void Guanli::write( int top1 )
{
ofstream outflie("Tongxunlu.txt", ios::out);
if (!outflie)
{
cout << "打开失败!" << endl;
return;
}
int i;
for (i = 0; i < top1; i++)
{
outflie << txl[i].id << "\t" << txl[i].name << "\t" << txl[i].sex << "\t" << txl[i].call << "\t" << txl[i].address << "\t" << txl[i].email << endl;
}
cout << "保存成功!" << endl;
outflie.close();
}
//联系人添加信息
void Guanli::Tongxunluadd()
{
int n=read();
int i=n+1;
char sign=0;
cout<<endl<<"请输入增加联系人相关信息: "<<endl;
while(sign!='N')
{
loop:
cout<<"编号:";
cin>>txl[i].id;
cout<< endl;
int c=0;
while(c<n)
{
c++;
if(txl[i].id==txl[c].id)
{
cout<<"你输入的编号已经存在!请重新输入"<<endl;
goto loop;
}
}
cout<<"姓名:";
cin>>txl[i].name;
cout<<endl;
cout<<"性别:";
cin>>txl[i].sex;
cout<<endl;
cout<<"通讯地址:";
cin>>txl[i].address;
cout<<endl;
cout<<"联系方式:";
cin>>txl[i].call;
cout<<endl;
cout<<"邮箱地址:";
cin>>txl[i].email;
cout<<endl;
cout<<"提示:是否继续写入联系人信息?(Y/N)";
cin>>sign; //输入判断
write(++i);
i++; //write(n+1);
}
}
//查询可联系人信息
void Guanli::Tongxunlusearch()
{
int n=read();
cout<<" 1、根据编号查询 "<< " 2、根据姓名查询 "<<" 3、根据通讯地址查询 "<<" 4、根据邮箱地址查询 "<<endl;
cout<<endl;
int c;
Guanli g;
cout<<"请输入选择:";
cin>>c;
switch(c)
{
case 1:
g.searchsid();
break;
case 2:
g.searchname();
break;
case 3:
g.searchaddress();
break;
case 4:
g.searchemail();
break;
default:
cout<<"输入错误,请重新输入!"<<endl;
}
g.write(n);
}
//按联系人编号查询
void Guanli::searchsid()
{
int n=read();
string s;
int i=0;
cout<<endl<<"查找联系人信息:"<<endl;
cout<<"请输入需要查找联系人的编号:"<<endl;
cin>>s;
while(txl[i].id!=s&&i<n) i++; //查找判断
if(i==n)
{
cout<<"提示:对不起,无法找到该联系人的信息!"<<endl;
}
else
{
cout<<"******************************"<<endl;
cout<<"编号: "<<txl[i].id<<endl;
cout<<"姓名: "<<txl[i]. name<<endl;
cout<<"性别: "<<txl[i].sex<<endl;
cout<<"通讯地址: "<<txl[i].address<<endl;
cout<<"联系方式: "<<txl[i].call<<endl;
cout<<"邮箱地址: "<<txl[i].email<<endl;
}
}
//按联系人姓名查询
void Guanli::searchname()
{
int n=read();
string a;
cout<<"请输入需要查找联系人的姓名:"<<endl;
cin>>a;
for(int i=0;i<n;i++)
if(txl[i].name==a)
{
cout<<"******************************"<<endl;
cout<<"编号: "<<txl[i].id<<endl;
cout<<"姓名: "<<txl[i]. name<<endl;
cout<<"性别: "<<txl[i].sex<<endl;
cout<<"通讯地址: "<<txl[i]. address<<endl;
cout<<"联系方式: "<<txl[i].call<<endl;
cout<<"邮箱地址: "<<txl[i].email<<endl;
}
}
//按通讯地址查询
void Guanli::searchaddress()
{
int n=read();
string b;
cout<<"请输入需要查找的通讯地址:"<<endl;
cin>>b;
for(int i=0;i<n;i++)
if(txl[i].address==b)
{
cout<<"******************************"<<endl;
cout<<"编号: "<<txl[i].id<<endl;
cout<<"姓名: "<<txl[i]. name<<endl;
cout<<"性别: "<<txl[i].sex<<endl;
cout<<"通讯地址: "<<txl[i]. address<<endl;
cout<<"联系方式: "<<txl[i].call<<endl;
cout<<"邮箱地址: "<<txl[i].email<<endl;
}
}
//按邮箱地址查询
void Guanli::searchemail()
{
int n=read();
string c;
cout<<"请输入需要查找的邮箱地址:"<<endl;
cin>>c;
for(int i=0;i<n;i++)
if(txl[i].email==c)
{
cout<<"******************************"<<endl;
cout<<"编号: "<<txl[i].id<<endl;
cout<<"姓名: "<<txl[i]. name<<endl;
cout<<"性别: "<<txl[i].sex<<endl;
cout<<"通讯地址: "<<txl[i]. address<<endl;
cout<<"联系方式: "<<txl[i].call<<endl;
cout<<"邮箱地址: "<<txl[i].email<<endl;
}
}
//修改联系人信息
void Guanli::alter()
{
int n=read();
string s;
int i=0;
cout<<endl<<"修改联系人信息:"<<endl;
cout<<"请输入需要修改联系人的编号:"<<endl;
cin>>s;
while(txl[i].id!=s&&i<n)
{
i++; //查找判断
}
if(i==n)
{
cout<<"提示:对不起,无该联系人的信息!!!"<<endl; //输入失败信息
}
else
{
cout<<"该联系人的信息:"<<endl;
cout<<"编号"<<txl[i].id<<'\n'<<"姓名"<<txl[i].name<<'\n'<<"性别"<<txl[i].sex<<'\n'<<"通讯地址"
<<txl[i].address<<'\n'<<'\n'<<"联系方式"<<txl[i].call<<'\n'<<"邮箱地址"<<txl[i].email<<'\n'
<<'\n'<<endl;
cout<<"请重新输入该联系人的信息"<<endl;
cout<<"姓名:";
cin>>txl[i].name;
cout<<endl;
cout<<"性别:";
cin>>txl[i].sex;
cout<<endl;
cout<<"通讯地址:";
cin>>txl[i].address;
cout<<endl;
cout<<"联系方式:";
cin>>txl[i].call;
cout<<endl;
cout<<"邮箱地址:";
cin>>txl[i].email;
cout<<endl;
char c;
cout<<"是否保存数据?(y/n)"<<endl;
cin>>c;
if(c='y')
cout<<"修改成功!"<<endl;
write(n);
}
}
//删除联系人信息
void Guanli::del()
{
int n=read();
string s;
int i=0,j;
cout<<endl<<"删除联系人信息:"<<endl;
cout<<"请输入需要删除联系人的编号:"<<endl;
cin>>s;
while(txl[i].id!=s&&i<n)
{
i++; //查找判断
}
if(i==n)
{
cout<<"提示:记录为空!!!"<<endl; //返回失败信息
}
else
{
for(j=i;j<n-1;j++) //删除操作
{
txl[j].id=txl[j+1].id;
}
cout<<"提示:已成功删除!"<<endl; //返回成功信息
}
cout<<"你要删除的信息如下:"<<endl;
cout<<"编号:"<<txl[i].id<<endl;
cout<<"姓名:"<<txl[i].name<<endl;
cout<<"性别:"<<txl[i].sex<<endl;
cout<<"通讯地址:"<<txl[i].address<<endl;
cout<<"联系方式:"<<txl[i].call<<endl;
cout<<"邮箱地址:"<<txl[i].email<<endl;
write(n-1);
}
//统计信息
void Guanli::total()
{
{
int n=read();
string c;
cout<<"请输入需要查找的联系人名称:"<<endl;
cin>>c;
for(int i=0;i<n;i++)
if(txl[i].name==c)
{
cout<<"你要统计的信息如下:"<<endl;
cout<<"姓名:"<<txl[i].name<<endl;
cout<<"编号:"<<txl[i].id<<endl;
cout<<"性别:"<<txl[i].sex<<endl;
cout<<"通讯地址:"<<txl[i].address<<endl;
cout<<"联系方式:"<<txl[i].call<<endl;
cout<<"邮箱地址:"<<txl[i].email<<endl;
}
}
}
//显示联系人信息
void Guanli::display()
{
int n=read();
cout<<endl<<"显示全部联系人信息:"<<endl;
if(!txl)
cout<<"没有记录"<<endl;
else
{
for(int i=0;i<n;i++) //循环输入
cout<<"编号"<<txl[i].id<<'\n'<<"姓名"<<txl[i].name<<'\n'<<"性别"<<txl[i].sex<<'\n'<<"通讯地址"
<<txl[i].address<<'\n'<<"联系方式"<<txl[i].call<<'\n'<<"邮箱地址"<<txl[i].email<<'\n'<<endl;
}
}
int main()
{
char choice;
cout<<"\n\n\t\t============欢迎使用通讯录管理系统===========\n\n";
cout<<"\t\t ==============1、添加联系人记录============\n";
cout<<"\t\t ==============2、查询联系人信息============\n";
cout<<"\t\t ==============3、显示所有记录==============\n";
cout<<"\t\t ==============4、修改联系人信息============\n";
cout<<"\t\t ==============5、删除联系人信息============\n";
cout<<"\t\t ==============6、查询记录信息=============\n";
cout<<"\t\t ==============7、统计所有记录=============\n";
cout<<"\t\t ================0、退出系统=============\n";
cout<<"\t\t 请输入序号进行操作:";
cin>>choice;
Guanli g;
if(choice=='0')
{
cout<<"\n\t\t 谢谢您使用本系统!\n\n"<<endl;
exit(0);
}
else if(choice=='1')
{
g.Tongxunluadd();
system("pause"); system("cls");
main();
}
else if(choice=='2')
{
g.Tongxunlusearch();
system("pause"); system("cls");
main();
}
else if(choice=='3')
{
g.display();
system("pause"); system("cls");
main();
}
else if(choice=='4')
{
g.alter();
system("pause"); system("cls");
main();
}
else if(choice=='5')
{
g.del();
system("pause"); system("cls");
main();
}
else if(choice=='6')
{
g.total();
system("pause"); system("cls");
main();
}
else if(choice=='7')
{
g.display();
system("pause"); system("cls");
main();
}
else
{
cout<<"\t 输入错误,请重新输入您的选择:";
main();
}
return 0;
}