用C++制作一款电话簿

这是一款可以支持添加联系人,删除联系人,查找联系人等多功能的电话簿,有一个高尚一点的名字是通讯管理系统,尽管这一部分代码比较长,敲了一个小时才敲完,但是内容比较简单,是C++入门后可选择敲的一个程序。话不多说看代码~

# include <iostream>
# include <string>
# define MAX 1000
using namespace std;
struct Person
{
 string name;
 int sex;
 int age;
 string phone;
 string addr;
};
struct Addressbooks
{
 Person personArray[MAX];
 int m_Size = 0;
};
void showMenu()
{
 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;
}
void AddPerson(Addressbooks * abs)
{
 string name;
 cout << "请输入联系人的姓名:" << endl;
 cin >> name;
 abs->personArray[abs->m_Size].name = name;
 int sex;
 cout << "请输入性别:" << endl;
 cout << "1 --- 男" << endl;
 cout << "2 --- 女" << endl;
 while (true)
 {
  cin >> sex;
  if (sex == 1 || sex == 2)
  {
   abs->personArray[abs->m_Size].sex = sex;
   break;
  }
  cout << "输入有误,请重新输入!" << endl;
 }
 int age;
 cout << "请输入年龄:" << endl;
 cin >> age;
 abs->personArray[abs->m_Size].age = age;
 string phone;
 cout << "请输入电话号码:" << endl;
 cin >> phone;
 abs->personArray[abs->m_Size].phone = phone;
 string homeplace;
 cout << "请输入家庭住址:" << endl;
 cin >> homeplace;
 abs->personArray[abs->m_Size].addr = homeplace;
 abs->m_Size++;
 cout << "当前已成功添加" <<abs->m_Size<<"名联系人" <<endl;
 system("pause");
 system("cls");
}
void ShowPerson(Addressbooks * abs)
{
 if (abs->m_Size == 0)
 {
  cout << "当前通讯录为空" << endl;
 }
 else
 {
  for (int i = 0; i < abs->m_Size; i++)
  {
   cout << "姓名: " << abs->personArray[i].name << "\t";
   cout << "性别: " << (abs->personArray[i].sex == 1 ?"男":"女") << "\t";
   cout << "年龄: " << abs->personArray[i].age << "\t";
   cout << "电话: " << abs->personArray[i].phone << "\t";
   cout << "家庭住址: " << abs->personArray[i].addr<< endl;
  }
 }
 system("pause");
 system("cls");
}
int isExist(Addressbooks * abs, string name)
{
 for (int i = 0; i < abs->m_Size; i++)
 {
  if (abs->personArray[i].name == name)
   return i;
 }
 return -1;
}
void Delete(Addressbooks * abs)
{
 string name;
 cout << "请输入您想删除的联系人的姓名: " << endl;
 cin >> name;
 int ret = isExist(abs, name);
 if (ret != -1)
 {
  for (int i = ret; i < abs->m_Size;i++)
  {
   abs->personArray[i] = abs->personArray[i+1];
  }
  cout << "删除成功!" << endl;
  abs->m_Size--;
 }
 else
 {
  cout << "查无此人" << endl;
 }
 system("pause");
 system("cls");
}
void Search(Addressbooks * abs)
{
 string name;
 cout << "请输入您要找的联系人" << endl;
 cin >> name;
 int ret = isExist(abs,name);
 cout << "姓名: " << abs->personArray[ret].name << "\t";
 cout << "性别: " << (abs->personArray[ret].sex == 1 ? "男" : "女") << "\t";
 cout << "年龄: " << abs->personArray[ret].age << "\t";
 cout << "电话: " << abs->personArray[ret].phone << "\t";
 cout << "家庭住址: " << abs->personArray[ret].addr << endl;
 system("pause");
 system("cls");
}
void Modify(Addressbooks * abs)
{
 string name;
 cout << "请输入您要修改的联系人的姓名: " << endl;
 cin >> name;
 int ret = isExist(abs, name);
 if (ret != -1)
 {
  system("cls");
  showMenu();
  cout << "已找到该联系人,请修改: " << endl;
  cout << "姓名 : " << endl;
  cin >> abs->personArray[ret].name;
  cout << "性别 : " << endl;
  cin >> abs->personArray[ret].sex;
  cout << "年龄 : " << endl;
  cin >> abs->personArray[ret].age;
  cout << "电话号码 :" << endl;
  cin >> abs->personArray[ret].phone;
  cout << "家庭住址 :" << endl;
  cin >> abs->personArray[ret].addr;
  cout << "修改成功!" << endl;
 }
 system("pause");
 system("cls");
}
void allClean(Addressbooks * abs)
{
 cout << "您确定是要执行本操作吗?" << endl;
 cout << "1 --- 是" << endl;
 cout << "2 --- 否" << endl;
 int select;
 cin >> select;
 if (select == 1)
 {
  abs->m_Size = 0;
  cout << "通讯录已清空!" << endl;
 }
 system("pause");
 system("cls");
}
int main(void)
{
 int select;
 Addressbooks abs;
 while (true)
 {
  showMenu();
  cout << "请输入您的选择:" << "  ";
  cin >> select;
  switch (select)
  {
  case 1:
   AddPerson(&abs);
   break;
  case 2:
   ShowPerson(&abs);
   break;
  case 3:
   Delete(&abs);
   break;
  case 4:
   Search(&abs);
   break;
  case 5:
   Modify(&abs);
   break;
  case 6:
   allClean(&abs);
   break;
  case 0:
   cout << "欢迎您下次再用!" << endl;
   return 0;
  }
 }
 return 0;
}



