您的位置:首页 > 编程语言 > C语言/C++

通讯录管理系统设计与实现

2017-01-03 10:10 453 查看

说明:所有源码已上传到笔者GitHub上,欢迎follow。感谢!!!

一、内容和要求

(1)完成通信录信息的管理,包括添加、修改、删除、查询功能。尽量丰富联系人的各项信息。

(2)能够根据联系的信息单独或分类查询。

(3)联系人的生日提醒功能。

(4)数据存储功能可以使用文件的块读写操作完成,也可以使用SQLite数据库(要学习如何用C/C++连接SQLite数据库)

二、需求分析

(1)添加:在系统中添加新成员,并保存在文件“通讯录.txt”中;

(2)修改:读入要修改成员的姓名,按照提示进行修改;

(3)删除:读入要删除成员的姓名,将进行删除;

(4)保存:将所有成员数据保存在文件“通讯录.txt”中;

(5)查找:读入要查找成员的姓名,查找成员将显示在屏幕上;

三、实现代码

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
static int count=0;
class telephone
{
char name[20];                                        //姓名
char number[17];                                      //电话号码
char birthday[18];                                    //生日
int year,month,day,hour,minute,age;

public:
telephone(char *na,char *nu,int y,int m,int d,int h,int mi,int a);
telephone(){};
//   ~telephone();
char *getname();                                      //获得姓名
char *getnumber();                                    //获得电话号码
int getyear();                                        //获得年份
int getmonth();                                       //获得月份
int getday();                                         //获得日
int gethour();                                        //获得小时
int getminute();                                      //获得分钟
int getage();                                         //获得年龄
void display();                                       //输出信息
void input();                                         //添加信息
void insert();                                        //插入信息
void Delete();                                        //删除信息
void change();                                        //改变信息

static int getcount();
};
//int telephone::count=0;
telephone::telephone(char *na,char *nu,int y,int m,int d,int h,int mi,int a)
{
strcpy(name,na);
strcpy(number,nu);
year=y;
month=m;
day=d;
hour=h;
minute=mi;
age=a;
//  count++;
}
char *telephone::getname()                                       //获得姓名
{
return name;
}
char *telephone::getnumber()                                     //获得电话号码
{
return number;
}
int telephone::getyear()                                         //获得年份
{
return year;
}
int telephone::getmonth()                                        //获得月份
{
return month;
}
int telephone::getday()                                          //获得日
{
return day;
}
int telephone::gethour()                                         //获得小时
{
return hour;
}
int telephone::getminute()                                       //获得分钟
{
return minute;
}
int telephone::getage()                                          //获得年龄
{
return age;
}
void telephone::display()                                        //输出信息
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"号码:"<<number<&l
4000
t;endl;
cout<<"生日:"<<hour<<":"<<minute<<"  "<<year<<"-"<<month<<"-"<<day<<endl;
}
void telephone::input()                                          //输入信息
{
char na[20];
cout<<"输入姓名:";
cin>>na;
strcpy(name,na);
cout<<"输入年龄:";
cin>>age;
cout<<"输入电话号码:";
cin>>number;
cout<<"输入生日(按时、分、年、月、日的顺序):";
cin>>hour>>minute>>year>>month>>day;
count++;
}
void telephone::insert ()                                        //插入信息
{
if(!age)
input();
}
void telephone::Delete ()                                       //删除信息
{
strcpy(name,(this+1)->name);
strcpy(number,(this+1)->number);
hour=(this+1)->hour;
minute=(this+1)->minute;
year=(this+1)->year;
month=(this+1)->month;
day=(this+1)->day;
age=(this+1)->age;
count--;

}
void telephone::change()                                       //改变信息
{
cout<<"please input again!"<<endl;
input();
}
int telephone::getcount()
{
return count;
}
const int N=20;
void menu();
void createfile(char *a,telephone *array);
void outputtel(telephone *array);
void inputtel(telephone *array);
int  searchtel(telephone *array,char *na);
bool inserttel(telephone *array);
bool Deletetel(telephone *array,char *na);
int main()
{
telephone array
;
int choice;                                                //读入选项
do
{
menu();
cout<<"please input your choice:";
cin>>choice;
if(choice>=0 && choice<=6)
switch(choice)
{
case 1:inputtel(array);  break;
case 2:cout<<"input the name searched"<<endl;
char na[20];
cin>>na;
int i;
i=searchtel(array,na);
if(i==N)
cout<<"无此人!\n";
else
array[i].display();
break;
case 3:outputtel(array);break;
case 4:if(inserttel(array))
cout<<"成功插入一条记录!"<<endl;
else
cout<<"插入失败!"<<endl;
break;
case 5:
cout<<"input the name deleted:"<<endl;
char na1[20];
cin>>na1;
//              system("cls");
if(Deletetel(array,na1))
cout<<"成功删除一条记录!"<<endl;
else
cout<<"删除失败!"<<endl;
break;
case 6:
cout<<"input the name changed:"<<endl;
cin>>na;
i=searchtel(array,na);
if(i==N)
cout<<"没有此联系人,改变失败"<<endl;
else
cout<<"被改变联系人的信息为:"<<endl;
array[i].display ();
cout<<"改变后的信息为:"<<endl;
array[i].Delete ();
array[i].change ();
break;
default:break;
}
}while(choice);
createfile("通讯录.txt",array);
return 0;
}
void menu()                                                     //定义菜单函数
{
cout<<"*********1.添加联系人*********"<<endl;
cout<<"*********2.查询联系人信息*********"<<endl;
cout<<"*********3.浏览联系人信息*********"<<endl;
cout<<"*********4.插入联系人*********"<<endl;
cout<<"*********5.删除联系人*********"<<endl;
cout<<"*********6.改变联系人信息*********"<<endl;
cout<<"*********0.退    出*********"<<endl;
}
void createfile(char *a,telephone *array)
{
ofstream outf(a);
if(!outf)
{
cout<<"can't open the file\n";
return;
}
for(int i=0;i<count;i++)
{
outf<<"姓名:";
outf<<"   "<<array[i].getname ()<<endl;
outf<<"电话:";
outf<<"   "<<array[i].getnumber ()<<endl;
outf<<"年龄:";
outf<<"   "<<array[i].getage ()<<endl;
outf<<"生日:";
outf<<"   "<<array[i].gethour ()<<array[i].getminute();
outf<<array[i].getyear ()<<"-"<<array[i].getmonth ()<<"-"<<array[i].getday ()<<endl;
}
outf.close ();
}
void outputtel(telephone *array)                                //输出对象信息
{
cout<<"学生总数="<<count<<endl;
for(int i=0;i<count;i++)
array[i].display();
}
int searchtel(telephone *array,char *na)                       //按姓名查找
{
int i,j=N;
for(i=0;i<N;i++)
if(strcmp(array[i].getname(),na)==0)
{
j=i;
break;
}
return j;
}
void inputtel(telephone *array)                               //输入对象元素
{
char ch;
int i=0;
do
{
if(telephone::getcount()==N)
cout<<"人数已满,无法继续录入!"<<endl;
if(!array[i].getage())
array[i++].input();
array[count].input();

cout<<"继续输入吗?(Y or N)"<<endl;
cin>>ch;
}while(ch=='Y');
}
bool inserttel(telephone *array)                               //根据年龄插入
{
if(telephone::getcount()==N)
{
cout<<"人数已满,无法插入!"<<endl;
return false;
}
//  else
//  for(int j=0;j<N;j++)
//  {
//      if(array[j].getage())
//      {
//          cout<<"无法插入!";
//          return false;
//      }
//  }

for(int i=0;array[i].getage() ;i++);
array[i].insert();
return true;
}
bool Deletetel(telephone *array,char *na)                //按姓名删除
{
if(telephone::getcount()==0)
{
cout<<"没有记录,无法删除!"<<endl;
return false;
}
int i=searchtel(array,na);
if(i==N)
{
cout<<"查无此人,无法删除!"<<endl;
return false;
}
array[i].Delete();
return true;
}


四、运行结果









转载请注明!谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息