其他分享
首页 > 其他分享> > 通讯录系统,分文件写,文件存储

通讯录系统,分文件写,文件存储

作者:互联网

主要应用了,函数的重载,类的继承,纯虚函数(虚函数),文件输入输出,运算符的重载,分文件实现等等。
可以在包含源文件和头文件的文件夹中创建2个txt文档,一个叫Phone.txt
一个叫PhoneCar.txt
目的是为了存储数据。

实现效果图:
在这里插入图片描述

下面是源文件,创建多个源文件
main.cpp

#include <bits/stdc++.h>
#include"Phonecar.h"
#include"Phone.h"
#include"shown.h"
#include"Phone_List.h"
#include"PhoneCar_List.h"
#include"People.h"
using namespace std;
int main()
{ People t;
while(true)
{ show();
cout<<"请输入数字"<<endl;
int a;
cin>>a;
switch(a)
{ case 0:return 0;
case 1:t.add();
break;
case 2: t.de();
break;
case 3:t.my();
break;
case 4:t.finds();
break;
case 5:t.display();
break;
case 6:t.exchange_car1();
break;
case 7:t.exchange_car2();
break;
case 8:t.copy_car1();
break;
case 9:t.copy_car2();
break;
default:cout<<"输入错误,退出系统"<<endl;
return 0 ;


}

}
return 0;
}

List.cpp

#include"List.h"
 void list1::add()
{

}
 void list1::de()
{

}
 void list1::display()
{

}
 void list1::finds()
{

}
 void list1::my()
{

}

People.cpp

#include"People.h"
#include<string.h>
class p
{ public:
    string name1,number1;
};
int b;
using namespace std;
void People::add()  //
{ list1 *temp;
cout<<"1,添加到手机卡,2添加到手机"<<endl;
int a;
cin>>a;
switch(a)
{ case 1:{temp=&car2;
temp->add();
break;}
case 2:{temp=&car1;
temp->add();
break;}
default:cout<<"输入错误,退出"<<endl;
return ;
}
}
void People::de()  //
{ list1 *temp;
cout<<"1,删除手机卡联系人,2删除手机联系人"<<endl;
int a;
cin>>a;
switch(a)
{case 1:{
    temp=&car2;
temp->de();
break;}
case 2:{
temp=&car1;
temp->de();
break;}
default:cout<<"输入错误,退出"<<endl;
return ;}

}
void People::display()
{
    list1 *temp;
    cout<<"1,显示手机卡中,2显示手机中"<<endl;
int a;
cin>>a;
switch(a)
{case 1:{temp=&car2;
temp->display();
break;}
case 2:{temp=&car1;
temp->display();
break;}
default:cout<<"输入错误,退出"<<endl;
return ;
}

}
void People::finds()
{list1 *temp;
cout<<"1,查找手机卡中,2查找手机中"<<endl;
int a;
cin>>a;
switch(a)
{case 1:{temp=&car2;
temp->finds();
break;}
case 2:{temp=&car1;
temp->finds();
break;}
default:cout<<"输入错误,退出"<<endl;
return ;
}
}
void People::my()
{list1 *temp;
cout<<"1,修改手机卡中,2修改手机中"<<endl;
int a;
cin>>a;
switch(a)
{case 1:{temp=&car2;
temp->my();
break;}
case 2:{temp=&car1;
temp->my();
break;}
default:cout<<"输入错误,退出"<<endl;
return ;
}
}
void People::exchange_car1()//手机->手机卡
{  int a=car2.count2();//手机卡numb
     if(a+car1.count1()>1005)
     {
         cout<<"容量不足,操作失败"<<endl;
         return ;
     }
    for(int i=1;i<car1.count1();i++)
    { if(car2.Car2[a].name==car1.Car1[i].name&&
         car2.Car2[a].number==car1.Car1[i].number)
         {
             continue;
         }
        car2.Car2[a].name=car1.Car1[i].name;
        car2.Car2[a].number=car1.Car1[i].number;
        a++;

    }
    car2.numb+=(car1.count1()-1);
    for(int i=1;i<car1.count1();i++)
    {car1.Car1[i].name=" ";
    car1.Car1[i].number=" ";
    car1.Car1[i].address=" ";
    car1.Car1[i].Qid=" ";
    }
     car1.num=1;
    cout<<"转移成功"<<endl;
    system("pause");
    system("cls");

}
void People::exchange_car2()
{int a=car1.count1();//手机卡num
if(a+car2.count2()>1005)
{ cout<<"容量已满,操作失败"<<endl;
system("pause");
  return ;

}
    for(int i=1;i<car2.count2();i++)
    {
        if(car1.Car1[a].name==car2.Car2[i].name
           &&car1.Car1[a].number==car2.Car2[i].number)
        {
            continue;
        }
        car1.Car1[a].name=car2.Car2[i].name;
        car1.Car1[a].number=car2.Car2[i].number;
        car1.Car1[a].address=" 0";
        car1.Car1[a].Qid=" 0";
        a++;
    }
    car1.num+=(car2.count2()-1);
    for(int i=0;i<=car2.count2();i++)
    {
        car2.Car2[i].name=" ";
        car2.Car2[i].number=" ";
    }
    cout<<"转移成功"<<endl;
    system("pause");
    system("cls");
    car2.numb=1;

}
void People::copy_car1()  //手机-》手机卡
{
    int a=car2.count2();//手机卡numb
    for(int i=1;i<car1.count1();i++)
    {
        car2.Car2[a].name=car1.Car1[i].name;
        car2.Car2[a].number=car1.Car1[i].number;
        a++;

    }
    car2.numb+=(car1.count1()-1);
    cout<<"复制成功"<<endl;
    system("pause");
    system("cls");

}
void People::copy_car2()
{
    int a=car1.count1();//手机卡num
    for(int i=1;i<car2.count2();i++)
    {
        car1.Car1[a].name=car2.Car2[i].name;
        car1.Car1[a].number=car2.Car2[i].number;
        car1.Car1[a].address="0 ";
        car1.Car1[a].Qid="0 ";
        a++;

    }
    car1.num+=(car2.count2()-1);
    cout<<"复制成功"<<endl;
    system("pause");
    system("cls");

}