```![开始界面](https://img-blog.csdnimg.cn/20200301092423922.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01SX2JhaTE=,size_16,color_FFFFFF,t_70)
![删除联系人
](https://img-blog.csdnimg.cn/20200301092521637.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01SX2JhaTE=,size_16,color_FFFFFF,t_70)![添加联系人](https://img-blog.csdnimg.cn/20200301092455289.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01SX2JhaTE=,size_16,color_FFFFFF,t_70)
适用于C++初学者 /************************************************* Copyright (C), 2008- , mekinglong of cumtcs File name:面向对象8(面向对象8.cpp) Author: 计07-2 秦杰 Version: 1.00 Date: 08.11.12 Description:编写程序实现一个简单的电话记录簿,要求记录的个人信息包括: 姓名,单位,家庭电话,移动电话。具体功能如下: 1.创建信息链表并以磁盘文件保存。 2.读取磁盘文件并显示输出所有人的移动电话。 3.按姓名或单位查询家庭电话。 4.通过姓名和单位确定个人,修改其电话信息并存盘. Others: .... Function List: // 主要函数列表,每条记录应包括函数名及功能简要说明 1.main() 完成各种提示与主操作输入输出 History: <author> <time> <version > <desc> *****************************************************/ #include <iostream> #include <assert.h> #include <string> #include <cstring> #include <fstream> #include <iomanip> using namespace std; class telist; class Node { private: char name[30]; char workplace[50]; char hometel[50]; char mobiletel[50]; Node *next; public: friend class telist; friend void load(telist&); }; class telist { private: Node *head; Node *tail; public: telist() { head=tail=NULL; } telist(Node* h,Node *t):tail(t),head(h){} void add(); void del(); void show(); Node* search(); void change(); void store(); Node* find1(); Node* find2(); friend void load(telist&); }; void operate(string ,telist &); void load(telist &); int main() { system("cls"); cout<<"欢迎使用电话簿管理系统:\n"; telist tele; load(tele); string strord; do { cout<<"输入你的操作:\n"; cout<<"1.加入新的电话记录。\n"; cout<<"2.显示输出所有人的移动电话\n"; cout<<"3.按姓名或单位查询家庭电话\n"; cout<<"4.通过姓名和单位确定个人,修改其电话信息并存盘.\n"; cout<<"5.删除一个信息并存盘。\n"; cout<<"0.退出程序。\n"; cin>>strord; operate(strord,tele); system("cls"); }while(strord!="0") ; system("pause"); system("cls"); return 0; } void operate(string str,telist &t) { if(str=="1") t.add(); else if(str=="2") t.show(); else if(str=="3") t.search(); else if(str=="4") t.change(); else if(str=="5") t.del(); else if(str=="0") t.store(); else cout<<"输入错误!\n"; system("pause"); } void load(telist &t) { ifstream infile("电话簿.dat",ios::in|ios::binary); if(infile == NULL) { cerr<<"打开电话簿失败!!\n"; t.head=t.tail=NULL; return; } if(!infile.eof()) { cout<<"没有数据\n"; t.head=t.tail=NULL; return ; } Node *temp=new Node; t.head=temp; Node *temp2; infile.read ((char*)temp,sizeof(Node)); while(!infile.eof()||temp->next==NULL) // { temp->next=new Node; temp2=temp; temp=temp->next; infile.read ((char*)temp,sizeof(*temp)); t.tail=temp; } temp->next=NULL; delete(temp); t.tail=temp2; t.tail->next=NULL; // delete temp; assert(t.tail->next==NULL); infile.close(); // delete infile; } void telist::add() { if(head==tail&&tail==NULL) { Node * newnode; newnode=head=tail=new Node; if(!newnode) { cout<<"警告:内存分配失败!\n"; return; }//if cout<<"姓名:\n"; cin>>newnode->name; cout<<"工作单位:\n"; cin>>newnode->workplace; cout<<"家庭电话:\n"; cin>>newnode->hometel; cout<<"移动电话:\n"; cin>>newnode->mobiletel; newnode->next=NULL; }//if else{ Node *temp; temp=new Node; tail->next = temp; if(!temp) { cout<<"警告:内存分配失败!\n"; return; } cout<<"姓名:\n"; cin>>temp->name; cout<<"工作单位:\n"; cin>>temp->workplace; cout<<"家庭电话:\n"; cin>>temp->hometel; cout<<"移动电话:\n"; cin>>temp->mobiletel; temp->next=NULL; tail=temp; }//else } void telist::del() { cout<<"先确定你要删除的人:\n"; Node *temp=search(); Node *p=head; if(temp==NULL) { cout<<"没找到要找的人!\n"; return ; } if(p==temp) { cout<<"确定删除?(y或n)"; char ord1; cin>>ord1; if(ord1=='N'||ord1=='n') return; if(ord1=='Y'||ord1=='y') { head=head->next; if(p==tail) tail=NULL;//wenti zaizheli } } else { for(;(p->next)!=temp;) { p=p->next; } cout<<"确定删除?(y或n)"; char ord; cin>>ord; if(ord=='N'||ord=='n')return; if(ord=='Y'||ord=='y')//if1 { if(temp==tail)//if2 { delete temp; tail=p; }//if2 else { p->next=temp->next; delete temp; }//else }//if1 }//else store(); }//del void telist::show() { Node *temp=head; for(;temp!=NULL;) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; temp=temp->next; } return; } Node* telist::search() { // char sname[30]; // char sworkplace[50]; int ord; cout<<"按照什么查询?\n"; cout<<"1.名字。\n"; cout<<"2.工作单位。\n"; cin>>ord; for(;!cin;) { cout<<"error!"; cin>>ord; } switch(ord) { case 1:return(find1());break; case 2:return(find2());break; default:cout<<"警告:操作错误!\n";return 0; } } Node* telist::find1() { char sn[30]; cout<<"请输入姓名:\n"; cin>>sn; cout<<endl; Node *temp=head; for(;temp;temp=temp->next) { if(!strcmp(sn,temp->name)) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; return temp; } if(temp->next==NULL) { cout<<"NO FIND!\n"; return NULL; } else return NULL; } } Node* telist::find2() { char sw[50]; cout<<"请输入工作单位:\n"; cin>>sw; cout<<endl; Node *temp=head; for(;temp;temp=temp->next) { if(!strcmp(sw,temp->workplace)) { cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace; cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl; return temp; break; } if(temp->next==NULL) { cout<<"NO FIND!\n"; return NULL; } else return NULL; } } void telist::store() { Node * temp=head; ofstream outfile("电话簿.dat",ios::out|ios::binary); if(!outfile) { cout<<"打开文件失败!\n"; return ; } for(;temp;) { outfile.write ((char *)temp,sizeof(*temp)); temp=temp->next ; } outfile.close(); } void telist::change() { Node *temp=search(); cout<<"请输入新的记录:\n"; cout<<"姓名:\n"; cin>>temp->name; cout<<"工作单位:\n"; cin>>temp->workplace; cout<<"家庭电话:\n"; cin>>temp->hometel; cout<<"移动电话:\n"; cin>>temp->mobiletel; store(); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值