简易通讯录代码
最近闲来无事,做个通讯录,最基础的那种,离实用差得远了,纯粹当练手了,所以不要抱有什么幻想,嘿嘿…
主要是有点忘记C++的运算符重载了,另外由于当时学C++时直接把文件那一章跳了过去,也想借此机会练习一下,还有一个重要的原因就是手机要刷机,虽然网上不少备份程序,还是想趁机写个自己用,嘿嘿…
收获有以下几点:
1.使用文件采用程序开始时全部读入,结束后全部输出的方式,整个程序运行时只进行两次文件的读写(这个是学长介绍的方法),由于数据不算庞大,采用这种方法空间效率还可以;
2.把所有操作放在类的构造函数中,这样可以造成自动调用的效果(虽然不知道这样做有木有什么潜在危害);
3.发现原来类型的声明可以放在类中,而且typedef包含的struct声明中可以有类似构造函数的成员函数出现,可以自动完成初始化操作,非常方便,(以前一直用类来着,看起来有点杀鸡用牛刀的感觉);
缺点是:
1.不能按姓氏排列记录;
2.没有检查号码位数以判断输入是否合法的功能;
3.代码不够简练,尤其是按名字和按号码查询可以合成一个函数而我用了两个;
contacts.h
#include<string>
#include<iostream>
using namespace std;
#include<fstream>
class Contacts
{
public:
Contacts(); //构造函数
private:
void UI(); //欢迎界面
void Read(); //从文件读取数据
void SearchName(string); //按名字搜索
void SearchNum(string); //按号码搜索
void Insert(string, string); //插入记录
void ShowAllRecord(); //打印全部记录
void DeleteName(string); //按名字删除
void DeleteNum(string); //按号码删除
void DeleteMenu(); //删除菜单
void Save(); //保存数据到文件
void SearchRecordMenu(); //搜索记录菜单
void InsertRecordMenu(); //插入记录菜单
typedef struct Record
{
string name;
string phonenum;
} Record; //一条记录包括名字和号码
typedef struct ListNode
{
Record record;
ListNode *next;
ListNode()
{
next = NULL;
}
} ListNode; //单条记录节点
typedef struct List
{
ListNode *firstrecord;
int listlen;
List()
{
listlen = 0;
firstrecord = NULL;
}
} List; //通讯录的表存储结构
List l;
};
Contacts::Contacts()
{
Read();
int choice;
while (1)
{
system("cls");
UI();
cout << "1 . 搜索记录" << endl << "2 . 插入记录" << endl << "3 . 删除记录" << endl
<< "4 . 所有记录" << endl << "5 . 退出" << endl << endl;
cout << "您的选择是 ?" << endl;
cin >> choice;
system("cls");
switch (choice)
{
case 1:
SearchRecordMenu();
break;
case 2:
InsertRecordMenu();
break;
case 3:
DeleteMenu();
system("pause");
break;
case 4:
system("cls");
ShowAllRecord();
system("pause");
break;
case 5:
system("cls");
Save();
return;
}
}
}
void Contacts::UI() //用户界面
{
cout << "************************************************************"
<< endl
<< "* *"
<< endl
<< "* *"
<< endl << "* 简 易 通 讯 录 *"
<< endl
<< "* *"
<< endl
<< "* *"
<< endl
<< "************************************************************"
<< endl;
}
void Contacts::Read()
{
ifstream infile;
infile.open("data.txt", ios::in);
if (infile != NULL)
{
string name, num;
ListNode *p;
while (infile >> name >> num)
{
if (l.firstrecord == NULL)
{
l.firstrecord = new ListNode;
l.firstrecord->record.name = name;
l.firstrecord->record.phonenum = num;
p = l.firstrecord;
}
else
{
p->next = new ListNode;
p = p->next;
p->record.name = name;
p->record.phonenum = num;
}
}
}
infile.close();
}
void Contacts::Save()
{
ofstream outfile;
outfile.open("data.txt", ios::out);
if (outfile != NULL)
{
ListNode *p = l.firstrecord;
while (p != NULL)
{
outfile << p->record.name << " " << p->record.phonenum << endl;
p = p->next;
}
}
outfile.close();
}
void Contacts::SearchName(string name) //按名字搜索记录
{
system("cls");
ListNode *p = l.firstrecord;
bool find = false;
while (p != NULL)
{
if (p->record.name == name)
{
cout << p->record.name << " : " << p->record.phonenum << endl;
find = true;
}
p = p->next;
}
if (find == false)
{
cout << "未找到记录 !" << endl << "插入这条记录 ?(y/n)" << endl;
char choice;
string num;
while (1)
{
cin >> choice;
system("cls");
switch (choice)
{
case 'y':
cout << "请输入" << name << "的电话号码:" << endl;
cin >> num;
system("cls");
Insert(name, num);
return;
case 'n':
return;
default:
cout << "插入这条记录 ?(y/n)" << endl;
break;
}
}
}
}
void Contacts::SearchNum(string num) //按号码搜索记录
{
system("cls");
ListNode *p = l.firstrecord;
bool find = false;
while (p != NULL)
{
if (p->record.phonenum == num)
{
cout << p->record.name << " : " << p->record.phonenum << endl;
find = true;
}
p = p->next;
}
if (find == false)
{
cout << "未找到记录 !" << endl << "插入这条记录 ?(y/n)" << endl;
char choice;
string name;
while (1)
{
cin >> choice;
system("cls");
switch (choice)
{
case 'y':
cout << "请输入号码" << num << "的机主姓名" << endl;
cin >> name;
system("cls");
Insert(name, num);
return;
case 'n':
return;
default:
cout << "插入这条记录 ?(y/n)" << endl;
break;
}
}
}
}
void Contacts::Insert(string name, string num) //插入记录
{
ListNode *p = l.firstrecord;
if (p == NULL)
{
l.firstrecord = new ListNode;
l.firstrecord->record.name = name;
l.firstrecord->record.phonenum = num;
}
else
{
while (p->next != NULL)
p = p->next;
p->next = new ListNode;
p = p->next;
p->record.name = name;
p->record.phonenum = num;
}
}
void Contacts::ShowAllRecord()
{
ListNode *p;
if ((p = l.firstrecord) == NULL)
{
cout << "没有记录 !" << endl;
return;
}
while (p != NULL)
{
cout << p->record.name << " : " << p->record.phonenum << endl;
p = p->next;
}
}
void Contacts::SearchRecordMenu()
{
int choice;
char quit;
string name, num;
while (1)
{
cout << "按名字(1)还是按号码(2)进行搜索 ?" << endl << endl;
cin >> choice;
system("cls");
switch (choice)
{
case 1:
cout << "请输入您要搜索的名字 :" << endl << endl;
cin >> name;
system("cls");
SearchName(name);
break;
case 2:
cout << "请输入您要搜索的电话号码 :" << endl << endl;
cin >> num;
system("cls");
SearchNum(num);
break;
}
cout << "退出 ?(y/n)" << endl << endl;
cin >> quit;
system("cls");
if (quit == 'y')
break;
else if (quit == 'n')
continue;
else
{
cout << "选择错误 !" << endl << endl;
break;
}
}
}
void Contacts::InsertRecordMenu()
{
char quit;
string name, num;
while (1)
{
cout << "请输入您要记录的名字和号码(之间用空格隔开) :" << endl << endl;
cin >> name >> num;
system("cls");
Insert(name, num);
cout << "退出 ?(y/n)" << endl << endl;
cin >> quit;
system("cls");
if (quit == 'y')
break;
else if (quit == 'n')
continue;
else
{
cout << "错误 !" << endl << endl;
break;
}
}
}
void Contacts::DeleteMenu()
{
int choice;
string name, num;
cout << "按姓名(1)还是按号码(2)删除记录?" << endl;
cin >> choice;
system("cls");
switch (choice)
{
case 1:
cout << "请输入您要删除的姓名:" << endl;
cin >> name;
system("cls");
DeleteName(name);
break;
case 2:
cout << "请输入您要删除的号码:" << endl;
cin >> num;
system("cls");
DeleteNum(num);
break;
}
}
void Contacts::DeleteName(string name)
{
ListNode *pre, *p;
pre = p = l.firstrecord;
if (p != NULL)
{
while (p != NULL && p->record.name != name)
{
pre = p;
p = p->next;
}
if (p == NULL)
{
cout << "未找到相应记录!" << endl;
return;
}
else
{
if (p->next == NULL)
{
if (pre == p)
{
l.firstrecord = NULL;
delete p;
}
else
{
pre->next = NULL;
delete p;
}
}
else
{
if (pre == p)
{
l.firstrecord = l.firstrecord->next;
delete p;
}
else
{
pre->next = p->next;
delete p;
}
}
cout << "删除成功!" << endl;
}
}
}
void Contacts::DeleteNum(string num)
{
ListNode *pre, *p;
pre = p = l.firstrecord;
if (p != NULL)
{
while (p != NULL && p->record.phonenum != num)
{
pre = p;
p = p->next;
}
if (p == NULL)
{
cout << "未找到相应记录!" << endl;
return;
}
else
{
if (p->next == NULL)
{
if (pre == p)
{
l.firstrecord = NULL;
delete p;
}
else
{
pre->next = NULL;
delete p;
}
}
else
{
if (pre == p)
{
l.firstrecord = l.firstrecord->next;
delete p;
}
else
{
pre->next = p->next;
delete p;
}
}
}
cout << "删除成功!" << endl;
}
}
main.cpp
#include"Contacts.h"
int main()
{
Contacts contact;
}