Phone.cpp

#include"Phone.h"
istream &operator>>(istream &in,Phone &A)
{
 cout<<"请输入姓名,手机号,地址,QQ号"<<endl;
in>>A.name>>A.number>>A.address>>A.Qid;
return in;
}
ostream &operator<<(ostream &out,Phone &B)
{
  out<<B.name<<"\t"<<B.number<<"\t"<<B.address<<"\t"
  <<B.Qid<<endl;
  return out;
}


Phone_List.cpp

#include"Phone_List.h"
#include<fstream>
#include<bits/stdc++.h>
void swap2(Phone &a,Phone &b)  //交换删除后的信息
{Phone c;
c.name=a.name;
c.address=a.address;
c.Qid=a.Qid;
c.number=a.number;
a.name=b.name;
a.address=b.address;
a.Qid=b.Qid;
a.number=b.number;
b.name=c.name;
b.address=c.address;
b.Qid=c.Qid;
b.number=c.number;
}
 Phone_list::Phone_list()    //从文本文件中读信息并创建数组
{
    for(int i=0;i<1005;i++)
    {

     Car1[i].address=" ";
      Car1[i].name=" ";
       Car1[i].number=" ";
        Car1[i].Qid=" ";
     }

ifstream in("Phone.txt");
if(!in)
{
    cout<<"打开失败"<<endl;
    return ;
}
int i=0;
while(in>>Car1[i].name>>Car1[i].number>>Car1[i].address>>Car1[i].Qid&&EOF)
{
    i++;
}
num=i;
in.close();
}
 Phone_list::~Phone_list()  //写入文本文档
{ ofstream out("Phone.txt");
  if(!out)
  {
      cout<<"打开文件失败"<<endl;
      return;
  }
 out<<"姓名\t"<<"电话\t"<<"地址\t"<<"qq号"<<endl;
 for(int i=1;i<num;i++)
 {out<<Car1[i].name<<"\t"<<Car1[i].number<<"\t"<<Car1[i].address<<"\t"
    <<Car1[i].Qid<<endl;}
out.close();
}
void Phone_list::add()    //添加联系人
{
while(true)
{ int a;
cout<<"1、添加,0、退出"<<endl;
cin>>a;
    if(a==0)
    break;
    if(num>1005)
    {cout<<"容量已满,无法添加"<<endl;
    system("pause");
    return ;}
    cin>>Car1[num];
    num+=1;
    system("pause");
          system("cls");
}
}
void Phone_list::de()    //删除
{ cout<<"请检查一下手机卡联系人中是否也有此人"<<endl;
    while(true)
{
    int a;
cout<<"1、删除,0、退出"<<endl;
cin>>a;
    if(a==0)
      break;
cout<<"请输入要删除联系人的手机号"<<endl;

string b;
cin>>b;
int ans=0;
for(int i=1;i<num;i++)
{
if(Car1[i].number==b)
    {Car1[i].name=" ";
    Car1[i].number=" ";
    Car1[i].address=" ";
    Car1[i].Qid=" ";
   for(int j=i;j<num-1;j++)
    {
        swap2(Car1[j],Car1[j+1]);
   }
    num-=1;
    ans++;
    cout<<"删除成功"<<endl;
    }
}
if(ans==0)
        cout<<"未找到此人"<<endl;
        system("pause");
          system("cls");
}}
void Phone_list::finds()  //查找
{
while(true)
{
    int a;
cout<<"1、查找,0、退出"<<endl;
cin>>a;
    if(a==0)
        break;
        cout<<"请输入要查找联系人的手机号"<<endl;
        cout<<"请检查一下手机卡联系人中是否也有此人"<<endl;
string b;
cin>>b;
int ans=0;
for(int i=1;i<num;i++)
{ if(Car1[i].number==b)
   {
       cout<<Car1[i];
       ans++;
   }
}
if(ans==0)
    cout<<"未找到此人"<<endl;
system("pause");
          system("cls");
}}
void Phone_list::display() //显示
{
    for(int i=0;i<num;i++)
     cout<<Car1[i];
     cout<<"********即将退出显示功能"<<endl;
    system("pause");
    system("cls");
}
void Phone_list::my()  //修改
{
    while(true)
{
    int a;
cout<<"1、修改,0、退出"<<endl;
cin>>a;
    if(a==0)
       break;
cout<<"请输入要修改联系人的手机号"<<endl;
cout<<"请检查一下手机卡联系人中是否也有此人"<<endl;
string b;
cin>>b;
int ans=0;
for(int i=1;i<num;i++)
{ if(Car1[i].number==b)
    { cin>>Car1[i];
    ans++;
    cout<<"修改成功"<<endl;
    }

}
if(ans==0)
    cout<<"未找到此人"<<endl;
 system("pause");
    system("cls");
}
}
int Phone_list::count1()
{
    return num;
}

