📄 phonebook.cpp
字号:
//*******************************************************************
/***************** PhoneBook个人电话簿管理系统 ****************/
/* 作者:郭佩佩 */
/* 学号:09002138 */
/* 日期:2004/11/26 */
//*******************************************************************
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
#include<stdlib.h>
struct record //结构体类型定义
{
char lastName[20]; //姓氏
char firstName[15]; //名字
char phoneNum[15]; //电话
char mobile[15]; //手机
record *next;
};
record *head; //全局变量,链表头指针
record *current; //全局变量,链表当前结点指针
// 函数原型声明
void handleChoice(int choice);
void addRecord();
void insertNode(record *newRecord);
record *posInsertPoint(char lastname[20]);
void makeNodeNewHead(record *newRecord);
void addNodeToEnd(record *newRecord);
void moveCurrentToEnd();
void display();
void update();
void deleteRecord();
void deleteAll();
void deleteHead();
void deleteEnd(record *previous);
void deleteMiddle(record *previous);
int verifyDelete();
void deleteNode(record *previous);
void deleteList();
void searchByLastname();
void wiriteToFile();
void loadFromFile();
void help();
char pause;
// 主函数
int main()
{
cout << "Welcome...\n";
cout << "*************************************************************\n";
cout << "\n\n\n 欢迎使用PhoneBook个人电话簿管理系统... \n\n";
cout << " Press enter to continue!\n\n\n";
cout << "\n*************************************************************";
cin.get(pause);
system("cls"); //执行系统命令:cls-清屏
int choice;
head = NULL;
loadFromFile();
do
{ //主菜单显示
cout << "Menu...\n";
cout << "***********************************************************\n";
cout << " PhoneBook个人电话管理系统主菜单 \n\n";
cout << " 请选择以下功能: \n";
cout << " 1 - 添加一条记录\n";
cout << " 2 - 显示全部记录\n";
cout << " 3 - 按姓氏查找\n";
cout << " 4 - 更新记录\n";
cout << " 5 - 删除一条记录\n";
cout << " 6 - 清空电话簿\n";
cout << " 7 - 帮助\n";
cout << " 8 - 退出系统\n";
cout << "***********************************************************\n";
cout << "选择: ";
cin >> choice;
handleChoice(choice);
}
while(choice != 8);
return 0;
}
// 根据用户选择(choice)调用对应处函数
void handleChoice(int choice)
{
switch(choice)
{
case 1: //添加一条记录
addRecord();
break;
case 2: //显示全部记录
display();
break;
case 3: //按姓氏查找
searchByLastname();
break;
case 4: //更新记录
update();
break;
case 5: //删除一条记录
deleteRecord();
break;
case 6: //清空电话簿
deleteAll();
break;
case 7: //帮助
help();
break;
case 8: //文件存盘,然后退出系统
wiriteToFile();
if(head != NULL)
{
deleteList();
}
break;
default : //其它输入则出错
cout <<"输入有误!!请您重新输入选项!!\n";
break;
}
}
// 在链表中增加一个记录
void addRecord()
{
record *newRecord;
newRecord = new record;
if(newRecord != NULL)
{
system("cls");
cout << "Add a new record...\n";
cout << "*************************************************************\n";
cout << "请您输入...\n";
cin.ignore(20,'\n');
cout << " 姓氏: ";
cin.get(newRecord->lastName,20);
cin.ignore(20,'\n');
cout << " 名字: ";
cin.get(newRecord->firstName,15);
cin.ignore(20,'\n');
cout << " 电话号码: ";
cin.get(newRecord->phoneNum,15);
cin.ignore(20,'\n');
cout << " 手机号码:";
cin.get(newRecord->mobile,15);
cin.ignore(20,'\n');
insertNode(newRecord);
}
else
{
cout << "警告: 存储器错误, 新记录添加失败!!\n";
}
system("cls");
}
// 将一个由newRecord 指向的新节点插入链表中
void insertNode(record *newRecord)
{
system("cls");
record *before;
record *after;
if(head == NULL) //链表为空,直接插入到头结点
{
newRecord->next = NULL;
head = newRecord;
}
else
{
if(strcmp(newRecord->lastName, head->lastName) < 0)
{
makeNodeNewHead(newRecord);
}
else
{
current = posInsertPoint(newRecord->lastName);
before = current;
after = current->next;
if(after == NULL)
{
addNodeToEnd(newRecord);
}
else
{
before->next = newRecord;
newRecord->next = after;
}
}
}
}
// 按姓氏,返回其在链表中的正确位置。新节点即将插入此点。
record *posInsertPoint(char lastname[20])
{
char tempName[20];
record *temp;
int tempint;
if(head->next != NULL)
{
current = head;
temp = current->next;
strcpy(tempName, temp->lastName);
tempint = strcmp(lastname,tempName);
while((tempint > 0) && (current->next !=NULL))
{
current = temp;
if(current->next != NULL)
{
temp = current->next;
strcpy(tempName, temp->lastName);
tempint = strcmp(lastname,tempName);
}
}
}
else
{
current = head;
}
return(current);
}
// 添加到链表头部
void makeNodeNewHead(record *newRecord)
{
record *temp;
temp = head;
newRecord->next = temp;
head = newRecord;
}
// 添加到链表末尾
void addNodeToEnd(record *newRecord)
{
newRecord->next = NULL;
moveCurrentToEnd();
current->next = newRecord;
}
// current指针移动到链表尾
void moveCurrentToEnd()
{
current = head;
while(current->next != NULL)
{
current = current->next;
}
}
// 显示全部记录
void display()
{
char fullname[36];
current = head;
if(current != NULL)
{
cout << endl;
cout << " 姓 名 电话号码 手机号码\n";
cout << "------------------------------ -------------- ---------------\n";
do
{
strcpy(fullname,"");
strcat(fullname, current->lastName);
strcat(fullname, ", ");
strcat(fullname, current->firstName);
cout.setf(ios::left);
cout << setw(36) << fullname;
cout.unsetf(ios::left);
cout.setf(ios::right);
cout << setw(12) << current->phoneNum;
cout.unsetf(ios::left);
cout.setf(ios::right);
cout << setw(15) << current->mobile << endl;
current = current->next;
cout << endl;
}
while(current != NULL);
cout << "Press Enter to continue \n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
}
else
{
cout << "\n记录为空!!\n";
cout << "Press Enter to continue \n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
}
}
// 按姓氏查找记录
void searchByLastname()
{
system("cls");
char searchStr[20];
current = head;
cin.ignore(20,'\n');
cout << "Search...\n";
cout << "*******************************************************************\n";
cout << "请输入姓氏: ";
cin.get(searchStr,20);
cin.ignore(20,'\n');
while((current != NULL) && (strcmp(current->lastName, searchStr) != 0))
{
current = current->next;
if(current != NULL)
{
cout << "匹配的记录:\n";
cout << current->firstName << ' ' << current->lastName << endl;
cout << current->phoneNum << ' ' << current->mobile << endl;
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
else
{
cout << "没有匹配记录!!\n";
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
}
}
// 更新记录
void update()
{
system("cls");
char searchStr[20];
current = head;
cin.ignore(20,'\n');
cout << "Update...\n";
cout << "********************************************************************\n";
cout << "请输入需要更新记录的姓氏:";
cin.get(searchStr,20);
cin.ignore(20,'\n');
while((current != NULL) && (strcmp(current->lastName, searchStr) != 0))
{
current = current->next;
}
if(current != NULL)
{
cout << "匹配的记录:\n";
cout << current->lastName << ' ' << current->firstName << ' ';
cout << current->phoneNum << ' ' << current->mobile << endl;
cout << "请您输入新的记录信息...\n";
cout << "姓氏: ";
cin.get(current->lastName,20);
cin.ignore(20,'\n');
cout << "名字: ";
cin.get(current->firstName,15);
cin.ignore(20,'\n');
cout << "电话号码: ";
cin.get(current->phoneNum,15);
cin.ignore(20,'\n');
cout << "手机号码:";
cin.get(current->mobile,15);
cin.ignore(20,'\n');
cout << "更新成功!!\n";
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
else
{
cout << "没有指定要更新的记录!!\n";
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
}
// 删除一条记录
void deleteRecord()
{
system("cls");
char searchStr[20];
record *previous;
previous = NULL;
current = head;
cin.ignore(20,'\n');
cout << "请输入需要删除的记录的姓氏: ";
cin.get(searchStr,20);
cin.ignore(20,'\n');
while((current != NULL) && (strcmp(current->lastName, searchStr) != 0))
{
previous = current;
current = current->next;
}
if(current != NULL)
{
cout << "\n匹配记录\n";
cout << current->firstName << ' ' << current->lastName << endl;
cout << current->phoneNum << ' ' << current->mobile << endl;
if(verifyDelete())
{
deleteNode(previous);
cout << "\n记录删除成功!!\n";
}
else
{
cout << "\n记录删除失败!!\n";
}
}
else
{
cout << "\n没有匹配记录,删除失败!!\n";
}
system("cls");
}
// 清空电话簿
void deleteAll()
{
if(verifyDelete())
{
deleteList();
cout << "\n记录删除成功!!\n";
}
else
{
cout << "\n记录删除失败!!\n";
}
system("cls");
}
// 帮助
void help()
{
help:
int question;
system("cls");
cout << "Help...\n";
cout << "**************************************************************\n";
cout << " 请选择帮助的主题:\n";
cout << " 1: 我的记录怎么了?\n";
cout << " 2: 我怎么样清除所有记录?\n";
cout << " 3: 怎么进行操作?\n";
cout << " 4: 怎么联系我?\n";
cout << " 5: 退出\n";
cout << "**************************************************************\n";
cout << "选择: ";
cin >> question;
switch(question)
{
case 1:
cout << "\n建议:\n";
cout << " 检查电话簿文件路径是否正确,\n";
cout << " 检查文件名是否正确.\n";
cout << "Press Enter to continue \n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
goto help;
break;
case 2:
cout << "\n建议:\n";
cout << " 返回主菜单,选择清空电话簿,或者直接删除文件PhoneBook.dat.\n";
cout << "Press Enter to contiune\n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
goto help;
break;
case 3:
cout << "\n建议:\n";
cout << " 直接选择主菜单各个功能号就OK.\n";
cout << "Press Enter to contiune\n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
goto help;
break;
case 4:
cout << "\n回答:\n";
cout << " 我的电话:025-85047627\n";
cout << " 我的地址:东大浦口校区090021信箱\n";
cout << " 我的E-mail: \n";
cout << " straydog@126.com\n";
cout << " guopp126@126.com\n";
cout << "Press Enter to contiune\n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");
goto help;
break;
default:
cout << "Press Enter To exit";
cout << "Press Enter to contiune\n";
break;
}
}
// 用户操作确认
int verifyDelete()
{
char YesNo;
cout << "\nAre you sure (Y/N) ";
cin >> YesNo;
if((YesNo == 'Y') || (YesNo == 'y'))
{
return(1);
}
else
{
return(0);
}
}
// 删除结点
void deleteNode(record *previous)
{
if(current == head)
{
deleteHead();
}
else
{
if(current->next == NULL)
{
deleteEnd(previous);
}
else
{
deleteMiddle(previous);
}
}
}
// 删除头结点
void deleteHead()
{
current = head;
if(head->next != NULL)
{
head = current->next;
}
else
{
head = NULL;
}
delete current;
}
// 删除尾结点
void deleteEnd(record *previous)
{
delete current;
previous->next = NULL;
current = head;
}
// 删除链表中间结点
void deleteMiddle(record *previous)
{
previous->next = current->next;
delete current;
current = head;
}
// 删除链表
void deleteList()
{
record *temp;
current = head;
do
{
temp = current->next;
delete current;
current = temp;
}
while(temp != NULL);
head = NULL;
}
// 把链表信息写入文件存盘
void wiriteToFile()
{
ofstream outfile;
outfile.open("PhoneBook.dat",ios::out);
if (outfile)
{
current = head;
if(head != NULL)
{
do
{
outfile << current->lastName << endl;
outfile << current->firstName << endl;
outfile << current->phoneNum << endl;
outfile << current->mobile << endl;
current = current->next;
}
while(current != NULL);
}
outfile << "End" << endl;
outfile.close();
}
else
{
cout << "打开文件错误!!\n";
}
}
// 从数据文件PhoneBook.dat中读取数据重建链表处理函数
void loadFromFile()
{
record *newRecord;
ifstream infile;
int loop = 0;
infile.open("PhoneBook.dat",ios::in);
if (infile)
{
do
{
newRecord = new record;
if(newRecord != NULL)
{
infile.get(newRecord->lastName,20);
infile.ignore(20,'\n');
if((strcmp(newRecord->lastName, "") != 0) && (strcmp(newRecord->lastName, "End") != 0))
{
infile.get(newRecord->firstName, 15);
infile.ignore(20,'\n');
infile.get(newRecord->phoneNum, 15);
infile.ignore(20,'\n');
infile.get(newRecord->mobile, 15);
insertNode(newRecord);
}
else
{
delete newRecord;
loop = 1;
}
}
else
{
cout << "警告: 存储器错误, 从磁盘加载文件失败!!\n";
loop = 1;
}
}
while(loop == 0);
infile.close();
}
else
{
cout << "电话簿为空!!\n";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -