目录
目标
系统中需要实现的功能如下:
-
添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人(添加成功)
-
显示联系人:显示通讯录中所有联系人信息
-
删除联系人:按照姓名进行删除指定联系人
-
查找联系人:按照姓名查看指定联系人信息
-
修改联系人:按照姓名重新修改指定联系人
-
清空联系人:清空通讯录中所有信息
-
退出通讯录:退出当前使用的通讯录
源码:
#include<iostream>
#include<string.h>
#include"operation_address_book.h"
using namespace std;
int main(){
cout<<"功能列表如下(输入对应的数字进行相关操作):"<<endl;
while (true){
cout<<"*1. 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人\n\
*2. 显示联系人:显示通讯录中所有联系人信息\n\
*3. 删除联系人:按照姓名进行删除指定联系人\n\
*4. 查找联系人:按照姓名查看指定联系人信息\n\
*5. 修改联系人:按照姓名重新修改指定联系人\n\
*6. 清空联系人:清空通讯录中所有信息\n\
*7. 退出通讯录:退出当前使用的通讯录"<<endl;
int operation;
cin>>operation;
operation_address_book address_book_operation;
if(operation==7){
cout<<"你确认要退出通讯录?(1:yes,2:no)"<<endl;
int exit;
cin>>exit;
if(exit==1)
break;
}
else if(operation ==1){
address_book_operation.add_new_address_book();
}
else if(operation ==2){
address_book_operation.show_all_address_book();
}
else if(operation ==3){
string name;
cout<<"输入你要删除的联系人名字:"<<endl;
cin>>name;
address_book_operation.delete_target_address_book(name);
}
else if(operation == 4){
string name;
cout<<"输入你要查找的联系人名字:"<<endl;
cin>>name;
address_book_operation.find_target_address_book(name);
}
else if(operation ==5){
string name;
cout<<"输入你要修改的联系人名字:"<<endl;
cin>>name;
address_book_operation.update_address_book(name);
}
else if(operation ==6){
cout<<"确认要清空通讯录?(1:yes,other:no)"<<endl;
int num;
cin>>num;
if(num ==1){
address_book_operation.clear_all_address_book();
}
}
else{
cout<<"输入有误,重新输入!!!!"<<endl;
}
}
return 0;
}
#include<iostream>
#include<string>
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
using namespace std;
#define address_book_name "./address_book.txt"
#define most_address_num 1000
class operation_address_book
{
public:
void set_address_book(string name,int sex,int age,string phone,string address){
book_name = name;
book_sex =sex;
book_age = age;
book_phone = phone;
book_address = address;
}
//检查通讯录是否满的功能
int check_address_book_num(){
int num = 0;
ifstream infile;
infile.open(address_book_name);
if(infile.is_open()){
string s;
while(getline(infile,s)){
num +=1;
}
}
else{
cout<<"文件打开失败!!!"<<endl;
}
infile.close();
return num;
}
//增加联系人功能
void add_new_address_book(){
int num = check_address_book_num();
if(num<1000){
cout<<"当前联系人数量"<<num<<" "<<"你还可以增加"<<most_address_num-num<<"个联系人"<<endl;
ofstream target_file;
target_file.open(address_book_name,ios::app);
if(target_file.is_open()){
cout<<"请输入通讯者姓名:";
cin>>book_name;
cout<<"\n请输入性别(0:男,1:女):";
cin>>book_sex;
cout<<"\n请输入年龄:";
cin>>book_age;
cout<<"\n请输入电话号码:";
cin>>book_phone;
cout<<"\n请输入住址:";
cin>>book_address;
target_file<<book_name<<" "<<book_sex<<" "<<book_age<<" "<<book_phone<<" "<<book_address<<endl;
cout<<"录入联系人:"<<book_name<<"成功"<<endl;
target_file.close();
}
else{
cout<<"文件未打开"<<endl;
}
}
else{cout<<"联系人已满!!请删除一些再添加!!"<<endl;}
}
//显示所有联系人
void show_all_address_book(){
int num = 0;
ifstream infile;
infile.open(address_book_name);
if(infile.is_open()){
cout<<"联系人如下(姓名,性别,年龄,电话,住址)"<<endl;
string s;
while(getline(infile,s)){
num +=1;
cout<<s<<endl;
}
cout<<"总共有:"<<num<<"个联系人"<<endl;
}
else{
cout<<"文件打开失败!!!"<<endl;
}
infile.close();
}
//删除指定联系人
void delete_target_address_book(string name){
//标识有没有此联系人
int flag =0;
ifstream infile;
string File_data = "";
infile.open(address_book_name);
if(infile.is_open()){
string s;
int i = 0;
while(getline(infile,s)){
string::size_type idx;
idx = s.find(name);
if(idx ==string::npos){
File_data +=s+"\n";
}
else{
cout<<"找到该联系人,在"<<i+1<<"条"<<endl;
flag = 1;
}
i +=1;
}
infile.close();
if(flag ==0){
cout<<"无此联系人"<<endl;
}
else{
ofstream outFile;
outFile.open(address_book_name);
outFile.flush();
outFile<<File_data;
outFile.close();
cout<<"删除成功!!"<<endl;
}
}
else{
cout<<"文件打开失败";
}
}
void find_target_address_book(string name){
//标识有没有此联系人
int flag =0;
ifstream infile;
infile.open(address_book_name);
if(infile.is_open()){
string s;
int i = 0;
while(getline(infile,s)){
string::size_type idx;
idx = s.find(name);
if(idx ==string::npos){
}
else{
cout<<"找到该联系人,在"<<i+1<<"条"<<"详细信息为:"<<s<<endl;
flag = 1;
}
i +=1;
}
if(flag ==0)
cout<<"没有找到该联系人!!"<<endl;
infile.close();
}
else{
cout<<"文件打开失败"<<endl;
}
}
void update_address_book(string name){
//标识有没有此联系人
int flag =0;
ifstream infile;
infile.open(address_book_name);
if(infile.is_open()){
string s;
string File_data="";
int i = 0;
while(getline(infile,s)){
string::size_type idx;
idx = s.find(name);
if(idx ==string::npos){
File_data +=s+"\n";
}
else{
cout<<"找到该联系人,在"<<i+1<<"条"<<"详细信息为:"<<s<<endl;
string name;
string sex;
string age;
string phone;
string address;
cout<<"请输入新的姓名:"<<endl;
cin>>name;
cout<<"请输入新的性别:"<<endl;
cin>>sex;
cout<<"请输入新的年龄:"<<endl;
cin>>age;
cout<<"请输入新的电话号码:"<<endl;
cin>>phone;
cout<<"请输入新的地址:"<<endl;
cin>>address;
File_data +=name+" "+sex+" "+age+" "+phone+" "+address+"\n";
flag = 1;
}
i +=1;
}
if(flag ==0)
cout<<"没有找到该联系人,无法修改!!"<<endl;
else{
ofstream outFile;
outFile.open(address_book_name);
outFile.flush();
outFile<<File_data;
outFile.close();
cout<<"修改成功!!"<<endl;
}
infile.close();
}
else{
cout<<"文件打开失败"<<endl;
}
}
void clear_all_address_book(){
fstream outFile;
outFile.open(address_book_name,ios::out);
outFile.flush();
outFile<<"";
outFile.close();
cout<<"清空成功!!"<<endl;
}
private:
string book_name;
int book_sex;
int book_age;
string book_phone;
string book_address;
};