一.
课题研究现状
手机通讯录在我们生活中应用广泛,通讯录作为通讯录
地址的书本,当今的通讯录可以涵盖多项内容,实现了
人与人之间的交流需求。可利用结构体、函数等知识,
实现了清空,排序等功能。
二.技术支持
(1)结构体
(2)定义函数
(3)指针
((4)realloc函数
三、计划实现的功能框架
1.添加联系人
2.删除联系人
3.显示联系人
4.查找联系人(根据首字母查找)
5.更改联系人信息
6.清空通讯录
7.收藏联系人
你看一下哈,可以参考。有注释,不清楚的可以问
#include <iostream>
#include <fstream>
#include <string>
#include<stdlib.h>
#include<windows.h>
#include <string.h>
using namespace std;
/*
1.手机卡联系人类:表示一个联系人
数据成员包括:
姓名
电话号码
成员函数包括
带参并带默认值的构造函数
一组set函数为数据成员赋值
一组modify函数,修改数据成员的值
重载>>,<<运算符,完成对象的输入和输出操作
*/
class Mobilecardcontact
{
public:
string name;
string number;
/*
带参并带默认值的构造函数
*/
Mobilecardcontact(string a = "a", string b = "b")
{
name = a; number = b;
}
/*
重载>>,<<运算符,完成对象的输入和输出操作
*/
friend istream& operator>>(istream& cin, Mobilecardcontact& b);
friend ostream& operator<<(ostream& cout, Mobilecardcontact& b);
int set(string a, string b);
int modify();//修改数据成员的值
};
istream& operator>>(istream& cin, Mobilecardcontact& b)
{
cout << "请输入姓名:" << endl;
cin >> b.name;
cout << "请输入电话号码:" << endl;
cin >> b.number;
return cin;
}
ostream& operator<<(ostream& cout, Mobilecardcontact& b)
{
cout << "姓名:" << b.name << endl;
cout << "电话号码:" << b.number << endl;
return cout;
}
int Mobilecardcontact::set(string a,string b)
{
name = a; number = b; return 0;
}
int Mobilecardcontact::modify()
{
cout << "请选择要修改的信息:" << endl;
cout << "修改姓名请输入1" << endl;
cout << "修改号码输入2" << endl;
int n; cin >> n;
if (n == 1)
{
string a; cout << "请输入要修改成的名字" << endl;
cin >> a; name = a;
}
else if (n == 2)
{
string a; cout << "请输入要修改成的电话号码" << endl;
cin >> a; number = a;
}
return 0;
}
/*
2.手机联系人(继承于手机卡联系人类):
新增数据成员:
籍贯
QQ号
成员函数包括
一组set函数为数据成员赋值
一组modify函数,修改数据成员的值
重载>>,<<运算符,完成数据成员的输入输出操作
*/
class Mobilecontact :public Mobilecardcontact
{
public:
string nativeplace;//籍贯
string qqnumber;//QQ号
Mobilecontact(string a = "a", string b = "b", string c = "c", string d = "d") :Mobilecardcontact(a, b)
{
nativeplace = c; qqnumber = d;
}
friend istream& operator>>(istream& cin, Mobilecontact& b);
friend ostream& operator<<(ostream& cout, Mobilecontact& b);
int set(string a, string b);
int modify();//修改数据成员的值
};
int Mobilecontact::set(string a, string b)
{
nativeplace = a; qqnumber = b; return 0;
}
int Mobilecontact::modify()
{
cout << "请选择要修改的信息:" << endl;
cout << "修改姓名请输入1" << endl;
cout << "修改号码请输入2" << endl;
cout << "修改籍贯请输入3" << endl;
cout << "修改QQ号码请输入4" << endl;
int n; cin >> n;
if (n == 1)
{
string a; cout << "请输入要修改成的名字" << endl;
cin >> a; name = a;
}
else if (n == 2)
{
int a; cout << "请输入要修改成的电话号码" << endl;
cin >> a; number = a;
}
else if (n == 3)
{
string a; cout << "请输入要修改成的籍贯" << endl;
cin >> a; nativeplace = a;
}
else if (n == 4)
{
int a; cout << "请输入要修改成的QQ号码" << endl;
cin >> a; qqnumber = a;
}
return 0;
}
istream& operator>>(istream& cin, Mobilecontact& b)
{
cout << "请输入姓名:" << endl; cin >> b.name;
cout << "请输入电话号码:" << endl; cin >> b.number;
cout << "请输入籍贯:" << endl; cin >> b.nativeplace;
cout << "请输入QQ号码:" << endl; cin >> b.qqnumber;
return cin;
}
ostream& operator<<(ostream& cout, Mobilecontact& b)
{
cout << "姓名:" << b.name << endl;
cout << "电话号码:" << b.number << endl;
cout << "籍贯:" << b.nativeplace << endl;
cout << "QQ号码:" << b.qqnumber << endl;
return cout;
}
/*
3.定义一个通讯簿抽象类,用来封装以下函数(为支持多态,可以将以下函数封装为纯虚函数)
增加函数:增加一个联系人
删除操作:删除一个联系人
Display:显示所有联系人的信息
修改某一联系人的信息:
查询并显示某一联系人的信息:
*/
class addressbook
{
public:
addressbook() { }
virtual int addcontacter() = 0;
virtual int inquire() = 0;
virtual int deletecontacter() = 0;
virtual int modification() = 0;
virtual int showallcontacter() = 0;
virtual int unloading() = 0;
~addressbook() { }
};
/*
5.手机卡通讯簿类(这是一个数据库类,继承于通讯簿抽象类):用于记录手机中存储的所有联系人的信息
数据成员包括:
手机联系人的数量
手机联系人对象数组
成员函数包括
构造函数:读取文本文件中的数据,并根据文件内容创建联系人对象数组
析构函数:将对象数组中的内容写入到文本文件中。
增加函数:增加一个联系人
删除操作:删除一个联系人
Display:显示所有联系人的信息
修改某一联系人的信息:
查询并显示某一联系人的信息:
*/
class Mobilecardaddressbook :public addressbook
{
public:
Mobilecardaddressbook()
{
cout << " ********************************" << endl;
cout << " * 欢迎来到手机卡通讯簿 *" << endl;
cout << " ********************************" << endl;
}
int addcontacter();
int inquire();
int deletecontacter();
int modification();
int showallcontacter();
int unloading();
~Mobilecardaddressbook()
{
cout << " ********************************" << endl;
cout << " * 成功离开手机通讯簿 *" << endl;
}
};
int Mobilecardaddressbook::addcontacter()
{
int m;
string a[1000][2];
fstream infile("Mobilecard contact.txt", ios::in);//打开源目录下已建好的手机卡通讯录
if (!infile)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i <= 1000; i++)
{
for (int j = 0; j < 2; j++)
{
infile >> a[i][j];
}
m = i;
if (a[i][0] == "")
{
break;
}
}
cout << "现在已经有:" << m << "个联系人" << endl;
if (m < 1000)
{
// cout << "请输入你想要添加到手机卡的联系人的姓名:" << endl;
// cout << "请输入你想要添加到手机卡的联系人的电话号码:" << endl;
Mobilecardcontact A;
cin >> A;
cout << "您刚刚输入的信息为:" << endl;
cout << A;
fstream infile("Mobilecard contact.txt", ios::app | ios::out);
if (!infile)
{
cout << "打开手机卡内存文件失败!" << endl;
}
infile << "姓名:" << A.name << endl;
infile << "电话号码:" << A.number << endl;
infile.close();
}
else if (m >= 1000)
{
cout << "无法添加" << endl;
}
return 0;
}
int Mobilecardaddressbook::inquire()
{
string a[1000][2];
cout << "请输入你想要查询联系人的姓名:" << endl;
string b; cin >> b;
string d; d = "姓名:" + b;
fstream infile("Mobilecard contact.txt", ios::in);
if (!infile)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i <= 1000; i++)
{
for (int j = 0; j < 2; j++)
{
infile >> a[i][j];
}
if (d == a[i][0])
{
cout << "您查找的联系人信息为:" << endl;
cout << d << endl;
cout << a[i][1] << endl;
}
}
infile.close();
return 0;
}
int Mobilecardaddressbook::modification()
{
string a[1000][2];
int m;
cout << "请输入你想要修改的联系人的电话号码:" << endl;
string b; cin >> b;
string d; d = "电话号码:" + b;
fstream infile("Mobilecard contact.txt", ios::in);
if (!infile)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i <= 1000; i++)
{
for (int j = 0; j < 2; j++)
{
infile >> a[i][j];
}
m = i;
if (a[i][0] == "")
{
break;
}
}
infile.close();
for (int i = 0; i <= m; i++)
{
if (d == a[i][1])
{
cout << "请输入您要修改成的姓名:" << endl;
string s, b;
cin >> s;
a[i][0] = "姓名:" + s;
cout << "请输入您要修改成的电话号码:" << endl;
cin >> b;
a[i][1] = "电话号码:" + b;
cout << "请输入您要修改成的籍贯:" << endl;
cout << "修改成功!" << endl;
}
}
fstream infile2("Mobilecard contact.txt", ios::out);
if (!infile2)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i <= m; i++)
{
for (int j = 0; j < 2; j++)
{
if (a[i][0] == "1" || a[i][1] == "1")
{
break;
}
else
{
infile2 << a[i][j] << endl;
}
}
}
infile2.close();
return 0;
}
int Mobilecardaddressbook::deletecontacter()
{
string a[1000][2];
int m;
cout << "请输入你想要删除的联系人的电话号码:" << endl;
string b; cin >> b;
string d; d = "电话号码:" + b;
fstream infile("Mobilecard contact.txt", ios::in);
if (!infile)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i <= 1000; i++)
{
for (int j = 0; j < 2; j++)
{
infile >> a[i][j];
}
m = i;
if (a[i][0] == "")
{
break;
}
}
infile.close();
for (int i = 0; i <= m; i++)
{
if (d == a[i][1])
{
a[i][0] = "1";
a[i][1] = "1";
cout << "联系人删除成功" << endl;
}
}
fstream infile2("Mobilecard contact.txt", ios::out);
if (!infile2)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i <= m; i++)
{
for (int j = 0; j < 2; j++)
{
if (a[i][0] == "1" || a[i][1] == "1")
{
}
else
{
infile2 << a[i][j] << endl;
}
}
}
infile2.close();
return 0;
}
int Mobilecardaddressbook::showallcontacter()
{
int m;
string a[1000][2];
fstream infile("Mobilecard contact.txt", ios::in);
if (!infile)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 2; j++)
{
infile >> a[i][j];
}
m = i;
if (a[i][0] == "")
{
break;
}
}
infile.close();
for (int i = 0; i < m; i++)
{
for (int j = 0; j < 2; j++)
{
cout << a[i][j] << endl;
}
}
return 0;
}
int Mobilecardaddressbook::unloading()
{
int m1;
string a[1000][2];
fstream infile2("Mobilecard contact.txt", ios::in);
if (!infile2)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 2; j++)
{
infile2 >> a[i][j];
}
m1 = i;
if (a[i][0] == "")
{
break;
}
}
infile2.close();
int m2;
string b[1000][4];
fstream infile("Mobile contact.txt", ios::in);
if (!infile)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 4; j++)
{
infile >> b[i][j];
}
m2 = i;
if (b[i][0] == "")
{
break;
}
}
for (int j = 0; j < m2; j++)
{
for (int i = 0; i < m1; i++)
{
if (b[j][1] == a[i][1])
{
a[i][0] = "1";
a[i][1] = "1";
}
}
}
int sum;
sum = m1 + m2 + 2;
if (sum <= 1000)
{
int i1 = 0;
int j1 = 0;
cout << "手机卡联系人已转存到手机联系人中" << endl;
cout << "(注:为了节省内存空间,姓名电话相同的项将不会进行转存!)" << endl;
for (int i = m2; i < sum; i++)
{
for (int j = 0; j < 4; j++)
{
if (j == 0 || j == 1)
{
b[i][j] = a[i1][j1];
j1++;
}
else if (j == 2 || j == 3)
{
b[i][j] = "无此信息";
j1++;
}
}
i1++;
j1 = 0;
}
fstream infile2("Mobile contact.txt", ios::out);
if (!infile2)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 4; j++)
{
if (b[i][0] == "1" || b[i][1] == "1")
{
break;
}
else if (b[i][0] != "" || b[i][1] != "")
{
infile2 << b[i][j] << endl;
}
}
}
infile2.close();
}
if (sum > 1000)
{
cout << "无法转存" << endl;
}
return 0;
}
/*
4.手机通讯簿类(这是一个数据库类,继承于通讯簿抽象类):用于记录手机中存储的所有联系人的信息
数据成员包括:
手机联系人的数量
手机联系人对象数组
成员函数包括
构造函数:读取文本文件中的数据,并根据文件内容创建联系人对象数组
析构函数:将对象数组中的内容写入到文本文件中。
增加函数:增加一个联系人
删除操作:删除一个联系人
Display:显示所有联系人的信息
修改某一联系人的信息:
查询并显示某一联系人的信息:
*/
class Mobileaddressbook :public addressbook
{
public:
Mobileaddressbook()
{
cout << " ********************************" << endl;
cout << " * 欢迎来到手机通讯簿 *" << endl;
cout << " ********************************" << endl;
}
int addcontacter();
int inquire();
int deletecontacter();
int modification();
int showallcontacter();
int unloading();
~Mobileaddressbook()
{
cout << " ********************************" << endl;
cout << " * 成功离开手机通讯簿 *" << endl;
}
};
int Mobileaddressbook::addcontacter()
{
int m;
string a[1000][4];
fstream infile("Mobile contact.txt", ios::in);
if (!infile)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i <= 1000; i++)
{
for (int j = 0; j < 4; j++)
{
infile >> a[i][j];
}
m = i;
if (a[i][0] == "")
{
break;
}
}
cout << "现在手机中已经有:" << m << "个联系人" << endl;
if (m < 1000)
{
// cout << "请输入你想要添加到手机的联系人的姓名:" << endl;
// cout << "请输入你想要添加到手机的联系人的电话号码:" << endl;
Mobilecontact A;
cin >> A;
cout << "您刚刚输入的信息为:" << endl;
cout << A;
fstream infile("Mobile contact.txt", ios::app | ios::out);
if (!infile)
{
cout << "打开手机内存文件失败!" << endl;
}
infile << "姓名:" << A.name << endl;
infile << "电话号码:" << A.number << endl;
infile << "籍贯:" << A.nativeplace << endl;
infile << "QQ号码:" << A.qqnumber << endl;
infile.close();
return 0;
}
else if (m >= 1000)
{
cout << "无法继续给手机添加新的联系人" << endl;
}
}
int Mobileaddressbook::inquire()
{
string a[1000][4];
cout << "请输入你想要查询联系人的姓名:" << endl;
string b; cin >> b;
string d; d = "姓名:" + b;
fstream infile("Mobile contact.txt", ios::in);
if (!infile)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i <= 1000; i++)
{
for (int j = 0; j < 4; j++)
{
infile >> a[i][j];
}
if (d == a[i][0])
{
cout << "您查找的联系人信息为:" << endl;
cout << d << endl;
cout << a[i][1] << endl;
cout << a[i][2] << endl;
cout << a[i][3] << endl;
}
}
infile.close();
return 0;
}
int Mobileaddressbook::deletecontacter()
{
string a[1000][4];
int m;
cout << "请输入你想要删的联系人的电话号码:" << endl;
string b; cin >> b;
string d; d = "电话号码:" + b;
fstream infile("Mobile contact.txt", ios::in);
if (!infile)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i <= 1000; i++)
{
for (int j = 0; j < 4; j++)
{
infile >> a[i][j];
}
m = i;
if (a[i][0] == "")
{
break;
}
}
infile.close();
for (int i = 0; i < m; i++)
{
if (d == a[i][1])
{
a[i][0] = "1";
a[i][1] = "1";
a[i][2] = "1";
a[i][3] = "1";
cout << "联系人删除成功" << endl;
}
}
fstream infile2("Mobile contact.txt", ios::out);
if (!infile2)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < 4; j++)
{
if (a[i][0] == "1")
{
break;
}
else
{
infile2 << a[i][j] << endl;
}
}
}
infile2.close();
return 0;
}
int Mobileaddressbook::modification()
{
string a[1000][4];
int m;
cout << "请输入你想要修改的联系人的电话号码:" << endl;
string b; cin >> b;
string d; d = "电话号码:" + b;
fstream infile("Mobile contact.txt", ios::in);
if (!infile)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i <= 1000; i++)
{
for (int j = 0; j < 4; j++)
{
infile >> a[i][j];
}
m = i;
if (a[i][0] == "")
{
break;
}
}
infile.close();
for (int i = 0; i < m; i++)
{
if (d == a[i][1])
{
cout << "请输入您要修改成的姓名:" << endl;
string s, b, c, d;
cin >> s;
a[i][0] = "姓名:" + s;
cout << "请输入您要修改成的电话号码:" << endl;
cin >> b;
a[i][1] = "电话号码:" + b;
cout << "请输入您要修改成的籍贯:" << endl;
cin >> c;
a[i][2] = "籍贯:" + c;
cout << "请输入您要修改成的qq号码:" << endl;
cin >> d;
a[i][3] = "QQ号码:" + d;
cout << "修改成功!" << endl;
}
}
fstream infile2("Mobile contact.txt", ios::out);
if (!infile2)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < 4; j++)
{
if (a[i][0] == "1")
{
break;
}
else
{
infile2 << a[i][j] << endl;
}
}
}
infile2.close();
return 0;
}
int Mobileaddressbook::showallcontacter()
{
int m;
string a[1000][4];
fstream infile("Mobile contact.txt", ios::in);
if (!infile)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 4; j++)
{
infile >> a[i][j];
}
}
infile.close();
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 4; j++)
{
if (a[i][j] != "")
{
cout << a[i][j] << endl;
}
}
}
return 0;
}
int Mobileaddressbook::unloading()
{
int m1;
string a[1000][2];
fstream infile("Mobilecard contact.txt", ios::in);
if (!infile)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 2; j++)
{
infile >> a[i][j];
}
m1 = i;
if (a[i][0] == "")
{
break;
}
}
infile.close();
int m2;
string b[1000][4];
fstream infile2("Mobile contact.txt", ios::in);
if (!infile2)
{
cout << "打开手机内存文件失败!" << endl;
}
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 4; j++)
{
infile2 >> b[i][j];
}
m2 = i;
if (b[i][0] == "")
{
break;
}
}
infile2.close();
for (int i = 0; i < m1; i++)
{
for (int j = 0; j < m2; j++)
{
if (a[i][1] == b[j][1])
{
b[j][0] = "1";
b[j][1] = "1";
}
}
}
int sum;
sum = m1 + m2 + 2;
if (sum <= 1000)
{
int i1 = 0;
int j1 = 0;
cout << "手机联系人已转存到手机卡联系人中" << endl;
cout << "(注:为了节省内存空间,姓名电话相同的项将不会进行转存!)" << endl;
for (int i = m1; i < sum; i++)
{
for (int j = 0; j < 2; j++)
{
a[i][j] = b[i1][j1];
j1++;
}
i1++;
j1 = 0;
}
fstream infile2("Mobilecard contact.txt", ios::out);
if (!infile2)
{
cout << "打开手机卡内存文件失败!" << endl;
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 2; j++)
{
if (a[i][0] == "1" || a[i][1] == "1")
{
break;
}
else if (a[i][0] != "1" || a[i][1] != "1")
{
infile2 << a[i][j] << endl;
}
}
}
infile2.close();
}
if (sum > 1000)
{
cout << "无法转存" << endl;
}
return 0;
}
/*
6.用户类(这是一个操作类,完成通讯簿的操作):用户拥有两个通讯簿(一个是手机中存储的联系人,一个是手机卡中存储的联系人),并且可以对通讯录进行管理
数据成员包括:
两个通讯簿对象
成员函数包括(成员函数体现用户的行为):
添加联系人:利用基类指针,调用相应的通讯簿对象(手机通讯簿或手机卡通信簿)的增加函数完成联系人的添加。实现动态联编,体现出多态特点。(下同)
删除联系人:调用相应的通讯簿对象的删除操作删除一个联系人
Display:显示相应的通讯簿中联系人的信息
修改某一联系人的信息:调用通讯簿对象的函数完成操作
查询并显示某一联系人的信息:调用通讯簿对象的函数完成操作
将手机卡中的存储的联系人的信息移动到手机中
将手机中存储的联系人的信息移动到手机卡中
将手机卡中的存储的联系人的信息复制到手机中
将手机中存储的联系人的信息复制到手机卡中
*/
class user
{
public:
int n;
user(int a)
{
/*
cout << " ********************************" << endl;
cout << " * 欢迎来到通讯录管理系统操作区 *" << endl;
cout << " ********************************" << endl;
*/
n = a;
if (n == 1)
{
addcontacteru();
}
else if (n == 2)
{
inquair();
}
else if (n == 3)
{
deletecontact();
}
else if (n == 4)
{
modification();
}
else if (n == 5)
{
showallcontacter();
}
else if (n == 6)
{
unloading();
}
}
~user()
{
/* cout << "----------------------------------" << endl;
cout << "***离开通讯录管理系统操作区成功***" << endl;
cout << "----------------------------------" << endl;
*/
}
int addcontacteru();
int inquair();
int deletecontact();
int modification();
int showallcontacter();
int unloading();
};
int user::addcontacteru()
{
while (n <= 10)
{
cout << " ********************************" << endl;
cout << " * 增加联系人 *" << endl;
cout << " ********************************" << endl;
cout << " * 在手机中增加新的联系人:1 *" << endl;
cout << " * 在手机卡中增加新的联系人:2 *" << endl;
cout << " * 返回上一菜单:0 *" << endl;
cout << " ********************************" << endl;
int n; cin >> n;
if (n == 1)
{
addressbook* a;
Mobileaddressbook b;
a = &b;
a->addcontacter();
}
else if (n == 2)
{
addressbook* a;
Mobilecardaddressbook c;
a = &c;
a->addcontacter();
}
else if (n == 0)
{
system("cls");//清屏
break;
}
}
return 0;
}
int user::modification()
{
while (n <= 10)
{
cout << " ********************************" << endl;
cout << " * 修改联系人 *" << endl;
cout << " ********************************" << endl;
cout << " * 修改手机中的联系人:1 *" << endl;
cout << " * 修改手机卡中的联系人: 2 *" << endl;
cout << " * 退出修改界面:0 *" << endl;
cout << " ********************************" << endl;
int n;
cin >> n;
if (n == 1)
{
addressbook* a;
Mobileaddressbook b;
a = &b;
a->modification();
}
else if (n == 2)
{
addressbook* a;
Mobilecardaddressbook c;
a = &c;
a->modification();
}
else if (n == 0)
{
system("cls");//清屏
break;
}
}
return 0;
}
int user::inquair()
{
while (n <= 10)
{
cout << " ********************************" << endl;
cout << " * 查询联系人 *" << endl;
cout << " ********************************" << endl;
cout << " * 查询手机中的联系人:1 *" << endl;
cout << " * 查询手机卡中的联系人: 2 *" << endl;
cout << " * 退出查询界面:0 *" << endl;
cout << " ********************************" << endl;
int n;
cin >> n;
if (n == 1)
{
addressbook* a;
Mobileaddressbook b;
a = &b;
a->inquire();
}
else if (n == 2)
{
addressbook* a;
Mobilecardaddressbook c;
a = &c;
a->inquire();
}
else if (n == 0)
{
system("cls");//清屏
break;
}
}
return 0;
}
int user::deletecontact()//用姓名作为删除依据可能会出现重名的现象,所以这里采用以电话为删除依据
{
while (n <= 10)
{
cout << " ********************************" << endl;
cout << " * 删除联系人 *" << endl;
cout << " ********************************" << endl;
cout << " * 删除手机中的联系人:1 *" << endl;
cout << " * 删除手机卡中的联系人: 2 *" << endl;
cout << " * 一键删除所有的联系人: 3 *" << endl;
cout << " * 退出删除界面:0 *" << endl;
cout << " ********************************" << endl;
int n;
cin >> n;
if (n == 1)
{
addressbook* a;
Mobileaddressbook b;
a = &b;
a->deletecontacter();
}
else if (n == 2)
{
addressbook* a;
Mobilecardaddressbook c;
a = &c;
a->deletecontacter();
}
else if (n == 3)
{
fstream infile("Mobile contact.txt", ios::out);
if (!infile)
{
cout << "打开手机内存文件失败!" << endl;
}
else {
infile << "";
}
fstream infile2("Mobilecard contact.txt", ios::out);
if (!infile)
{
cout << "打开手机内存文件失败!" << endl;
}
else {
infile << "";
}
cout << "全部删除成功!" << endl;
infile.close();
infile2.close();
}
else if (n == 0)
{
system("cls");//清屏
break;
}
}
return 0;
}
int user::showallcontacter()
{
while (n <= 10)
{
cout << " ********************************" << endl;
cout << " * 显示联系人 *" << endl;
cout << " ********************************" << endl;
cout << " * 显示手机中的联系人:1 *" << endl;
cout << " * 显示手机卡中的联系人: 2 *" << endl;
cout << " * 退出显示界面:0 *" << endl;
cout << " ********************************" << endl;
int n; cin >> n;
if (n == 1)
{
addressbook* a;
Mobileaddressbook b;
a = &b;
a->showallcontacter();
}
if (n == 2)
{
addressbook* a;
Mobilecardaddressbook c;
a = &c;
a->showallcontacter();
}
if (n == 0)
{
system("cls");//清屏
break;
}
}
return 0;
}
int user::unloading()
{
while (n <= 10)
{
cout << " ********************************" << endl;
cout << " * 转存联系人 *" << endl;
cout << " ********************************" << endl;
cout << " * 转存手机中的联系人:1 *" << endl;
cout << " * 转存手机卡中的联系人: 2 *" << endl;
cout << " * 退出转存界面:0 *" << endl;
cout << " ********************************" << endl;
int n; cin >> n;
if (n == 1)
{
addressbook* a;
Mobileaddressbook b;
a = &b;
a->unloading();
}
else if (n == 2)
{
addressbook* a;
Mobilecardaddressbook c;
a = &c;
a->unloading();
}
else if (n == 0)
{
system("cls");//清屏
break;
}
}
return 0;
}
/*
7.界面菜单类:用来给出操作提示
数据成员:可以不定义数据成员
成员函数:
Display函数:显示操作菜单的提示。
说明:可以根据需要定义多个函数,显示不同的菜单(操作提示)。
*/
class menu
{
public:
menu() {
cout << " ********************************" << endl;
cout << " * 欢迎使用小岳通讯录管理系统 *" << endl;
cout << " ********************************" << endl;
}
~menu() {
/*
cout << " ********************************" << endl;
cout << " * 成功离开通讯录管理系统菜单 *" << endl;
cout << " ********************************" << endl;
*/
}
int display();
};
int menu::display()
{
int n;
cout << " * 请输入数字选择相应功能 *" << endl;
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;
cin >> n;
return n;
}
int main()
{
int a = 1;
while (a < 10)
{
menu A;
a = A.display();
if (a == 0)break;
user B(a);
}
}
大概这样子,仅供参考:
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define MAX_NAME 256
#define MAX_PHONE_NUMBER 32
typedef struct
{
// 联系人姓名
char name[256];
// 联系人电话
char phone_number[32];
}contact_t;
// 创建联系人
contact_t contact_construct()
{
contact_t ret;
memset(ret.name, 0, sizeof(ret.name));
memset(ret.phone_number, 0, sizeof(ret.phone_number));
}
// 3. 显示联系人
void contact_display(const contact_t* contact)
{
printf("联系人姓名: %s\n联系人电话: %s\n",
contact->name, contact->phone_number);
}
// 5. 更改联系人信息
void contact_update(contact_t* contact, char new_name[MAX_NAME], char new_phone_number[MAX_PHONE_NUMBER])
{
strcpy(contact->name, new_name);
strcpy(contact->phone_number, new_phone_number);
}
int contact_compare(const contact_t* lcontact, const contact_t* rcontact)
{
return strcmp(lcontact->phone_number, rcontact->phone_number);
}
typedef struct
{
contact_t* contacts;
int num_contacts;
}contact_list_t;
contact_list_t contact_list_construct()
{
contact_list_t list;
list.contacts = NULL;
list.num_contacts = 0;
return list;
}
void contact_list_destroy(contact_list_t* list)
{
free(list->contacts);
list->num_contacts = 0;
}
void contact_list_add(contact_list_t* contact_list, const contact_t* contact)
{
contact_t* newcontacts = (contact_t*)realloc(contact_list->contacts, ((size_t)contact_list->num_contacts + 1) * sizeof(contact_t));
if (!newcontacts)
{
fprintf(stderr, "fail to add contact\n");
exit(EXIT_FAILURE);
}
contact_list->contacts = newcontacts;
contact_list->contacts[contact_list->num_contacts] = *contact;
contact_list->num_contacts++;
}
void contact_list_delete(contact_list_t* contact_list, const contact_t* contact)
{
for (int i = 0; i < contact_list->num_contacts; ++i)
{
if (contact_compare(&contact_list->contacts[i], contact) == 0)
{
for (int j = i; j < contact_list->num_contacts - 1; ++j)
{
contact_list->contacts[j] = contact_list->contacts[j + 1];
}
contact_t* newcontacts = (contact_t*)realloc(contact_list->contacts, ((size_t)contact_list->num_contacts - 1) * sizeof(contact_t));
contact_list->num_contacts -= 1;
return;
}
}
printf("fail to find contact\n");
}
typedef struct
{
// 所有联系人
contact_list_t contacts;
// 收藏联系人
contact_list_t contacts_collection;
}address_book_t;
// 创建通讯录
address_book_t address_book_construct()
{
address_book_t address_book;
address_book.contacts = contact_list_construct();
address_book.contacts_collection = contact_list_construct();
}
// 1. 添加联系人
void address_book_add_contact(address_book_t* address_book, const contact_t* contact)
{
contact_list_add(&address_book->contacts, contact);
}
// 2. 删除联系人
void address_book_delete_contact(address_book_t* address_book, const contact_t* contact)
{
contact_list_delete(&address_book->contacts, contact);
}
// 4. 查找联系人
void address_book_find_contact(address_book_t* address_book, char initial, contact_list_t* result)
{
for (int i = 0; i < address_book->contacts.num_contacts; ++i)
{
contact_t* contact = &address_book->contacts.contacts[i];
if (strlen(contact->name) > 0 && contact->name[0] == initial)
{
contact_list_add(result, contact);
}
}
}
// 6. 清空通讯录
void address_book_clean(address_book_t* address_book)
{
contact_list_destroy(&address_book->contacts);
contact_list_destroy(&address_book->contacts_collection);
}
// 7. 收藏联系人
void address_book_collect_contact(address_book_t* address_book, const contact_t* contact)
{
contact_list_add(&address_book->contacts_collection, contact);
}
第1步 设计一个结构体,其中包含若干个字符串数组。每个字符串数组用来记录一条通信录信息,比如这样:
struct {
uint index;
char name[256];
char tel[20];
char addr[1024];
TXL *prev; //前一条通信录结构体的指针
TXL *next; //下一条通信录结构体的指针
……
}TXL;
第2步 声明一个TXL类型的指针:
TXL *MyTxl;
第3步 当用户新建一条通信录的时候,你就按照TXL的字节长度,malloc一个内存空间pTmp,用来缓存用户输入的信息。
第4步 当用户点击保存的时候,如果是新建的就调用realloc,给原来的TXL空间补充一段空间,大小=pTmp,然后将pTmp缓存里的数据倒腾到pMyTxl里。如果是编辑已有的通信录,就把pTmp倒腾到pMyTxl[index]里。
基本就这些了,有啥不明白的,再问吧。