PhoneCar.cpp

#include"Phonecar.h"
istream &operator>>(istream &input,Phonecar &A)
{
    cout<<"请输入姓名,手机号"<<endl;
input>>A.name>>A.number;
return input;
}
ostream &operator<<(ostream &out,Phonecar &B)
{
  out<<B.name<<"\t"<<B.number<<endl;
  return out;
}
void Phonecar::set(string c,string b)
{
    name=c;
    number=b;
}
void Phonecar::moidfy()
{
   cout<<"1,修改姓名  "<<endl;
   cout<<"2,修改手机号"<<endl;
   cout<<"请输入对应的数字"<<endl;
   int a;
   cin>>a;
   switch(a)
   {case 1:{string p;
    cin>>p;
    name=p;
    break;}
    case 2:{string u;
    cin>>u;
    number=u;
    break;
    }
    default: break;
   }
}

PhoneCar_List.cpp

#include"PhoneCar_List.h"
#include<iostream>
#include<fstream>
using namespace std;
void swap1(Phonecar &a,Phonecar &b)
{ Phonecar c;
    c.name=a.name;
    c.number=a.number;
    a.name=b.name;
    a.number=b.number;
    b.number=c.number;
    b.name=a.name;
}
 PhoneCar_List::PhoneCar_List()    //从文本文件中读信息并创建数组
{ for(int i=0;i<1005;i++)
{
     Car2[i].name=" ";
     Car2[i].number=" ";
}
    ifstream in("PhoneCar.txt");
if(!in)
{
    cout<<"打开失败"<<endl;
    return ;
}
int i=0;
while(in>>Car2[i].name>>Car2[i].number&&EOF)
{
    i++;
}
numb=i;
in.close();
}
 PhoneCar_List::~PhoneCar_List()  //写入文本文档
{ ofstream out("PhoneCar.txt");
  if(!out)
  {
      cout<<"打开文件失败"<<endl;
      return ;}
  out<<"姓名\t"<<"电话"<<endl;
  for(int i=1;i<numb;i++)
  {out<<Car2[i].name<<"\t"<<Car2[i].number<<endl;
  }
out.close();
}
void PhoneCar_List::add()    //添加联系人
{
    while(true)
{ int a;
cout<<"1、添加,0、退出"<<endl;
    cin>>a;
      if(a==0)
        {return;}
        if(numb>1005)
            {cout<<"容量已满,无法添加"<<endl;
            system("pause");
            return;}
          cin>>Car2[numb];
          numb+=1;
          system("pause");
          system("cls");
}
}
void PhoneCar_List::de()    //删除
{ while(true)
{
     int a;
   cout<<"1、删除,0、退出"<<endl;
     cin>>a;

        if(a==0)
       {break;}
  cout<<"请输入要删除联系人的手机号"<<endl;
  string b;
   cin>>b;
   int ans=0;
   int c=0,i;

for( i=1;i<numb;i++)
{ if(Car2[i].number==b)
    {Car2[i].name=" ";
    Car2[i].number=" ";
    cout<<"删除成功"<<endl;
    for(int j=i;j<numb-1;j++)
        {swap1(Car2[j],Car2[j+1]);}
    numb-=1;
    ans++;

    }
}
    if(ans==0)
    cout<<"未找到此人"<<endl;
    system("pause");
    system("cls");
}
}

