实验内容:
为某单位建立一个基于文件存储的员工通讯录管理系统,可以方便查询每个员工的办公电话、手机号、电子邮箱等信息。基本功能包括从通讯录文件中读取信息构建通讯录表(顺序表、链表均可),员工信息查询、修改、删除,整个通讯录显示输出,保存等。可以再基本功能基础上,自行扩充。
实验步骤:
(1)按照实验要求编写代码,构建系统。
(2)输入验收用例,验证其输出结果。
#include <iostream>
#include<string>
#include<Windows.h>
#include <iterator>
#include <fstream>
#include<cmath>
using namespace std;
typedef struct Datatype {
string Num; // 员工编号
string Name; // 员工姓名
string Tel;// 办公电话
string Phone;// 手机号
}Datatype;
//Datatype notebook[50000];
typedef struct notebook {
Datatype Person[50000];
int length = 0;
}notebook;
notebook myNotebook;
void PrintHello_Change() {
cout << "****************************" << endl;
cout << "1.员工编号" << endl;
cout << "2.员工姓名" << endl;
cout << "3.办公电话" << endl;
cout << "4.手机号" << endl;
cout << "****************************" << endl;
}
void PrintHello() {
cout << "****************************" << endl;
cout << "1.查询"<<endl;
cout << "2.修改" << endl;
cout << "3.删除" << endl;
cout << "4.输出" << endl;
cout << "5.保存" << endl;
cout << "6.增加" << endl;
cout << "7.退出" << endl;
cout << "****************************" << endl;
}
void Add() {
int length = myNotebook.length;
Datatype A ;
string Info;
cout << "请输入新员工的编号" << endl;
cin >> Info;
for (int i = 0; i < length; i++) {
if (Info == myNotebook.Person[i].Num) {
cout << "该编号员工已存在!" << endl;
return;
}
}
for (int i = 0; i < Info.length(); i++) {
if (Info[i] < '0' || Info[i]>'9') {
cout << "编号错误" << endl;
return;
}
}
A.Num = Info;
cout << "请输入新员工的姓名" << endl;
cin >> Info;
A.Name = Info;
cout << "请输入新员工的办公电话" << endl;
cin >> Info;
for (int i = 0; i < Info.length(); i++) {
if (Info[i] < '0' || Info[i]>'9') {
cout << "电话格式错误" << endl;
return;
}
}
A.Tel = Info;
cout << "请输入新员工的手机号" << endl;
cin >> Info;
for (int i = 0; i < Info.length(); i++) {
if (Info[i] < '0' || Info[i]>'9') {
cout << "手机号格式错误" << endl;
return;
}
}
A.Phone = Info;
myNotebook.Person[length++]=A;
myNotebook.length = length;
cout << "添加成功" << endl;
}
void PrintInfo(Datatype A) {
cout << "****************************" << endl;
cout << "员工编号" << endl;
cout << A.Num << endl;
cout << "员工姓名" << endl;
cout << A.Name << endl;
cout << "办公电话" << endl;
cout << A.Tel << endl;
cout << "手机号" << endl;
cout << A.Phone << endl;
cout << "****************************" << endl;
}
void Save() {
ofstream out("info.txt", ios::out | ios::trunc);
if (!out.is_open())
{
cout << "文件创建失败!" << endl;
exit(0);
}
for (int i = 0; i < myNotebook.length; i++) {
Datatype A = myNotebook.Person[i];
out << "****************************" << endl;
out << "员工编号" << endl;
out << A.Num << endl;
out << "员工姓名" << endl;
out << A.Name << endl;
out << "办公电话" << endl;
out << A.Tel << endl;
out << "手机号" << endl;
out << A.Phone << endl;
out << "****************************" << endl;
}
out.close();
cout << "保存成功" << endl;
}
void PrintAll() {
if (myNotebook.length == 0) {
cout << "通讯录表为空" << endl;
return;
}
for (int i = 0; i < myNotebook.length; i++) {
PrintInfo(myNotebook.Person[i]);
}
}
void Delete() {
if (myNotebook.length == 0) {
cout << "通讯录表为空" << endl;
return;
}
cout << "输入编号" << endl;
string num;
cin >> num;
for (int i = 0; i < num.length(); i++) {
if (num[i] < '0' || num[i]>'9') {
cout << "编号有误" << endl;
return;
}
}
for (int i = 0; i < myNotebook.length; i++) {
if (myNotebook.Person[i].Num == num) {
myNotebook.length--;
for (int j = i ; j < myNotebook.length; j++) {
myNotebook.Person[j] = myNotebook.Person[j + 1];
}
cout << "删除完成" << endl;
return;
}
}
cout << "查无此人" << endl;
}
void Change() {
if (myNotebook.length == 0) {
cout << "通讯录表为空" << endl;
return;
}
cout << "输入编号" << endl;
string num;
cin >> num;
for (int i = 0; i < num.length(); i++) {
if (num[i] < '0' || num[i]>'9') {
cout << "编号有误" << endl;
return;
}
}
for (int i = 0; i < myNotebook.length; i++) {
if (myNotebook.Person[i].Num == num) {
cout << "请输入需要修改的信息,目前信息为:" << endl;
PrintInfo(myNotebook.Person[i]);
PrintHello_Change();
int value;
cin >> value;
string Info;
switch (value)
{
case 1: {
cout << "输入新的信息" << endl;
cin >> Info;
myNotebook.Person[i].Num= Info;
break;
}
case 2: {
cout << "输入新的信息" << endl;
cin >> Info;
myNotebook.Person[i].Name = Info;
break;
}
case 3: {
cout << "输入新的信息" << endl;
cin >> Info;
myNotebook.Person[i].Tel = Info;
break;
}
case 4: {
cout << "输入新的信息" << endl;
cin >> Info;
myNotebook.Person[i].Phone = Info;
break;
}
default: {
cout << "错误的输入" << endl;
break;
}
}
return;
}
}
cout << "此人不存在" << endl;
}
void Find() {
if (myNotebook.length == 0) {
cout << "通讯录表为空" << endl;
return;
}
cout << "输入编号" << endl;
string num;
cin >> num;
for (int i = 0; i < num.length(); i++) {
if (num[i] < '0' || num[i]>'9') {
cout << "编号有误" << endl;
return;
}
}
for (int i = 0; i < myNotebook.length; i++) {
if (myNotebook.Person[i].Num == num) {
PrintInfo(myNotebook.Person[i]);
return;
}
}
cout << "查无此人" << endl;
}
/*
typedef struct Datatype {
string Num; // 员工编号
string Name; // 员工姓名
string Tel;// 办公电话
string Phone;// 手机号
}Datatype;
//Datatype notebook[50000];
typedef struct notebook {
Datatype Person[50000];
int length = 0;
}notebook;
*/
void Read() {
ifstream in("info.txt", ios::in); /*以读的方式打开txt文件*/
istream_iterator<string> in_iter(in), eof; //创建流迭代器从文件中读取字符串,eof表示尾后迭代器
int length = myNotebook.length;
Datatype A;
string Datain="";
while (in_iter != eof) {
Datain = *in_iter;
for (int i = 1; i <= 10&& in_iter != eof; i++, in_iter++) {
Datain = *in_iter;
//cout << i << " " << Datain << endl;
if (i == 3) {
//int nn = 0;
//for (int j = 0; j < Datain.length(); j++) {
// nn += (Datain[j] - '0') * pow(10, Datain.length() - j);
//}
//A.Num = nn;
A.Num = Datain;
}
if (i == 5) {
A.Name = Datain;
}
if (i == 7) {
A.Phone = Datain;
}
if (i == 9) {
A.Tel = Datain;
}
}
myNotebook.Person[myNotebook.length++] = A;
}
in.close();
}
/*
* for (int i = 0; i < myNotebook.length; i++) {
Datatype A = myNotebook.Person[i];
out << "****************************" << endl;
out << "员工编号" << endl;
out << A.Num << endl;
out << "员工姓名" << endl;
out << A.Name << endl;
out << "办公电话" << endl;
out << A.Tel << endl;
out << "手机号" << endl;
out << A.Phone << endl;
out << "****************************" << endl;
}
*/
int main()
{
int value;
PrintHello();
Read();
while (true) {
cin >> value;
switch (value)
{
case 1: {
Find();
system("pause");
system("cls");
PrintHello();
break;
}
case 2: {
Change();
system("pause");
system("cls");
PrintHello();
break;
}
case 3: {
Delete();
system("pause");
system("cls");
PrintHello();
break;
}
case 4: {
PrintAll();
system("pause");
system("cls");
PrintHello();
break;
}
case 5: {
Save();
system("pause");
system("cls");
PrintHello();
break;
}
case 6: {
Add();
system("pause");
system("cls");
PrintHello();
break;
}
case 7: {
exit(0);
}
default: {
cout << "输入有误" << endl;
system("pause");
system("cls");
PrintHello();
break;
}
}
}
}