1 问题分析
作为一款通讯录设计,最主要的就是联系人,我的这款班级通讯录中成员主要包括两类人:学生和老师。另外实现的功能需要满足增删查改并且存入文本文件中。
2 方案设计
我的联系人中的学生类和老师类继承抽象的Basic基类对象,继承其中的姓名,电话,地址,性别等两者共有的属性,在老师类中特殊成员是工号和教职工的职位(班主任,数学老师,语文老师等), 在学生类中特殊成员是学号和学生在班担任的职位(班长,体委,团支书等)(其实学生类和老师类可以合在一起写,但是我为了在后续分开存储在文件中更加方便清晰,就选择了分开写)。再由学生类和老师类作为成员构成联系人类,在联系人类中实现增删查改的操作。
我的c++类图如下:
3 关键算法
我的学生类和老师类用的是c++中的STL里面的vector容器进行存储,所以可以便利的使用其中自带的函数,比如find()函数,来进行查找联系人,在删除联系人的时候,只需要使用vector容器自带的erase()来进行删除,添加联系人的操作使用push_back()进行等等。
4.实现操作的一部分截图
1.登录界面
2.用户登录界面
3.用户注册界面
4.通讯录的主界面
5.添加联系人
6.查询联系人
7.修改联系人
8.显示所有联系人
9.删除联系人
10.清空通讯录
总的代码如下:
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<fstream>
const int MAX = 100;//Account最多有MAX人
string NAME;//全局变量 存储登录的的姓名
string ID; //全局变量 存储登录的的学号or工号
//抽象人类
class Person
{
public:
Person()
{
m_sex = -1;
m_age = -1;
}
Person(string name, int age, int sex, string phonenumber, string number, string address)
{
m_name = name;
m_age = age;
m_sex = sex;
m_phonenumber = phonenumber;
m_number = number;
m_address = address;
}
virtual void print() = 0;
~Person()
{
}
string m_name;//姓名
int m_age;//年龄
int m_sex;//性别(1--男性, 2--女性)
string m_phonenumber;//手机电话
string m_number;//座机号
string m_address;//家庭住址
};
//学生类
class Student : public Person
{
public:
string m_status;//身份学生(职位:班长,学委,团支书等)
string student_id;//学号
Student(string name, int age, int sex, string phonenumber, string number, string address, string status, string id) : Person(name, age, sex, phonenumber, number, address)
{
m_status = status;
student_id = id;
}
Student()
{
}
void print()
{
cout << m_name << "\t" << m_age;
if (m_sex == 1)
{
cout << "\t男性";
}
else if (m_sex == 2)
{
cout << "\t女性";
}
cout << "\t" << m_status << "\t" << student_id;
cout << "\t" << m_phonenumber << "\t\t" << m_number << "\t\t\t" << m_address << "\t\t" << endl;;
}
bool operator==(const Student& s)
{
if (this->m_phonenumber == s.m_phonenumber || this->m_number == s.m_number || this->m_name == s.m_name || this->student_id == s.student_id)
{
return true;
}
return false;
}
};
//老师类
class Teacher : public Person
{
public:
string m_job;//教师身份(职位:班主任,英语老师,数学老师等)
string teacher_id;//工号
Teacher(string name, int age, int sex, string phonenumber, string number, string address, string job, string id) : Person(name, age, sex, phonenumber, number, address)
{
m_job = job;
teacher_id = id;
}
Teacher()
{
m_sex = -1;
m_age = -1;
}
void print()
{
cout << m_name << "\t" << m_age;
if (m_sex == 1)
{
cout << "\t男性";
}
else if (m_sex == 2)
{
cout << "\t女性";
}
cout << "\t" << m_job << "\t" << teacher_id;
cout << "\t\t" << m_phonenumber << "\t\t" << m_number << "\t\t\t" << m_address << "\t\t" << endl;
}
bool operator==(const Teacher& t)
{
if (this->m_phonenumber == t.m_phonenumber || this->m_number == t.m_number || this->m_name == t.m_name || this->teacher_id == t.teacher_id)
{
return true;
}
return false;
}
};
//联系人类
class Contact
{
public:
vector<Student> v1;
vector<Teacher> v2;
static int total_student;
static int total_teacher;
//删除联系人
void delete_person()
{
while (1)
{
system("cls");
show_changeperson();
cout << "*请问您想删除哪位联系人(请输入联系人姓名,若输入“stop”则放弃删除):*";
string name;
cin >> name;
if (name == "stop")
{
cout << "*放弃删除,即将返回*" << endl;
system("pause");
return;
}
int flag = 0;
for (int i = 0; i < v1.size(); i++)
{
if (v1[i].m_name == name)
{
v1.erase(v1.begin() + i);
--total_student;
flag = 1;
break;
}
}
for (int i = 0; i < v2.size(); i++)
{
if (v2[i].m_name == name)
{
v2.erase(v2.begin() + i);
--total_teacher;
flag = 2;
break;
}
}
if (flag == 0)
{
cout << "*抱歉,没有查找到此联系人,请重新输入*" << endl;
continue;
}
else
{
showperson();
cout << endl << "*-------删除成功*--------" << endl;
cout << endl << "请问是否需要继续删除(y / Y or n / N): * ";
char h;
cin >> h;
while (h != 'y' && h != 'Y' && h != 'n' && h != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> h;
}
if (h == 'y' || h == 'Y')
{
continue;
}
else if (h == 'n' || h == 'N')
{
break;
}
}
}
}
//添加联系人
void addperson()
{
while (1)
{
system("cls");
cout << "*请输入联系人信息: *" << endl;
cout << "*请问你想添加的是老师还是学生信息(1--学生, 2--老师)*" << endl;
int order;
cin >> order;
if (order != 1 && order != 2)
{
cout << "*输入错误,请重新输入!*" << endl;
cin >> order;
continue;
}
if (order == 1)
{
Student s;
cout << "*当前您所输入的是学生信息* " << endl;
cout << "*请输入姓名:* ";
cin >> s.m_name;
cout << "*请输入年龄:* ";
cin >> s.m_age;
cout << "*请输入性别(1--男性, 2--女性):* ";
cin >> s.m_sex;
while (s.m_sex != 1 && s.m_sex != 2)
{
cout << "*抱歉, 输入错误,请重新输入* " << endl;
cin >> s.m_sex;
}
cout << "*请输入职位:* ";
cin >> s.m_status;
cout << "*请输入学号:* ";
cin >> s.student_id;
cout << "*请输入手机号(没有则输入无):* ";
cin >> s.m_phonenumber;
cout << "*请输入座机号(没有则输入无):* ";
cin >> s.m_number;
cout << "*请输入家庭住址:* ";
cin >> s.m_address;
total_student++;
cout << "*所有信息录入完毕* " << endl;
cout << "*请问是否要继续录入(y/Y or n/N)* " << endl;
v1.push_back(s);
char tmp;
cin >> tmp;
while (tmp != 'y' && tmp != 'Y' && tmp != 'n' && tmp != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> tmp;
}
if (tmp == 'y' || tmp == 'Y')
{
system("cls");
continue;
}
else if (tmp == 'n' || tmp == 'N')
{
system("cls");
break;
}
}
else if (order == 2)
{
cout << "*当前您所输入的是老师信息* " << endl;
Teacher t;
cout << "*请输入姓名:* ";
cin >> t.m_name;
cout << "*请输入年龄:* ";
cin >> t.m_age;
cout << "*请输入性别(1--男性, 2--女性):* ";
cin >> t.m_sex;
while (t.m_sex != 1 && t.m_sex != 2)
{
cout << "*抱歉, 输入错误,请重新输入* " << endl;
cin >> t.m_sex;
}
cout << "*请输入职位:* ";
cin >> t.m_job;
cout << "*请输入工号:* ";
cin >> t.teacher_id;
cout << "*请输入手机号(没有则输入无):* ";
cin >> t.m_phonenumber;
cout << "*请输入座机号(没有则输入无):* ";
cin >> t.m_number;
cout << "*请输入家庭住址:* ";
cin >> t.m_address;
v2.push_back(t);
total_teacher++;
cout << "*所有信息录入完毕* " << endl;
cout << "*请问是否要继续录入(y/Y or n/N)* " << endl;
char tmp;
cin >> tmp;
while (tmp != 'y' && tmp != 'Y' && tmp != 'n' && tmp != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> tmp;
}
if (tmp == 'y' || tmp == 'Y')
{
system("cls");
continue;
}
else if (tmp == 'n' || tmp == 'N')
{
system("cls");
break;
}
}
}
}
//展示联系人
void showperson()
{
system("cls");
cout << "--------------------当前用户:" << NAME << " " << ID << "--------------------------" << endl;
cout << "*当前通讯录中共有:* --" << total_student + total_teacher << "人--" << endl;
cout << " ||*老师的信息*||" << endl;
cout << "*当前老师人数:*" << "---" << total_teacher << "人---" << endl;
if (v2.size() == 0)
{
cout << "*暂未添加老师信息" << endl;
}
else
{
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
for (int i = 0; i < v2.size(); i++)
{
v2[i].print();
}
}
cout << " ||*学生的信息*||" << endl;
cout << "*当前学生人数:*" << "---" << total_student << "人---" << endl;
if (v1.size() == 0)
{
cout << "*暂未添加学生信息" << endl;
}
else
{
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
for (int i = 0; i < v1.size(); i++)
{
v1[i].print();
}
}
}
//修改联系人
void change()
{
int number = 0;
int flag = 0;
while (1)
{
system("cls");
show_changeperson();
flag = 0;
cout << "*请问您想修改哪位联系人的信息(请输入姓名,若输入“stop”则放弃修改):*";
string name;
cin >> name;
if (name == "stop")
{
cout << "*放弃修改,即将返回*" << endl;
system("pause");
return;
}
for (int i = 0; i < v1.size(); i++)
{
if (v1[i].m_name == name)
{
system("cls");
number = i;
flag = 1;
break;
}
}
for (int i = 0; i < v2.size(); i++)
{
if (v2[i].m_name == name)
{
system("cls");
number = i;
flag = 2;
break;
}
}
if (flag == 0)
{
cout << "*抱歉,没有查找到此人,请重新输入*" << endl;
}
else
{
break;
}
}
while (1)
{
system("cls");
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|工号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
if (flag == 1)
{
v1[number].print();
}
else if (flag == 2)
{
v2[number].print();
}
showproject();
cout << "*请输入你想更改的联系人的什么信息*" << endl;
int tmp;
cin >> tmp;
if (tmp == 1)//姓名
{
cout << "*请输入修改后的姓名:* ";
string n;
cin >> n;
if (flag == 1)
{
v1[number].m_name = n;
}
else if (flag == 2)
{
v2[number].m_name = n;
}
}
else if (tmp == 2)//性别
{
cout << "*请输入修改后的性别(1--男性, 2--女性):* ";
int n;
while (1)
{
cin >> n;
if (n != 1 && n != 2)
{
cout << "*抱歉输入有误,请重新输入*" << endl;
continue;
}
else
{
if (flag == 1)
{
v1[number].m_sex = n;
}
else if (flag == 2)
{
v2[number].m_sex = n;
}
break;
}
}
}
else if (tmp == 3)//学号or工号
{
cout << "*请输入修改后的学号or工号:* ";
string n;
cin >> n;
if (flag == 1)
{
v1[number].student_id = n;
}
else if (flag == 2)
{
v2[number].teacher_id = n;
}
}
else if (tmp == 4)//家庭住址
{
cout << "*请输入修改后的家庭住址:* ";
string n;
cin >> n;
if (flag == 1)
{
v1[number].m_address = n;
}
else if (flag == 2)
{
v2[number].m_address = n;
}
}
else if (tmp == 5)//手机号
{
cout << "*请输入修改后的手机号:* ";
string n;
cin >> n;
if (flag == 1)
{
v1[number].m_phonenumber = n;
}
else if (flag == 2)
{
v2[number].m_phonenumber = n;
}
}
else if (tmp == 6)//座机号
{
cout << "*请输入修改后的座机号:* ";
string n;
cin >> n;
if (flag == 1)
{
v1[number].m_number = n;
}
else if (flag == 2)
{
v2[number].m_number = n;
}
}
else if (tmp == 7)//职务
{
cout << "*请输入修改后的职务:* ";
string n;
cin >> n;
if (flag == 1)
{
v1[number].m_status = n;
}
else if (flag == 2)
{
v2[number].m_job = n;
}
}
else if (tmp == 8)//年龄
{
cout << "*请输入修改后的年龄:* ";
int n;
cin >> n;
if (flag == 1)
{
v1[number].m_age = n;
}
else if (flag == 2)
{
v2[number].m_age = n;
}
}
else if (tmp == 9)
{
cout << "*放弃修改,即将返回*" << endl;
system("pause");
return;
}
else
{
cout << "*抱歉,您所输入的数据无效,请重新输入*" << endl;
system("pause");
continue;
}
system("cls");
cout << "*修改完毕*" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
if (flag == 1)
{
v1[number].print();
}
else if (flag == 2)
{
v2[number].print();
}
cout << "*是否需要继续修改该联系人其他信息(y/Y or n/N):*";
char h;
cin >> h;
while (h != 'y' && h != 'Y' && h != 'n' && h != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> h;
}
if (h == 'y' || h == 'Y')
{
system("cls");
continue;
}
else if (h == 'n' || h == 'N')
{
system("cls");
break;
}
}
}
//展示可以修改的联系人
void show_changeperson()
{
cout << "*当前通讯录中共有:* --" << total_student + total_teacher << "人--" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
for (int i = 0; i < v2.size(); i++)
{
v2[i].print();
}
for (int i = 0; i < v1.size(); i++)
{
v1[i].print();
}
}
//显示修改的项目
void showproject()
{
cout << " ----------------------------------------------------------------" << endl;
cout << " | 1 . 姓名 |" << endl;
cout << " | |" << endl;
cout << " | 2 . 性别 |" << endl;
cout << " | |" << endl;
cout << " | 3 . 学号or工号 |" << endl;
cout << " | |" << endl;
cout << " | 4 . 家庭住址 |" << endl;
cout << " | |" << endl;
cout << " | 5 . 手机号 |" << endl;
cout << " | |" << endl;
cout << " | 6 . 座机号 |" << endl;
cout << " | |" << endl;
cout << " | 7 . 职务 |" << endl;
cout << " | |" << endl;
cout << " | 8 . 年龄 |" << endl;
cout << " | |" << endl;
cout << " | 9 . 放弃修改 |" << endl;
cout << " ----------------------------------------------------------------" << endl;
}
//清空通讯录
void clear()
{
cout << "*清空通讯录的操作不可逆! ";
cout << "请问您确定要清空通讯录吗?(y/Y or n/N)*" << endl;
char h;
cin >> h;
while (h != 'y' && h != 'Y' && h != 'n' && h != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> h;
}
if (h == 'y' || h == 'Y')
{
v1.clear();
v2.clear();
total_student = 0;
total_teacher = 0;
cout << "*通讯录已经全部清空*" << endl;
}
else if (h == 'n' || h == 'N')
{
cout << "*放弃清空,即将返回*" << endl;
}
}
//搜索联系人
void search()
{
while (1)
{
system("cls");
cout << "*请问你想按姓名查找,按电话查找,还是学号or工号查找(1--电话 2--姓名 3--学号or工号)*" << endl;
int tmp;
cin >> tmp;
while (tmp != 1 && tmp != 2 && tmp != 3)
{
cout << "*抱歉,您所输入的不正确, 请重新输入*" << endl;
cin >> tmp;
}
if (tmp == 1)
{//按照电话查找
{
cout << "*请问你想按手机号码查找还是座机号码查找(1--手机号码 2--座机号码)*" << endl;
int tm;
cin >> tm;
while (tm != 1 && tm != 2)
{
cout << "*抱歉,您所输入的不正确, 请重新输入*" << endl;
cin >> tm;
}
if (tm == 1)//手机号码查找
{
cout << "*请输入你想查找的联系人的手机电话:*";
Student s_tmp;
Teacher t_tmp;
int flag = 0;
int mark = 0;
cin >> s_tmp.m_phonenumber;
t_tmp.m_phonenumber = s_tmp.m_phonenumber;
vector<Student>::iterator s_it = find(v1.begin(), v1.end(), s_tmp);
while (s_it != v1.end())
{
if (mark == 0)
{
cout << "-----查找到联系人的信息为----" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
mark = 1;
}
s_it->print();
flag = 1;
s_it = find(s_it + 1, v1.end(), s_tmp);
}
vector<Teacher>::iterator t_it = find(v2.begin(),v2.end(), t_tmp);
while (t_it != v2.end())
{
if (mark == 0)
{
cout << "-----查找到联系人的信息为----" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
mark = 1;
}
t_it->print();
flag = 1;
t_it = find(t_it + 1, v2.end(), t_tmp);
}
if (flag == 0)
{
cout << "*抱歉,没有通过此手机号码查找到该联系人*" << endl;
cout << "*请问是否需要重新查找(y/Y or n/N):*";
char t;
cin >> t;
while (t != 'y' && t != 'Y' && t != 'n' && t != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> tmp;
}
if (t == 'y' || t == 'Y')
{
continue;
}
else if (t == 'n' || t == 'N')
{
return;
}
}
else if (flag == 1)
{
return;
}
}
else if (tm == 2)// 座机号码查找
{
cout << "*请输入你想查找的联系人的座机电话:*";
Student s_tmp;
Teacher t_tmp;
int flag = 0;
int mark = 0;
cin >> s_tmp.m_number;
t_tmp.m_number = s_tmp.m_number;
vector<Student>::iterator s_it = find(v1.begin(), v1.end(), s_tmp);
while (s_it != v1.end())
{
if (mark == 0)
{
cout << "-----查找到此联系人的信息为----" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
mark = 1;
}
s_it->print();
flag = 1;
s_it = find(s_it + 1, v1.end(), s_tmp);
}
vector<Teacher>::iterator t_it = find(v2.begin(), v2.end(), t_tmp);
while (t_it != v2.end())
{
if (mark == 0)
{
cout << "-----查找到联系人的信息为----" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
mark = 1;
}
t_it->print();
flag = 1;
t_it = find(t_it + 1, v2.end(), t_tmp);
}
if (flag == 0)
{
cout << "*抱歉,没有通过此座机号码查找到该联系人*" << endl;
cout << "*请问是否需要重新查找(y/Y or n/N):*";
char t;
cin >> t;
while (t != 'y' && t != 'Y' && t != 'n' && t != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> t;
}
if (t == 'y' || t == 'Y')
{
continue;
}
else if (t == 'n' || t == 'N')
{
return;
}
}
else if (flag == 1)
{
return;
}
}
}
}
else if (tmp == 2)//按照姓名查找
{
system("cls");
int mark = 0;
cout << "*请输入你想查找的联系人的姓名:*";
Student s_tmp;
Teacher t_tmp;
int flag = 0;
cin >> s_tmp.m_name;
t_tmp.m_name = s_tmp.m_name;
vector<Student>::iterator s_it = find(v1.begin(), v1.end(), s_tmp);
while (s_it != v1.end())
{
if (mark == 0)
{
cout << "-----查找到联系人的信息为----" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
mark = 1;
}
flag = 1;
s_it->print();
s_it = find(s_it + 1, v1.end(), s_tmp);
}
vector<Teacher>::iterator t_it = find(v2.begin(), v2.end(), t_tmp);
while (t_it != v2.end())
{
if (mark == 0)
{
cout << "-----查找到联系人的信息为----" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
mark = 1;
}
t_it->print();
flag = 1;
t_it = find(t_it + 1, v2.end(), t_tmp);
}
if (flag == 0)
{
cout << "*抱歉,没有通过此姓名查找到该联系人*" << endl;
cout << "*请问是否需要重新查找(y/Y or n/N):*";
char t;
cin >> t;
while (t != 'y' && t != 'Y' && t != 'n' && t != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> tmp;
}
if (t == 'y' || t == 'Y')
{
continue;
}
else if (t == 'n' || t == 'N')
{
return;
}
}
else if (flag == 1)
{
return;
}
}
else if (tmp == 3)//按工号学号查找
{
cout << "*请输入你想查找的联系人的学号or工号:*";
Student s_tmp;
Teacher t_tmp;
int flag = 0;
int mark = 0;
cin >> s_tmp.student_id;
t_tmp.teacher_id = s_tmp.student_id;
vector<Student>::iterator s_it = find(v1.begin(), v1.end(), s_tmp);
while (s_it != v1.end())
{
if (mark == 0)
{
cout << "-----查找到联系人的信息为----" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
mark = 1;
}
s_it->print();
flag = 1;
s_it = find(s_it + 1, v1.end(), s_tmp);
}
vector<Teacher>::iterator t_it = find(v2.begin(), v2.end(), t_tmp);
while (t_it != v2.end())
{
if (mark == 0)
{
cout << "-----查找到联系人的信息为----" << endl;
cout << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
mark = 1;
}
t_it->print();
flag = 1;
t_it = find(t_it, v2.end(), t_tmp);
}
if (flag == 0)
{
cout << "*抱歉,没有通过此学号or工号查找到该联系人*" << endl;
cout << "*请问是否需要重新查找(y/Y or n/N):*";
char t;
cin >> t;
while (t != 'y' && t != 'Y' && t != 'n' && t != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> tmp;
}
if (t == 'y' || t == 'Y')
{
continue;
}
else if (t == 'n' || t == 'N')
{
return;
}
}
else if (flag == 1)
{
return;
}
}
}
}
};
int Contact::total_student = 0;
int Contact::total_teacher = 0;
//文件操作
class file
{
public:
//打开文件并存储于vector容器中
void openfile(Contact& m_contact)
{
ifstream ifs;
string fi = NAME + "-" + ID + "-Teacher";
ifs.open(fi, ios::in);
if (!ifs.is_open())
{
return;
}
Teacher tmp;
string buf;
string linenum;
for (int i = 0; i < 3; i++)//前三行数据无意义
{
getline(ifs, buf);
}
int line = 0;
while (getline(ifs, linenum))//判断有效数据有多少行
{
line++;
}
ifs.close();
ifs.open(fi, ios::in);//重新打开文件
for (int i = 0; i < 3; i++)//前三行数据无意义
{
getline(ifs, buf);
}
string t;
for (int i = 0; i < line; i++)//逐个读取真正有意义的数据(老师)
{
ifs >> tmp.m_name;
ifs >> tmp.m_age;
ifs >> t;
if (t == "男性")
{
tmp.m_sex = 1;
}
else if (t == "女性")
{
tmp.m_sex = 2;
}
ifs >> tmp.m_job;
ifs >> tmp.teacher_id;
ifs >> tmp.m_phonenumber;
ifs >> tmp.m_number;
ifs >> tmp.m_address;
m_contact.v2.push_back(tmp);
m_contact.total_teacher++;
}
ifs.close();
fi = NAME + "-" + ID +"-Student";
ifs.open(fi, ios::in);//读取学生的文件
if (!ifs.is_open())
{
return;
}
Student tm;
for (int i = 0; i < 3; i++)//前三行数据无意义
{
getline(ifs, buf);
}
line = 0;
while (getline(ifs, linenum))//判断有效数据有多少行
{
line++;
}
ifs.close();
ifs.open(fi, ios::in);//重新打开文件
for (int i = 0; i < 3; i++)//前三行数据无意义
{
getline(ifs, buf);
}
for (int i = 0; i < line; i++)//逐个读取真正有意义的数据(老师)
{
ifs >> tm.m_name;
ifs >> tm.m_age;
ifs >> t;
if (t == "男性")
{
tm.m_sex = 1;
}
else if (t == "女性")
{
tm.m_sex = 2;
}
ifs >> tm.m_status;
ifs >> tm.student_id;
ifs >> tm.m_phonenumber;
ifs >> tm.m_number;
ifs >> tm.m_address;
m_contact.v1.push_back(tm);
m_contact.total_student++;
}
ifs.close();
}
//保存在文件中
void savefile(Contact& m_contact)
{
//v1--学生 v2--老师
ofstream ofs;
string fi = NAME + "-" + ID + "-Contact";
ofs.open(fi, ios::out);//合并的通讯录
if (!ofs.is_open())
{
cout << "*文件打开失败,程序出现错误!*" << endl;
system("pause");
return;
}
ofs << "--------------------当前用户:" << NAME <<" " << ID << "--------------------------" << endl;
ofs << "*当前通讯录中共有:* --" << m_contact.total_student + m_contact.total_teacher << "人--" << endl;
ofs << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|or|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
for (int i = 0; i < m_contact.v2.size(); i++)
{
ofs << m_contact.v2[i].m_name << "\t" << m_contact.v2[i].m_age;
if (m_contact.v2[i].m_sex == 1)
{
ofs << "\t男性";
}
else if (m_contact.v2[i].m_sex == 2)
{
ofs << "\t女性";
}
ofs << "\t" << m_contact.v2[i].m_job << "\t" << m_contact.v2[i].teacher_id;
ofs << "\t\t" << m_contact.v2[i].m_phonenumber << "\t\t" << m_contact.v2[i].m_number << "\t\t\t" << m_contact.v2[i].m_address << endl;
}
for (int i = 0; i < m_contact.v1.size(); i++)
{
ofs << m_contact.v1[i].m_name << "\t" << m_contact.v1[i].m_age;
if (m_contact.v1[i].m_sex == 1)
{
ofs << "\t男性";
}
else if (m_contact.v1[i].m_sex == 2)
{
ofs << "\t女性";
}
ofs << "\t" << m_contact.v1[i].m_status << "\t" << m_contact.v1[i].student_id;
ofs << "\t\t" << m_contact.v1[i].m_phonenumber << "\t\t" << m_contact.v1[i].m_number << "\t\t\t" << m_contact.v1[i].m_address << endl;
}
ofs.close();
fi = NAME + "-" + ID + "-Teacher";
ofs.open(fi, ios::out);//老师的通讯录
if (!ofs.is_open())
{
cout << "*文件打开失败,程序出现错误!*" << endl;
system("pause");
return;
}
ofs << "--------------------当前用户:" << NAME << " " << ID << "--------------------------" << endl;
ofs << "*当前通讯录中共有老师:* --" << m_contact.total_teacher << "人--" << endl;
ofs << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|工号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
for (int i = 0; i < m_contact.v2.size(); i++)
{
ofs << m_contact.v2[i].m_name << "\t" << m_contact.v2[i].m_age;
if (m_contact.v2[i].m_sex == 1)
{
ofs << "\t男性";
}
else if (m_contact.v2[i].m_sex == 2)
{
ofs << "\t女性";
}
ofs << "\t" << m_contact.v2[i].m_job << "\t" << m_contact.v2[i].teacher_id;
ofs << "\t\t" << m_contact.v2[i].m_phonenumber << "\t\t" << m_contact.v2[i].m_number << "\t\t\t" << m_contact.v2[i].m_address << endl;
}
ofs.close();
fi = NAME + "-" + ID + "-Student";
ofs.open(fi, ios::out);//学生的通讯录
if (!ofs.is_open())
{
cout << "*文件打开失败,程序出现错误!*" << endl;
system("pause");
return;
}
ofs << "--------------------当前用户:" << NAME << " " << ID << "--------------------------" << endl;
ofs << "*当前通讯录中共有学生:* --" << m_contact.total_student << "人--" << endl;
ofs << "|姓名|\t" << "|年龄|\t" << "|性别|\t" << "|职务|\t" << "|学号|\t\t" << "|手机号|\t\t" << "|座机号|\t\t" << "|家庭住址|\t\t" << endl;
for (int i = 0; i < m_contact.v1.size(); i++)
{
ofs << m_contact.v1[i].m_name << "\t" << m_contact.v1[i].m_age;
if (m_contact.v1[i].m_sex == 1)
{
ofs << "\t男性";
}
else if (m_contact.v1[i].m_sex == 2)
{
ofs << "\t女性";
}
ofs << "\t" << m_contact.v1[i].m_status << "\t" << m_contact.v1[i].student_id;
ofs << "\t\t" << m_contact.v1[i].m_phonenumber << "\t\t" << m_contact.v1[i].m_number << "\t\t\t" << m_contact.v1[i].m_address << endl;
}
ofs.close();
}
};
//用户账户
struct Account
{
string name;//姓名
string id;// 学号
string password;// 密码
};
//用户账户操作
class Accountoperate
{
private:
Account a[MAX];
public:
//用户注册or登录界面
void Surface()
{
while (1)
{
system("cls");
cout << " ****************************************************************" << endl;
cout << " | |" << endl;
cout << " | 欢迎使用班级通讯录 |" << endl;
cout << " | |" << endl;
cout << " | 1 . 用户登录 |" << endl;
cout << " | |" << endl;
cout << " | 2 . 用户注册 |" << endl;
cout << " | |" << endl;
cout << " ****************************************************************" << endl;
int n;
cin >> n;
while (n != 1 && n != 2)
{
cout << "*抱歉, 输入错误,请重新输入* " << endl;
cin >> n;
}
if (n == 1)
{
Register();
return;
}
else if (n == 2)
{
Createaccount();
}
}
}
//用户注册
void Createaccount()
{
while (1)
{
system("cls");
cout << "---------------当前为用户注册界面-----------------" << endl;
string name, id, pass;
cout << "*请输入学号or工号:* ";
cin >> id;
cout << "*请输入姓名:* ";
cin >> name;
cout << "*请输入密码:* ";
cin >> pass;
cout << "--------------------------------------------------" << endl;
if (Repeatcheck(id))
{
cout << "*很抱歉,当前学号or工号已经有人使用*" << endl;
cout << "*是否需要重新注册(y/Y or n/N):* ";
char h;
cin >> h;
while (h != 'y' && h != 'Y' && h != 'n' && h != 'N')
{
cout << "*输入错误,请重新输入* " << endl;
cin >> h;
}
if (h == 'y' || h == 'Y')
{
continue;
}
else if (h == 'n' || h == 'N')
{
system("cls");
return;
}
}
else
{
cout << "*注册成功,请返回登录*" << endl;
ofstream ofs;
ofs.open("Account.txt", ios::app);
if (!ofs.is_open())
{
cout << "*文件打开失败,程序即将退出*" << endl;
exit(0);
}
ofs << "\n";
ofs << id;
ofs << ' ';
ofs << name;
ofs << ' ';
ofs << pass;
ofs.close();
system("pause");
return;
}
}
}
//用户注册时查重学号or工号
bool Repeatcheck(string id)
{
string n[MAX], i[MAX], p[MAX];
ifstream ifs;
ifs.open("Account.txt", ios::in);
if (!ifs.is_open())
{
cout << "*文件打开失败,程序即将退出*" << endl;
exit(0);
}
for (int j = 0; j < MAX; j++)
{
ifs >> i[j];
ifs >> n[j];
ifs >> p[j];
}
ifs.close();
for (int j = 0; j < MAX; j++)
{
if (id == i[j])
{
return true;
}
}
return false;
}
//用户登录
void Register()
{
system("cls");
string n[MAX], i[MAX], p[MAX];
ifstream ifs;
ifs.open("Account.txt", ios::in);
if (!ifs.is_open())
{
cout << "*文件打开失败,程序即将退出*" << endl;
exit(0);
}
for (int j = 0; j < MAX; j++)
{
ifs >> i[j];
ifs >> n[j];
ifs >> p[j];
}
ifs.close();
while (1)
{
cout << "---------------当前为用户登录界面-----------------" << endl;
string name, id, pass;
cout << "*请输入学号or工号:* ";
cin >> id;
cout << "*请输入姓名:* ";
cin >> name;
cout << "*请输入密码:* ";
cin >> pass;
int flag = 0;
cout << "--------------------------------------------------" << endl;
for (int h = 0; h < MAX; h++)
{
if (id == i[h] && name == n[h] && pass == p[h])
{
cout << "*用户认证成功*" << endl;
flag = 1;
NAME = name;
ID = id;
system("pause");
return;
}
}
if (flag == 0)
{
cout << "*用户认证失败,请重新输入*" << endl;
system("pause");
system("cls");
}
}
}
};
//addressbook类中集合操作
class addresssbook
{
private:
Contact m_contact;
file f;
Accountoperate account;
public:
//操作菜单
void showmenu()
{
account.Surface();
f.openfile(m_contact);
while (1)
{
system("cls");
cout << " ****************************************************************" << endl;
cout << " | |" << endl;
cout << " | 欢迎使用班级通讯录 |" << endl;
cout << " ";
cout << "当前用户:" << NAME << " " << ID;
cout << " " << endl;
cout << " ****************************************************************" << endl;
cout << " | 1 . 添加联系人 |" << endl;
cout << " | |" << endl;
cout << " | 2 . 查询联系人 |" << endl;
cout << " | |" << endl;
cout << " | 3 . 修改联系人 |" << endl;
cout << " | |" << endl;
cout << " | 4 . 删除联系人 |" << endl;
cout << " | |" << endl;
cout << " | 5 . 显示所有联系人 |" << endl;
cout << " | |" << endl;
cout << " | 6 . 清空通讯录 |" << endl;
cout << " | |" << endl;
cout << " | 0 . 退出程序并保存在文件中 |" << endl;
cout << " ****************************************************************" << endl;
int order;
cout << "请输入你想进行的操作的序号:" << endl;
cin >> order;
if (order == 1)//添加联系人
{
m_contact.addperson();
}
else if (order == 2)//查询联系人
{
m_contact.search();
system("pause");
}
else if (order == 3)//修改联系人
{
m_contact.change();
}
else if (order == 4)//删除联系人
{
m_contact.delete_person();
}
else if (order == 5)//显示所有联系人
{
m_contact.showperson();
system("pause");
}
else if (order == 6)//清空联系人
{
m_contact.clear();
system("pause");
}
else if (order == 0)//退出程序
{
f.savefile(m_contact);
cout << "联系人信息已经保存在文件中,程序安全退出,欢迎下次使用" << endl;
system("pause");
return;
}
else
{
cout << "抱歉,输入错误,请重新输入" << endl;
system("pause");
}
}
}
};
int main()
{
addresssbook book;
book.showmenu();
return 0;
}