void PhoneCar_List::finds()  //查找
{
while(true)
{
    int a;
cout<<"1、查找,0、退出"<<endl;
cin>>a;
        if(a==0)
        {break;}
        cout<<"请输入要查找联系人的手机号"<<endl;
        cout<<"请检查一下手机联系人中是否也有此人"<<endl;
  string b;
      cin>>b;
      int ans=0;
    for(int i=1;i<numb;i++)
{ if(Car2[i].number==b)
    {cout<<Car2[i];
    ans++;}

}
if(ans==0)
    cout<<"未找到此人"<<endl;
    system("pause");
    system("cls");
}}
void PhoneCar_List::display() //显示
{
for(int i=0;i<numb;i++)
    cout<<Car2[i];
    cout<<"********即将退出显示功能"<<endl;
    system("pause");
    system("cls");
}
void PhoneCar_List::my()  //修改
{
    while(true)
{
    int a;
cout<<"1、修改,0、退出"<<endl;
cin>>a;
    if(a==0)
       {
       break;}
cout<<"请输入要修改联系人的手机号"<<endl;
cout<<"请检查一下手机联系人中是否也有此人"<<endl;
 string b;
   cin>>b;
   int ans=0;
  for(int i=1;i<numb;i++)
{ if(Car2[i].number==b)
    {cin>>Car2[i];
    ans++;
    cout<<"修改成功"<<endl;
    }

}
if(ans==0)
        cout<<"未找到此人"<<endl;
        system("pause");
        system("cls");
}
}
int PhoneCar_List::count2()
{
    return numb;
}

shown.cpp

#include"shown.h"
#include<iostream>
using namespace std;
void show()
{
    cout<<"********欢迎使用通讯录系统*************"<<endl;
    cout<<"0,退出系统                            "<<endl;
    cout<<"1,添加联系人                          "<<endl;
    cout<<"2,删除联系人                          "<<endl;
    cout<<"3,修改联系人                          "<<endl;
    cout<<"4,查询联系人                          "<<endl;
    cout<<"5,浏览联系人                          "<<endl;
    cout<<"6,将联系人的信息从手机转移到手机卡上  "<<endl;
    cout<<"7, 将联系人的信息从手机卡上转移到手机上"<<endl;
    cout<<"8, 将联系人的信息从手机上复制到手机卡上"<<endl;
    cout<<"9, 将联系人的信息从手机卡上复制到手机上"<<endl;
    cout<<"***************************************"<<endl;
}

下面是源文件对应的头文件
List.h

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
class list1                 //通讯录
{public:
virtual void add()=0;                    //添加联系人
virtual void de()=0;                 //删除联系人
virtual void display()=0;                 // 显示全部联系人
virtual void my()=0;           //   修改联系人
virtual void finds()=0; // 搜索并显示联系人
};


#endif // LIST_H_INCLUDED

List1.h

#ifndef LIST1_H_INCLUDED
#define LIST1_H_INCLUDED
class list1                 //通讯录
{public:
virtual void add()=0;                       //添加联系人
virtual void de()=0;                    //删除联系人
virtual void display()=0;                  // 显示全部联系人
virtual void my()=0;             //   修改联系人
virtual void finds()=0;  // 搜索并显示联系人
};


#endif // LIST1_H_INCLUDED

People.h

#ifndef PEOPLE_H_INCLUDED
#define PEOPLE_H_INCLUDED
#include"Phone_List.h"
#include"PhoneCar_List.h"
#include"List.h"
class People
{public:
    Phone_list car1;
    PhoneCar_List car2;
     void add();   //添加
    void de();    //删除
    void display();  // 展示
    void my();    // 修改
    void finds();  //  查找
    void exchange_car1();   //手机->手机卡
    void exchange_car2();  // 手机卡->手机
    void copy_car1();   //手机复制->手机卡
    void copy_car2();   //手机卡复制->手机

};


#endif // PEOPLE_H_INCLUDED

Phone.h

#ifndef PHONE_H_INCLUDED
#define PHONE_H_INCLUDED
#include"Phonecar.h"
class Phone :public Phonecar     //手机类
{
public:
string address,Qid;
Phone(string c="0",string d="0",string a="0",string b="0")
{ name=a;
number=b;
address=c;
Qid=d;
}
void set(string a,string b,string c,string d);
void moidfy();
friend istream &operator>>(istream &in,Phone &A);
friend ostream &operator<<(ostream &out,Phone &B);
};
istream &operator>>(istream &in,Phone &A);
ostream &operator<<(ostream &out,Phone &B);



#endif // PHONE_H_INCLUDED

Phone_List.h

#ifndef PHONE_LIST_H_INCLUDED
#define PHONE_LIST_H_INCLUDED
#include"List.h"
#include"Phone.h"
using namespace std;
class Phone_list:public list1
{public:
    int num;
    Phone Car1[1005];
    Phone_list();
    ~Phone_list();
    void add();   //
   void de();    //
    void display();  //
    void my();    //
    void finds();  //
    int count1();
};


#endif  PHONE_LIST_H_INCLUDED

Phonecar.h

#ifndef PHONECAR_H_INCLUDED
#define PHONECAR_H_INCLUDED
#include<iostream>
using namespace std;
class Phonecar     //手机卡类
{
public:
string name;
string number;
Phonecar(string a="s",string b="s",int c=0)
{name=a;
number=b;
}
void set(string c,string b);
void moidfy();
friend istream &operator>>(istream &in,Phonecar &A);
friend ostream &operator<<(ostream &out,Phonecar &B);
};
istream &operator>>(istream &input,Phonecar &A);
ostream &operator<<(ostream &out,Phonecar &B);


#endif // PHONECAR_H_INCLUDED

PhoneCar_List.h

#ifndef PHONECAR_LIST_H_INCLUDED
#define PHONECAR_LIST_H_INCLUDED
#include"List"
#include <bits/stdc++.h>
#include"Phonecar.h"
#include"List.h"
//#include"Phone.h"
//#include"Phone_List.h"
#include"PhoneCar_List.h"
class PhoneCar_List:public list1
{ public:
    int numb;
   Phonecar Car2[1005];
    PhoneCar_List();
    ~PhoneCar_List();
    void add();   //添加
    void de();    //删除
    void display();  // 展示
    void my();    // 修改
    void finds();  //  查找
    int count2();  //返回numb值
};
#endif // PHONECAR_LIST_H_INCLUDED

shown.h

#ifndef SHOWN_H_INCLUDED
#define SHOWN_H_INCLUDED
void show();

#endif // SHOWN_H_INCLUDED

源文件和头文件
上图为文件在磁盘中的情况,
下图器在编译器中的代码情况
在编译器中的情况

标签:文件,存储,cout,int,void,Car1,number,通讯录,name
来源: https://blog.csdn.net/ccb1372098/article/details/120711749